Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking down use of functions marked for deprecation

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();
    }
}
like image 492
Scott Avatar asked Jan 06 '10 16:01

Scott


2 Answers

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.

like image 117
Gordon Avatar answered Oct 29 '22 13:10

Gordon


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.

like image 41
Pekka Avatar answered Oct 29 '22 14:10

Pekka