mi-schi / phpmd-extension
Contains extra phpmd rules from clean code book and the best practices of my experiences.
Installs: 780 950
Dependents: 5
Suggesters: 0
Security: 0
Stars: 39
Watchers: 5
Forks: 7
Open Issues: 1
Requires
- phpmd/phpmd: ^2.3
Requires (Dev)
- mockery/mockery: @stable
- phpunit/phpunit: ^4.7
README
Features
Contains extra phpmd rules from clean code book and the best practices of my experiences.
- CleanCode
- ConditionalExpression - increase readability
- ConstructorNewOperator - decouple classes, see open-close principle
- DataStructureConstants - increase maintainability
- DataStructureMethods - increase extensibility, see single-responsibility principle
- MemberPrimaryPrefix - supports low coupling, increase testability, see law of demeter
- PublicFieldDeclaration - increase maintainability, see encapsulation/information hiding
- ReturnStatement - increase reading rate, increase extensibility
- SwitchStatement - increase extensibility, see open-close principle
- TraitPublicMethod - increase maintainability, see encapsulation/information hiding
- TryStatement - increase readability
- PrivateFieldDeclaration - supports high cohesion, see DRY and single-responsibility principle
- Naming
- ClassNameSuffix - increase extensibility, see single-responsibility principle
- CommentDescription - reduce unused declarations, increase comprehensibility
- MethodName - increase readability, increase comprehensibility
- Test
- NumberOfMocks - increase testability, supports low coupling, see single-responsibility principle
- NumberOfAsserts - increase maintainability, speed up debugging, see single-responsibility principle
- MethodName - increase readability, increase comprehensibility
Installation
Download the phpmd-extension.phar
:
$ curl -OsL https://github.com/mi-schi/phpmd-extension/releases/download/stable/phpmd-extension.phar
Alternatively you can use tooly-composer-script for installation.
It is also possible to use composer (not recommended, see #5):
composer require mi-schi/phpmd-extension --dev
Usage
- Create a
phpmd.xml
file and import the basic rules from phpmd. The example below contains some useful changes. Afterwards you can extend the configuration with rules from this repository. - Then execute the mess detection with
phpmd-extension.phar [path/to/src] xml [path/to/phpmd.xml]
. Thephpmd-extension.phar
pass all arguments to the basic phpmd command. You don't have to install phpmd.phpmd-extension.phar
includes phpmd.
Basic Rules
<ruleset name="basic-rules" 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>mess detection</description> <rule ref="rulesets/cleancode.xml" /> <rule ref="rulesets/codesize.xml"> <exclude name="ExcessiveParameterList" /> <exclude name="ExcessiveMethodLength" /> <exclude name="ExcessiveClassLength" /> <exclude name="CyclomaticComplexity" /> </rule> <rule ref="rulesets/codesize.xml/ExcessiveParameterList"> <properties> <property name="minimum" value="4" /> </properties> </rule> <rule ref="rulesets/codesize.xml/ExcessiveMethodLength"> <properties> <property name="minimum" value="31" /> <property name="ignore-whitespace" value="true" /> </properties> </rule> <rule ref="rulesets/codesize.xml/ExcessiveClassLength"> <properties> <property name="minimum" value="301" /> <property name="ignore-whitespace" value="true" /> </properties> </rule> <rule ref="rulesets/codesize.xml/CyclomaticComplexity"> <properties> <property name="reportLevel" value="6" /> <property name="showClassesComplexity" value="true" /> <property name="showMethodsComplexity" value="true" /> </properties> </rule> <rule ref="rulesets/controversial.xml" /> <rule ref="rulesets/design.xml" /> <rule ref="rulesets/naming.xml"> <exclude name="ShortVariable" /> <exclude name="LongVariable" /> </rule> <rule ref="rulesets/naming.xml/ShortVariable"> <properties> <property name="minimum" value="2" /> </properties> </rule> <rule ref="rulesets/naming.xml/LongVariable"> <properties> <property name="maximum" value="30" /> </properties> </rule> <rule ref="rulesets/unusedcode.xml" /> </ruleset>
Add extra rules
<rule ref="../../../../../../rulesets/cleancode.xml" /> <rule ref="../../../../../../rulesets/naming.xml" /> <rule ref="../../../../../../rulesets/test.xml" />
You can also customize the rules with own properties or use only specific rules. Just take a look in the xml files. It works as the basic ruleset logic.
Exclude or customize extra rule
If you don't want the Law-of-Demeter rule MemberPrimaryPrefix exclude it.
<rule ref="../../../../../../rulesets/cleancode.xml"> <exclude name="MemberPrimaryPrefix" /> </rule>
If you want to customize it, use the following code:
<rule ref="../../../../../../rulesets/cleancode.xml/MemberPrimaryPrefix"> <properties> <property name="maxChainCount" value="3" description="max count of method chains" /> <property name="allowedPrefixes" value="add,set" description="allowed prefixes for the method train" /> <property name="delimiter" value="," description="delimiter for explode" /> </properties> </rule>