phpmath / coding-standard
The coding standards for PHP_CodeSniffer we use at phpmath
Package info
github.com/phpmath/coding-standard
Language:Shell
Type:phpcodesniffer-standard
pkg:composer/phpmath/coding-standard
Requires
- php: ^8.4
- doctrine/coding-standard: ^14.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 12:59:29 UTC
README
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.
Documentation
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.
Install
composer require --dev phpmath/coding-standard
Composer 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 } }
Requirements
- PHP 8.4 or newer
doctrine/coding-standard
Usage
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.
What is enforced
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.
Development
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 test
Contributing
All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
Security
If you discover a security issue, please create an issue in the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.