Avoid messy coding with PHPMD (Atom + Ubuntu)

PHP Mess Detector aka PHPMD is a tool that helps you to write clean code. It warns you about overcomplicated code, Spaghetti code, unused code, possible bugs, etc.

PHPMD warns you in realtime, while you code:

In my opinion, PHPMD and PHPCS are the most important tools for professional PHP development. If you are curious about PHPCS, check my PHPCS+Atom tutorial here.

1. Install PHPMD (PHP Mess Detector):

Check if phpmd is installed:

phpmd -i
(out)Command 'phpmd' not found, but can be installed with:
(out)sudo apt install phpmd

Install it (if not installed):

sudo apt install phpmd

2. On Atom, install linter-phpmd package:

On Atom, install package linter-phpmd by AtomLinter:
Ctrl+Shift+P > Install Packages and Themes > linter-phpmd

Close and reopen Atom if needed.

3. Set a configuration file for your project:

Add this file to the root of your project. It enables all rules, except the “controversial” ruleset:

Disable the controversial rules!

The so-called “controversial” ruleset, unlike the others, is not related to mess prevention at all. It is just a very opinionated coding style. It enforces the use of camelCase notation. Not surprisingly, the name of the ruleset is actually “controversial ruleset”. I really don’t know why this was added to PHPMD and I strongly recommend you to disable it, even if you like camelCase (use PHPCS for this instead).

If you are developing for WordPress, it is MANDATORY to disable the controversial ruleset, as camelCase goes against WP coding style.

The PHPMD config file can alternatively be named as phpmd.xml or phpmd.ruleset.xml.

Suppressing specific warnings:

Just add @SuppressWarnings(RULE) to a comment before your class/method/command:

All – To disable ALL rules: (PHPMD)

Wildcard – To disable rules that contain “unused”: ("unused")

Suppressing warnings for the whole project:

1. Exceptions:

Let’s say you agree to the idea that variables need to have more than 2 characters (“ShortVariable” ruleset), but you would like to add exceptions for $wp and $id (common in the WP development):

2. Removing a rule:

Let’s remove the “ExitExpression” rule from the “Design” ruleset.

3. Removing an entire ruleset:

Let’s say you have ALL rules enabled in your phpmd.xml.dist file:

To remove the “Naming” ruleset, just delete its line:

Close Menu