Skip to content

Instantly share code, notes, and snippets.

@phptek
Last active February 5, 2020 10:01
Show Gist options
  • Save phptek/08a00f6b4ea697afbdee to your computer and use it in GitHub Desktop.
Save phptek/08a00f6b4ea697afbdee to your computer and use it in GitHub Desktop.
How to configure phpStorm with EditorConfig and PHPMD:
-----------------------------------------------------
PHPMD:
------
phpmd (PHP Mess Detector) is a static analysis tool that will find issues in your code you never knew you had. Your code will still run, follow a few of phpmd's recommendations though and it will be better!
phpmd is already bundled and enabled in phpstorm, but in order to more easily manage versions and add custom rulesets, you should do the following:
Get the SilverStripe rulesets via composer, which will also fetch and install a compatible version of phpmd itself:
#> composer global require --prefer-dist chillu/phpmd-silverstripe
Setup phpMd in phpStorm:
File > Settings > Languages & Frameworks > PHP > Mess Detector > Configuration "Local"
Click the "..." button and navigate to your globally installed phpmd executable e.g. /home/russellm/.composer/vendor/phpmd/phpmd/src/bin/phpmd
Click "OK"
Configure the SilverStripe ruleset by copying it into phpmd's own 'rulesets' directory
#> cp ~/.composer/vendor/chillu/phpmd-silverstripe/Rulesets/silverstripe.xml ~/.composer/vendor/phpmd/phpmd/src/main/resources/rulesets/
Setup a phpmd "Inspection" in phpStorm:
File > Settings > Editor > Inspections > Select "Project Default" for "Profile"
File > Settings > Editor > Inspections > PHP > Uncheck all > Check "PHP Mess Detector validation" > Apply > OK
File > Settings > Editor > Inspections > PHP > "PHP Mess Detector validation" > Under "Custom rulesets" click the "+" icon and navigate to /home/russellm/.composer/vendor/phpmd/phpmd/src/main/resources/rulesets/silverstripe.xml > OK
Now you should be set to run some analyses from within the IDE:
To manually run phpmd over a single PHP script in phpStorm:
Open a script and go: Code > Inspect code
To manually run phpmd over an entire directory in phpStorm:
Open a script and go: Code > Inspect code > Custom scope > Select an option or create a scope by selecting the "..." button
To have phpmd run automatically when opening code in the IDE:
- Assuming the above steps have been followd; each PHP script is automatically subjected to a phpmd analysis
- View the analysis results in the RHS gutter, at the top-right. It appears in a yellow box
- You can customise the PHP inspection highlight level by right-clicking the editor's RHS gutter and seleting "Customise highlighting level"
More info:
https://www.jetbrains.com/phpstorm/help/mess-detector.html
http://phpmd.org/
Allow you to manually invoke .editorconfig formatting (and override any existing code formatting settings)
Allow you to automatically invoke .editorconfig formatting (and override any existing code formatting settings)
How to export the lot for use by others
EditorConfig:
-------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment