Configure PHPMD (PHP Mess Detector) for Magento

PHP Mess Detector is an excellent tool for maintaining code quality. It has the ability to detect potential bugs, sub-optimal code, overcomplicated expressions, and unused parameters, methods, and properties. The tool is particularly helpful in managing cyclomatic complexity, which can become a challenge when codebases grow large.

Setting Up PHP Mess Detector

Setting up PHP Mess Detector is a straightforward process. The initial setup merely requires navigating to the PhpStorm Settings. Under the PHP subsection, select Quality Tools, and then Mess Detector. In this section, click on the triple dot icon to create a PHP interpreter and set it to your Docker phpfpm container.

The path for PHP Mess Detector should be set to /var/www/html/vendor/bin/phpmd. Once this is done, you can validate the setup. Successful validation will return the version of PHP Mess Detector at the bottom of the screen.

Creating a Custom Ruleset

The next step is to create a custom ruleset. To define the ruleset, click the plus sign. The ruleset should be located at the following specific location:

dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml

No additional boxes need to be checked. This ruleset should be effective for use with Magento. After defining the ruleset, toggle it on and click OK.

Testing PHP Mess Detector

To test the efficacy of PHP Mess Detector, paste in some code that you know will trigger the tool. If the tool is functioning correctly, you should see a green squiggly on the first line of the code. For example, PHP Mess Detector will flag unused local variables, such as $a.

While PhpStorm has improved its ability to detect such issues, there are scenarios which PHP Mess Detector can detect that PhpStorm may not, such as instances of cyclomatic complexity. Cyclomatic complexity occurs when your code becomes too large, making it difficult to maintain or rationalize.

Conclusion

In conclusion, it is highly recommended to make use of PHP Mess Detector. This tool can significantly improve the quality of your code and could potentially save your code from critical errors.

Complete and Continue