简体   繁体   English

是否可以将所有 PHP 混乱检测器检查替换为 PHP 代码嗅探器检查?

[英]Can all PHP mess-detecter checks be replaced with PHP code-sniffer checks?

I have a project which uses both PHPMD (PHP Mess Detector) and PHPCS (PHP Code Sniffer) which got me wondering if all PHPMD checks can be replaced with PHPCS checks or is it best to use both in tandem?我有一个项目同时使用 PHPMD(PHP 混乱检测器)和 PHPCS(PHP 代码嗅探器),这让我想知道是否所有 PHPMD 检查都可以替换为 PHPCS 检查,还是最好同时使用两者?

The list of PHPMD checks seems to be here: https://phpmd.org/rules/index.html but I haven't found any information on the benefits provided by PHPMD. PHPMD 检查列表似乎在这里: https ://phpmd.org/rules/index.html 但我没有找到有关 PHPMD 提供的好处的任何信息。

PHP Code Sniffer is mainly used to detect violations to a given coding standard like PSR-2. PHP Code Sniffer 主要用于检测违反给定编码标准(如 PSR-2)的情况。 It has configurations for different styles and you can configure manually each of the rules.它具有不同样式的配置,您可以手动配置每个规则。

PHP Mess Detector is more focused on the quality of the code itself - like complexity of the functions, unnecessary constructs, rules that define clean code, etc. Similarly you can configure each of the predefined rules - whether you want them to be applied or not, and possible threshold for some of the checks. PHP Mess Detector 更关注代码本身的质量——比如函数的复杂性、不必要的构造、定义干净代码的规则等。同样,您可以配置每个预定义的规则——无论您是否希望应用它们,以及某些检查的可能阈值。

To answer your questions - yes ALL of PHP MD rules can be written as CodeSniffer sniffs, but until someone has the time and patience to do so - we can keep using both :)回答您的问题 - 是的,所有 PHP MD 规则都可以编写为 CodeSniffer 嗅探,但直到有人有时间和耐心这样做 - 我们可以继续使用两者:)

For example, some of the PHPMD rules already exists in the PHPCS, like the complexity one: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php例如,PHPCS 中已经存在一些 PHPMD 规则,例如复杂性规则: https : //github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php

Also for the function/method sizes and other similar checks in PHPMD, here is a standard for PHPCS that has some of them:同样对于 PHPMD 中的函数/方法大小和其他类似检查,这里是 PHPCS 的标准,其中包含一些:

https://github.com/object-calisthenics/phpcs-calisthenics-rules https://github.com/object-calisthenics/phpcs-calisthenics-rules

And you can search for more if you really like to mimic everything.如果你真的喜欢模仿一切,你可以搜索更多。 And fill in the missing ones yourself.并自己填写缺失的。

But for the most of the time, you can use both and not bother combining them if you don't have the time, patience or some hardware/compute/time limitations.但在大多数情况下,如果您没有时间、耐心或某些硬件/计算/时间限制,您可以同时使用两者,而不必费心将它们结合起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM