The Leviy coding standard for PHP code, with rulesets for PHP CodeSniffer and PHP Mess Detector.
Install this package using Composer:
composer require --dev leviy/coding-standard
Create a file phpcs.xml
in the root of the project with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
<rule ref="LEVIY"/>
<config name="ignore_warnings_on_exit" value="1"/>
<arg name="extensions" value="php"/>
<file>src/</file>
<file>tests/</file>
<file>public/index.php</file>
</ruleset>
Create a file phpmd.xml
in the root of the project with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
name="Mess detector ruleset">
<description>Ruleset for PHP Mess Detector that enforces coding standards</description>
<rule ref="vendor/leviy/coding-standard/phpmd.xml"/>
</ruleset>
An annotated ruleset.xml
configuration file can be found here
where you can see the complete range of features and configuration options that can be used to modify the configuration to your project needs.
Documentation about tweaking the ruleset and exluding rules or changing settings can be found here.
Run vendor/bin/phpcs
to run PHP CodeSniffer. If you have a file phpcs.xml
or
phpcs.xml.dist
in root directory of the project, CodeSniffer will use it
automatically. Add a path to run CodeSniffer only against that
file or directory. See
Usage for more
detailed usage instructions.
Run vendor/bin/phpmd src/ text phpmd.xml
to run PHP Mess Detector against the
src
directory and print a report in the "text" format.
-
Open "Preferences" and navigate to "Editor > Inspections".
-
Enable "PHP > Quality tools > PHP_Code Sniffer validation".
-
Select a "Custom" coding standard.
-
Select the path to the projects code sniffer configuration.
Enjoy the immediate feedback of code sniffer validation issues!