跟踪标记为弃用的功能的使用情况

Sco*_*ott 5 php function deprecated

遵循以下主题:如何处理库中的函数弃用?我想找到一种方法来跟踪对已弃用函数的所有调用,这样我就可以确保在删除函数之前将它们全部替换掉​​.给出以下PHP方法

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

function getBar(){
    return "bar";
}
Run Code Online (Sandbox Code Playgroud)

我想出了以下方法,我正在寻找反馈.

function getFoo(){
    try{
        throw new Exception("Deprecated function used"); 
    } catch(Exception $e){
         //Log the Exception with stack trace
         ....
         // return value as normal
         return getBar();
    }
}
Run Code Online (Sandbox Code Playgroud)

Gor*_*don 4

对于 PHP 内部不推荐使用的函数,只需将 E_STRICT 添加到error_reporting即可。

对于要引发有关已弃用函数的通知或警告的用户态函数,我建议花时间添加注释的开发人员@deprecated也触发E_USER_DEPRECATED警告,例如

function getFoo(){
    trigger_error(__FUNCTION__ . 'is deprecated', E_USER_DEPRECATED );
    return getBar();
}
Run Code Online (Sandbox Code Playgroud)

我不知道任何可用的 QA 工具是否可以自动检测代码是否包含已弃用的方法调用。不过,这些是你最好的选择。

如果您使用代码覆盖率为 100% 的 TDD,则无需担心删除已弃用的方法或函数。你的自动化测试只会失败,你就会知道去哪里寻找。