企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 如何创建自定义规则集 如果只想挑选PHPMD自带的部分规则,或者修改预定义阈值,你可以创建自定义规则集文件来注明(你自己的)自定义规则集合。 ## 以一个空ruleset.xml开始 开始一个新规则集最简单的方式是拷贝一个现有文件然后从文件体中去掉所有规则标签。或者你可以使用模板作为规则集文件。记得修改`@name`和`<description />`的内容。 ```xml <?xml version="1.0"?> <ruleset name="My first PHPMD rule set" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd"> <description> My custom rule set that checks my code... </description> </ruleset> ``` ## 添加规则引用到新ruleset.xml 首先是要在新规则集文件中加入整个[unused code](https://phpmd.org/rules/unusedcode.html)规则。`<rule />`元素可以引用PHPMD内建的这个规则集。 ```xml <?xml version="1.0"?> <ruleset name="My first PHPMD rule set" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd"> <description> My custom rule set that checks my code... </description> <!-- Import the entire unused code rule set --> <rule ref="rulesets/unusedcode.xml" /> </ruleset> ``` 很好。自定义规则集适用于分析源代码的整个`unused code `代码规则。 我们也经常在自定义规则集中使用`codesize`规则集的`cyclomatic complexity`规则。可以使用`<rule />`元素`@ref`属性达到目的。 ```xml <?xml version="1.0"?> <ruleset name="My first PHPMD rule set" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd"> <description> My custom rule set that checks my code... </description> <!-- Import the entire unused code rule set --> <rule ref="rulesets/unusedcode.xml" /> <!-- Import the entire cyclomatic complexity rule --> <rule ref="rulesets/codesize.xml/CyclomaticComplexity" /> </ruleset> ``` 现在,我们想改变`cyclomatic complexity`规则的某些属性。我们先把规则的优先权改成最高值1,然后降低报告冲突时的阈值。使用的xml元素可以从原始的规则集文件找。 ```xml <?xml version="1.0"?> <ruleset name="My first PHPMD rule set" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd"> <description> My custom rule set that checks my code... </description> <!-- Import the entire unused code rule set --> <rule ref="rulesets/unusedcode.xml" /> <!-- Import the entire cyclomatic complexity rule and customize the rule configuration. --> <rule ref="rulesets/codesize.xml/CyclomaticComplexity"> <priority>1</priority> <properties> <property name="reportLevel" value="5" /> </properties> </rule> </ruleset> ``` PHPMD附加处理自定义设置。这就是说PHPMD保留所有没有自定义设置的原始配置。 ## 排除规则 最后我们要重用PHPMD的`naming`规则集。但是我们必须排除两个经常变化的`naming规则`。通过在规则引用中声明`<exclude />`元素可以实现排除。这个元素有一个属性@name,它指定了排除规则的名字。 ```xml <?xml version="1.0"?> <ruleset name="My first PHPMD rule set" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd"> <description> My custom rule set that checks my code... </description> <!-- Import the entire unused code rule set --> <rule ref="rulesets/unusedcode.xml" /> <!-- Import the entire cyclomatic complexity rule and customize the rule configuration. --> <rule ref="rulesets/codesize.xml/CyclomaticComplexity"> <priority>1</priority> <properties> <property name="reportLevel" value="5" /> </properties> </rule> <!-- Import entire naming rule set and exclude rules --> <rule ref="rulesets/naming.xml"> <exclude name="ShortVariable" /> <exclude name="LongVariable" /> </rule> </ruleset> ``` ## 总结 你可以使用PHPMD的规则集语法定制规则的所有部分,也可以在你自己的规则集中重用所有现存规则集xml文件。当创建自定义规则集时如果碰巧不知道有什么规则或者某规则可用设置的话,可以看看PHPMD的规则文档。PHPMD自带规则集文件也可以很好的参考。