开发者

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 doi开发者_开发百科ng 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();
    }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜