# 如何創建自定義規則集
如果只想挑選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自帶規則集文件也可以很好的參考。