A bug can unexpectedly occur in complex or tricky coding. On top of that, due to a single typo, it may be that the variable or method isn’t actually being used like you wish. There is actually a lot of software out there that point out these kinds of coding problems.
On this occasion we will be introducing you to PHPMD, as used in the PHP project. It is a piece of PMD software by PHP, which bears a similarity to Java PMD. It clearly indicates the areas where bugs could occur, any coding that isn’t suitable, complex expressions, and any unused parameters/methods, properties.
You can install PHPMD in a number of ways. If you use Composer, please type in the command as follows:
composer global require "phpmd/phpmd=@stable"
Installing it this way will install it into
~/.composer/vendor/bin/. It’s best to go through the path.
Another method is to install it only within the project itself. Input the command into Composer as follows:
composer require "squizlabs/php_codesniffer=*"
This method will install it into
The third way of installing it is to directly download the file.
wget -c http://static.phpmd.org/php/latest/phpmd.phar php phpmd.phar --help
Loading it will show
How to use it.
Let’s first introduce you to the options available.
$ ~/.composer/vendor/bin/phpmd --help Mandatory arguments: 1) A php source code filename or directory. Can be a comma-separated string 2) A report format 3) A ruleset filename or a comma-separated string of rulesetfilenames Available formats: xml, text, html. Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode. Optional arguments that may be put after the mandatory arguments: --minimumpriority: rule priority threshold; rules with lower priority than this will not be used --reportfile: send report output to a file; default to STDOUT --suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml --exclude: comma-separated string of patterns that are used to ignore directories --strict: also report those nodes with a @SuppressWarnings annotation --ignore-violations-on-exit: will exit with a zero code, even if any violations are found
There are three arguments altogether.
- Specify a source code or directory. Put a comma between each one if specifying more than one.
- Format the report. You can specify between xml, text or HTML.
- Rule set. There are options of: cleancode, codesize, controversial, design, naming, and unusedcode.
It may take some time to complete, depending on the number of files. The following results will be displayed if you specified “text”.
/path/to/your/project/Acl.php:101 The method setAccess uses an else expression. Else is never necessary and you can simplify the code to work without else. /path/to/your/project/ApiClient.php:47 Avoid using static access to class 'Ncmb\NCMB' in method 'create'. /path/to/your/project/ApiClient.php:121 Avoid using static access to class 'Ncmb\User' in method 'request'.
The following results will be displayed if you specified “HTML”. It will prove useful to save this file via the reportfile option.
Regarding rule sets.
The rules applicable to PHPMD are as follows:
- cleancode rule: cleancode
This points out any messy coding issues.
- codesize rule: codesize This will split any classes that are too big, or methods that are too frequent, to reduce the code size.
- discussion rule: controversial
This points out any frequently occurring problems with camel case and snake case when developing via PHP, so that camel case can be used.
- design rule: design
- Points out any problems relating to the software design.
- naming rule: naming
Points out any names that are too long or too short.
- unusedcode rule: unusedcode Searches for and points out any coding that isn’t being used.
Specify the desired rules (multiple options are possible) to check your coding.
Sider corresponds with PHPMD, as used in the PHP Project code review. It is very useful because GitHub push links to PHPMD and gives an automatic review, and also makes use of cloud services for multiple people during development. Please endeavor to try and use it.
When running the entire project, the nature of the tool itself can also cause PHPMD to take some time to analyze. SideCI is a specialized code reviewing service, which means that it takes extra care when analyzing PHPMD target reviews. PHPMD analysis results can be received in a practical amount of time. It would be great if people who already use PHPMD and the like through CI services such as Jenkins or SonarQube, could give SideCI a try.