Skip to content

A modern PHP coding standard (PHPCS) compatible with PSR-12, featuring additional rules for strictness and type safety.

License

Notifications You must be signed in to change notification settings

WPTechnix/coding-standards

WPTechnix Coding Standards

PHP 8.0+ coding standards enforcing strict types, immutability, and PSR-12 compliance.

Source Code Download Package PHP Programming Language Read License

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

Automatic Registration (Optional)

For automatic registration of the standard with PHP_CodeSniffer, optionally install the Dealerdirect Composer Installer:

composer require --dev dealerdirect/phpcodesniffer-composer-installer

Without Dealerdirect, you can still use the standard by manually referencing it in your phpcs.xml file.

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

Project-Specific Coding Standards

This base standard can be extended for project-specific coding requirements. The following projects xtend WPTechnix coding standards:

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.

About

A modern PHP coding standard (PHPCS) compatible with PSR-12, featuring additional rules for strictness and type safety.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •