phpfunctiondeprecated

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();
    }
}

Solution

  • 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.