追踪标记为弃用的函数的使用情况

发布于 2024-08-17 04:01:17 字数 633 浏览 2 评论 0原文

按照此线程:如何处理库中的函数弃用? 我想要找到一种方法来跟踪对已弃用函数的所有调用,以便我可以确保在删除该函数之前将它们全部替换。鉴于以下 PHP 方法,

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

function getBar(){
    return "bar";
}

我想出了以下方法,并且正在寻求反馈。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-08-24 04:01:17

对于 PHP 内部已弃用的函数,只需将 E_STRICT 添加到 error_reporting 即可。

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

function getFoo(){
    trigger_error(__FUNCTION__ . 'is deprecated', E_USER_DEPRECATED );
    return getBar();
}

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

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

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.

流星番茄 2024-08-24 04:01:17

依赖实际调用的已弃用函数是危险的 - 您将拥有 100% 的代码覆盖率以确保您不会错过任何内容。慢慢地找到所有对已弃用函数的调用并一一替换它们是可以的,但对于完全过渡来说还不够好。

我认为文件> 在 IDE 中搜索文件

是最好的选择,因为据我所知,PHP 没有好的重构工具。

事后思考:也许PhpXRef是解决方案。

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.

笨笨の傻瓜 2024-08-24 04:01:17

多年后仍然是当前的话题。最近,我惊讶地发现 php 8 的流行 lint 产品并没有普遍检查已弃用的函数或其他结构。我认为我做出这样的假设并不是没有道理的。为了检查自 php 5.4 升级到 php 8.3 以来的所有弃用和更改(当没有此类硬件可用于测试时),我最终所做的就是浏览所有适用的 RFC 文档并制作我自己的列表。手动逐项检查该列表就完成了工作,而我使用的 lint 产品错过了该列表上的关键项目。我找不到任何 lint 的 php8 的 Windows 软件 - 也许我的搜索错过了它们。唯一的选择是在 Windows 上创建 php8 实例,并使用“php -l”检查文件。但我认为这不会发现弃用。当我访问 8.3 服务器时,我将在一些测试文件上尝试,看看会发生什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文