spryker/code-sniffer
Spryker Code Sniffer is a PHP_CodeSniffer ruleset for Spryker projects. Enforces Spryker coding standards and best practices, supports automated code style checks in CI, and helps keep code consistent across teams and modules.
This sniffer package follows PSR-2 completely and ships with a lot of additional fixers on top (incl. PSR-12). Please see the Spryker Coding conventions for details.
See docs.
Upstream docs: squizlabs/PHP_CodeSniffer/wiki
Make sure you include the sniffer as require-dev dependency:
composer require --dev spryker/code-sniffer
The Development module provides a convenience command:
console code:sniff:style
(or console c:s:s as shortcut)
To automatically fix fixable errors, use
console code:sniff:style -f
-v is useful for more info output.
To run only a specific sniff, use the -s option. See -h for help.
You can also sniff a specific project level module or path:
console code:sniff:style [-m ModuleName] [optional-sub-path] -v
You can also manually invoke the phpcs/phpcbf commands:
vendor/bin/phpcs --standard=vendor/spryker/code-sniffer/Spryker/ruleset.xml ./
vendor/bin/phpcbf --standard=vendor/spryker/code-sniffer/Spryker/ruleset.xml ./
The command phpcs just sniffs, phpcbf fixes.
You probably want to ignore some folders, e.g. --ignore=vendor/ or some of your test fixture folders.
You can always switch the standard to the stricter one named SprykerStrict.
It is an extension of the Spryker standard with its own (strict) sniffs added on top.
There is also an opt-in SprykerConventions standard with opinionated, project-wide
convention sniffs (e.g. dependency-provider closure registration, plugin pre/post naming,
non-portable SQL). It is deliberately not part of Spryker/SprykerStrict, so bumping
the package version never activates these on existing code. Opt in from your own ruleset with
<rule ref="SprykerConventions"/> (or reference individual sniffs) once you are ready to adopt them.
E.g. for PHPStorm:
$ProjectFileDir$/vendor/bin/phpcs, Parameters to --standard=$ProjectFileDir$/vendor/spryker/code-sniffer/Spryker/ruleset.xml -p $FilePath$ and Working directory to $ProjectFileDir$.$ProjectFileDir$/vendor/bin/phpcbf, Parameters to --standard=$ProjectFileDir$/vendor/spryker/code-sniffer/Spryker/ruleset.xml -v $FilePath$ and Working directory to $ProjectFileDir$.Control + Comma for sniffing, and Control + Dot for fixing.You can also set up file watchers, but here you should better only whitelist certain sniffs that only add things and don't remove anything.
In order to simplify command line interface, phpcs allows to specify default rule set in and standards path the following way.
Assuming the following directory structure:
vendor/spryker/code-sniffer/ # Base directory
|_ Spryker/ # Rule set name
|_ ruleset.xml # Rule set
The base directory and rule set can be used in configuration now.
vendor/bin/phpcs --config-set installed_paths vendor/spryker/code-sniffer/
vendor/bin/phpcs --config-set default_standard Spryker
You might need to specify full directory path. Now the tools can be used without --standard switch.
You can exchange or extend the Spryker coding standard by providing your own ruleset.xml. This can be configured in the Development module config:
// DevelopmentConfig.php
/**
* Either a relative or full path to the ruleset.xml or a name of an installed
* standard (see `phpcs -i` for a list of available ones).
*
* @return string
*/
public function getCodingStandard()
{
return '/path/to/your/ruleset.xml';
}
If you use it for custom projects, just use --standard to point to your ruleset file.
Make sure that you include the Spryker core standard ruleset in your custom one, e.g.:
<?xml version="1.0"?>
<ruleset name="SprykerProject">
<description>
Spryker Coding Standard for Project.
Extends main Spryker Coding Standard.
All sniffs in ./Sniffs/ will be auto loaded
</description>
<rule ref="vendor/spryker/code-sniffer/Spryker/ruleset.xml"/>
<exclude-pattern>*/src/Generated/*</exclude-pattern>
<exclude-pattern>*/src/Orm/*</exclude-pattern>
<exclude-pattern>*/tests/_support/_generated/*</exclude-pattern>
<exclude-pattern>*/tests/_helpers/*</exclude-pattern>
<exclude-pattern>*/tests/_output/*</exclude-pattern>
<exclude-pattern>./data/DE/*</exclude-pattern>
<!-- Define your own sniffs here -->
</ruleset>
If you want to use the SprykerStrict standard in your project, you should replace the string:
<rule ref="vendor/spryker/code-sniffer/Spryker/ruleset.xml"/>
with this one:
<rule ref="vendor/spryker/code-sniffer/SprykerStrict/ruleset.xml"/>
How can I help you explore Laravel packages today?