The shared PHP_CodeSniffer ruleset used by all phpmath libraries. It is built on the Doctrine Coding Standard (PSR-12 based, plus Slevomat sniffs) with a few phpmath specific changes.
The full documentation is at https://phpmath.github.io/coding-standard/. Every change is listed in the changelog and every release on the releases page.
composer require --dev phpmath/coding-standardComposer asks whether to allow the dealerdirect/phpcodesniffer-composer-installer plugin, which registers the standard with PHP_CodeSniffer. Allow it in your composer.json:
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}- PHP 8.4 or newer
doctrine/coding-standard
Create a phpcs.xml.dist in your project and reference the phpmath standard:
<?xml version="1.0"?>
<ruleset name="my/library">
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="cache" value=".phpcs-cache"/>
<file>src/</file>
<file>tests/</file>
<rule ref="phpmath"/>
</ruleset>Run vendor/bin/phpcs to check and vendor/bin/phpcbf to fix what can be fixed automatically. Individual sniffs can be disabled with <exclude name="..."/> inside the <rule ref="phpmath"> element.
declare(strict_types=1);in every file (SlevomatCodingStandard.TypeHints.DeclareStrictTypes).- Native parameter, return and property types (
ParameterTypeHint,ReturnTypeHint,PropertyTypeHint). - No unused imports and imports sorted alphabetically (
Namespaces.UnusedUses,Namespaces.AlphabeticallySortedUses). - Classes are
finalorabstract(Classes.RequireAbstractOrFinal). - Classes that can be
readonlyarereadonly(Classes.ReadonlyClass), supporting immutable value objects. - Constructor property promotion, null coalescing and null safe operators, short nullable syntax (
?int). - Formatting: PSR-12 as extended by the Doctrine Coding Standard.
Everything else comes from the Doctrine ruleset. List all active sniffs with vendor/bin/phpcs -e --standard=phpmath.
The ruleset is tested against small fixtures: tests/fixtures/pass must produce no violations, and every file in tests/fixtures/fail must trigger the sniff it is named for. The same script rejects the em dash character anywhere in the repository.
composer install
composer testAll contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
If you discover a security issue, please create an issue in the issue tracker.
The MIT License (MIT). Please see License File for more information.