This coding standard is meant to be used on Magento projects and modules.
It uses the following rulesets:
To use this ruleset, require it in composer:
composer require --dev smile/magento2-smilelab-phpcs
Two rulesets are available:
SmileLab
(Magento >=2.4.4)SmileLab-237-243
(Magento >=2.3.7 <2.4.4)
Older versions of Magento (<2.4.4) require a separate ruleset, because they use an outdated version of the Magento coding standard.
Create a configuration file namedphpcs.xml.dist
at the root of your project.
Example for a Magento project:
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<arg name="basepath" value="."/>
<arg name="extensions" value="php,phtml"/>
<arg name="colors"/>
<arg value="p"/>
<arg value="s"/>
<rule ref="SmileLab"/>
<file>app/code</file>
<file>app/design</file>
</ruleset>
Example for a community module:
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<arg name="basepath" value="."/>
<arg name="extensions" value="php,phtml"/>
<arg name="colors"/>
<arg value="p"/>
<arg value="s"/>
<config name="php_version" value="{{min_php_version}}"/>
<rule ref="SmileLab"/>
<file>.</file>
<exclude-pattern>vendor/*</exclude-pattern>
</ruleset>
Where {{min_php_version}}
is the minimum compatible version of PHP required by your module.
For example, if the min version is PHP 7.4:
<config name="php_version" value="70400"/>
You can run phpcs with this command:
vendor/bin/phpcs --extensions=php,phtml
You can fix most of the errors found with:
vendor/bin/phpcbf --extensions=php,phtml
If your class overrides a method declared in a parent class, use @inheritdoc
:
/**
* @inheritdoc
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
}