views:

64

answers:

2

Following this thread: How to handle functions deprecation in library? I want to find a way to track all calls to the deprecated function so I can make sure I get them all replaced before the function is removed. Given the following PHP methods

/*
   @deprecated - just use getBar()
*/
function getFoo(){
    return getBar();
}

function getBar(){
    return "bar";
}

I came up with the following method of doing so and I'm looking for feedback.

function getFoo(){
    try{
        throw new Exception("Deprecated function used"); 
    } catch(Exception $e){
         //Log the Exception with stack trace
         ....
         // return value as normal
         return getBar();
    }
}
+1  A: 

Relying on the deprecated function being actually called is dangerous - you would have 100% code coverage to make sure you don't miss anything. It's all right for slowly finding all calls to deprecated functions and replace them one by one, but not good enough for a complete transition.

I think File > Search in Files

in your IDE is your best bet, as there are no good refactoring tools around for PHP that I know of.

Afterthought: Maybe PhpXRef is the solution.

Pekka
Pekka, this is often the approach that is taken. However, this approach fails to find instances that were created after the search in files was done.(Say a different developer doesn't read the documentation). The method above was designed to offer yet another way to 'contact' the developer, yet is still dependent on the developer reading his error logs.
Scott
A combination of both methods is probably the best.
Pekka
+2  A: 

For PHPs internal deprecated functions, just add E_STRICT to error_reporting.

For userland functions to raise Notice or Warning about deprecated functions, I'd suggest the developer who took the time to add the @deprecated annotation also triggers an E_USER_DEPRECATED warning, e.g.

function getFoo(){
    trigger_error(__FUNCTION__ . 'is deprecated', E_USER_DEPRECATED );
    return getBar();
}

I am not aware if any of the available QA tools can automatically detect if code contains deprecated method calls. Those are your best bet though.

You shouldn't need to be concerned about removing deprecated methods or functions if you are using TDD with 100% code coverage. Your automated tests will simply fail and you will know where to look.

Gordon
+1 for spreading the gospel of running at E_STRICT :)
therefromhere
Gordon, I like your solution as it makes feel like I'm not abusing Exceptions. One downside I see is that it doesn't offer the programmer where the offending call was made, like the stack trace does, but just that the call was made.
Scott
Scott: feel free to add any of the magic constants to adjust the error message as you see fit: http://us.php.net/manual/en/language.constants.predefined.php or include data from `debug_backtrace()` - the example above is deliberately kept short for readability.
Gordon