PhpStorm 2022.3 Help

PHP Mess Detector

In addition to built-in coding assistance, PhpStorm provides checking the source code through integration with the PHP Mess Detector tool, which detects potential problems related to code size, inconsistency, unused code, violation of naming conventions, poor design, and so on.

To use PHP Mess Detector from PhpStorm instead of command line, you need to register it in PhpStorm and configure it as a PhpStorm code inspection. Once installed and enabled in PhpStorm, the tool is available in any opened PHP file, and no additional steps are required to launch it. The on-the-fly code check is activated upon every update in the file thus making it easy to get rid of discovered problems.

Errors and warnings reported by PHP Mess Detector on-the-fly are displayed as popup messages. When the tool is run in the batch mode, the errors and warnings are displayed in the Problems tool window. Each message has the phpmd prefix to distinguish it from PhpStorm internal inspections.

You can have predefined rules applied or define your own custom set of rules.

Prerequisites

Prior to integrating PHP Mess Detector in PhpStorm, make sure the following prerequisites are met:

  • The directory containing the PHP engine executable must be added to the system path. This allows code quality tool scripts execute calls to the system-wide PHP engine.

  • For Docker Compose-based remote interpreters, make sure to use docker-compose exec mode to avoid spawning additional containers.

    1. In the Settings dialog (Ctrl+Alt+S), go to PHP.

    2. On the PHP page that opens, click the Browse button next to the CLI Interpreter list.

    3. In the CLI Interpreters dialog that opens, set the Lifecycle mode for the selected interpreter to Connect to existing container ('docker-compose exec') .

      docker-compose exec for PHP interpreter

Install and configure PHP Mess Detector

Install PHP Mess Detector with Composer

When you install PHP Mess Detector with Composer, PhpStorm automatically downloads the necessary scripts, registers them in the IDE, and, optionally, enables and configures the corresponding code inspection.

  1. Inside composer.json, add the phpmd/phpmd dependency record to the require or require-dev section. Press Ctrl+Space to get code completion for the package name and version.

  2. Do one of the following:

    • Click the Install shortcut link on top of the editor panel.

    • If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

Click the Settings button next to the package record in the composer.json editor gutter to jump to the corresponding Settings page and configure PHP Mess Detector manually.

Gutter icon for phpmd settings in composer.json

Reset PHP Mess Detector configuration

After PHP Mess Detector is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHP Mess Detector configuration.

  1. In the Settings dialog (Ctrl+Alt+S), navigate to PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the Mess Detector area and click the Browse button next to the Configuration list.

  3. In the PHP Mess Detector dialog that opens, empty the PHP Mess Detector path field.

    Empty PHP Mess Detector path field
  4. Update the project Composer dependencies by clicking Update on top of the composer.json editor panel. See Update dependencies for details.

PhpStorm will perform the PHP Mess Detector configuration anew and thus apply the changes in composer.json.

Configure PHP Mess Detector in PhpStorm

In Settings (Ctrl+Alt+S) | PHP | Quality Tools | PHP Mess Detector, you can change the default PHP interpreter, set the path to a manually downloaded and installed PHP Mess Detector executable file, or add some options to be passed to PHP Mess Detector when running it in PhpStorm.

PHP Mess Detector settings
  • Configuration: in this field, you can change the default PHP interpreter and path to the PHP Mess Detector executable file.

    1. To only change the interpreter, select the required item from the list of local and remote PHP interpreters configured in your project.

    2. To change the path to the PHP Mess Detector executable file, or both the interpreter and the path, click the Browse button next to the Configuration list to open the PHP Mess Detector dialog and edit the fields there as described on the Mess Detector Dialog reference page.

  • Show ignored files: use this setting to exclude files from PHP Mess Detector validation inspection. For details, see the Quality Tools reference page.

  • Options: in this area, select the checkboxes next to the validations to be performed when running PHP Mess Detector as a PhpStorm inspection. For details, see the PHP Mess Detector reference page.

Enable PHP Mess Detector as a PhpStorm inspection

Configure the PHP Mess Detector inspection with Composer

You can include the information on the default and custom PHP Mess Detector rulesets inside the scripts section of composer.json. When you install or update project dependencies, the specified rulesets will be detected and the PHP Mess Detector validation inspection will be enabled automatically.

If no ruleset is specified in the scripts section of composer.json, PhpStorm will additionally check the project root to locate the ruleset with the phpmd.xml default name. If the file is present, it will be automatically selected as the inspection's Custom ruleset.

  • In the scripts section of composer.json, add the phpmd PHP Mess Detector launch command into one of the leaf elements.

    Provide the names of the built-in standards or the paths to custom rulesets as the arguments to denote the coding standards used.

    For example, adding the following record will select the Codesize and Controversial built-in rulesets, as well as the custom ruleset defined in the /my/src/custom_ruleset.xml file:

    "scripts": { "phpmd": "phpmd codesize,controversial,/my/src/custom_ruleset.xml" }

Enable PHP Mess Detector validation in Inspections settings

  1. In the Settings dialog (Ctrl+Alt+S), click Inspections under Editor.

  2. On the Inspections page that opens, expand the PHP | Quality Tools node and select the checkbox next to PHP Mess Detector validation.

    Select PHP Mess Detector validation checkbox
  3. On the right-hand pane of the page, configure how PhpStorm handles the PHP Mess Detector inspection output:

    1. Scope: choose the scope to limit the inspection application to.

    2. Severity: choose the severity degree for the inspection. The selected value determines how seriously the detected discrepancies will be treated by PhpStorm and presented in the inspection results.

    3. Highlighting in editor: choose how the issues detected by the inspection are highlighted in the editor.

Share a custom coding style with the team

  1. Put the root directory of your coding standard under the project root.

  2. Configure PHP Mess Detector as a PhpStorm inspection.

  3. Appoint your coding standard.

  4. Make sure that a project profile is selected at the top of the Inspections page. Such profiles are saved in a particular project's .idea directory (for example, $PROJECT_DIR$/.idea/inspectionProfiles). See Configure profiles for details.

  5. On the Version Control page of the Settings dialog, make sure that the .idea directory is put under version control.

Run PHP Mess Detector in the batch mode

  1. From the main menu, select Code | Inspect code.

  2. In the Specify Inspection Scope dialog that opens, select the inspection profile from the list, or click Configure to open the Inspections dialog and configure a new profile.

    You can also click Configure to check which fixes will be applied within the scope of the selected inspection profile, and make sure that the PHP Mess Detector validation inspection is enabled.

  3. View the inspection results in the Problems tool window. Errors and warnings reported by PHP Mess Detector are prefixed with phpmd to distinguish them from PhpStorm internal inspections.

Exclude files from PHP Mess Detector Validation inspection

When waiting for Mess Detector response exceeds the limit specified in the Tool process timeout field in the Mess Detector dialog, PhpStorm suggests adding the file to the ignore list.

  1. In the Settings dialog (Ctrl+Alt+S), navigate to PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the Mess Detector area and click the Show ignored files link.

    • To add a file, click the Add button and locate the desired file in the dialog that opens.

    • To delete a file from the list and have Mess Detector process it again, select the file and click the Remove button.

    • To remove all the files from the list, click the Close button.

Last modified: 21 April 2023