wptechnix / coding-standards
A modern PHP coding standard built on top of PSR-12, with extra enforcements for strictness and type safety.
Installs: 1
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Language:JavaScript
Type:phpcodesniffer-standard
pkg:composer/wptechnix/coding-standards
Requires
- php: ^8.0
- phpcsstandards/phpcsextra: ^1.4
- slevomat/coding-standard: ^8.0
- squizlabs/php_codesniffer: ^3.13.0 || ^4.0
README
PHP 8.0+ coding standards enforcing strict types, immutability, and PSR-12 compliance.
About
This is a coding standard for PHP_CodeSniffer. It builds upon rules from Slevomat Coding Standard and PHPCSExtra, with a focus on:
Type Safety:
declare(strict_types=1)required in all files- Parameter, return, property, and class constant type hints required
- Union, intersection, and DNF types supported
- Disallows mixed type hint and array type hint syntax (use generics in PHPDoc)
- Strict comparison operators (
===,!==) enforced
Modern PHP 8.0+ Features:
- Constructor property promotion required
- Arrow functions required for single-expression closures
- Null-safe operator and null coalesce operators required where applicable
- Non-capturing catch blocks (PHP 8.0+)
- Numeric literal separators for readability
- Trailing commas in multiline structures
Immutability & Code Quality:
- Readonly properties enforced, public mutable properties forbidden
- Disallows
empty()function - Early returns required
- Alphabetically sorted use statements and array keys
- Proper class member ordering and spacing
- PHPDoc requirements with proper formatting
Installation
Install this package as a development dependency using Composer.
composer require --dev wptechnix/coding-standards
Usage
To use this coding standard, add <rule ref="WPTechnix"/> to your phpcs.xml
configuration.
Here are the contents of an example phpcs.xml.dist file that you may place in
the root of your repository:
<?xml version="1.0"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> <arg name="extensions" value="php"/> <arg name="colors"/> <arg value="sp"/> <file>./src</file> <file>./tests</file> <rule ref="WPTechnix"/> </ruleset>
Then, run PHP_CodeSniffer:
./vendor/bin/phpcs
To automatically fix violations:
./vendor/bin/phpcbf
Contributing
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.
Copyright and License
The wptechnix/coding-standards library is copyright © WPTechnix and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.