PHP 8.0+ coding standards enforcing strict types, immutability, and PSR-12 compliance.
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
Install this package as a development dependency using Composer.
composer require --dev wptechnix/coding-standardsFor automatic registration of the standard with PHP_CodeSniffer, optionally install the Dealerdirect Composer Installer:
composer require --dev dealerdirect/phpcodesniffer-composer-installerWithout Dealerdirect, you can still use the standard by manually referencing it in your phpcs.xml file.
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/phpcsTo automatically fix violations:
./vendor/bin/phpcbfThis base standard can be extended for project-specific coding requirements. The following projects xtend WPTechnix coding standards:
- WPTechnix WordPress Coding Standards - Extends this standard with WordPress-specific rules and conventions
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.
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.