PHP Mess Detector is one of those tools that helps keep the code base manageable and clean. We are 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 occur, any unsuitable coding, complex expressions, unused parameters/methods and properties.
Here you have the description directly from the site:
It is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.
What PHPMD does is: It takes a given PHP source code base and look for several potential problems within that source. These problems can be things like:
- Possible bugs
- Suboptimal code
- Overcomplicated expressions
- Unused parameters, methods, properties
$ composer global require phpmd/phpmd
You can also install it only for your project, but if you are using it with more than one project, I recommend you to install it globally instead.
Now, phpmd is running and monitoring your code all the time. Here example:
We will explain its use through the console
$ 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
It may take some time to complete, depending on the number of files. The following results will be displayed if you specified “text”.
The rules applicable to PHPMD are as follows:
Specify the desired rules to check your coding (multiple options are possible).
$ phpmd web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php text cleancode,codesize,controversial,design,naming,unusedcode /Proyectos/Test/Web/data/www/keopx/web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php:25 The parameter $field_definition is not named in camelCase. /Proyectos/Test/Web/data/www/keopx/web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php:61 Avoid unused local variables such as '$a'. /Proyectos/Test/Web/data/www/keopx/web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php:71 The method preSave uses an else expression. Else is never necessary and you can simplify the code to work without else.