Reputation: 11206
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();
}
}
Upvotes: 5
Views: 640
Reputation: 131
Still a current topic, years later. I was recently surprised to find that popular lint products for php 8 do not universally check for deprecated functions or other constructs. I do not think I was unreasonable to make the assumption that they would. What I ended up doing in order to check all deprecations and changes since php 5.4 when upgrading to php 8.3 (when no such hardware was available for testing) was to go through all of the RFC documents that applied and make my own list. Going through that list manually item by item got the job done, whereas the lint product I used missed key items on that list. I could not find any Windows software that lint's php8 - maybe my search missed them. The only alternative was to create a php8 instance on Windows, and use 'php -l' to check the files. I don't think that would have found the deprecations though. When I get access to the 8.3 server, I will try that on a few test files and see what happens.
Upvotes: 1
Reputation: 317119
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.
Upvotes: 4
Reputation: 449613
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.
Upvotes: 1