wptechnix/coding-standards
最新稳定版本:v2.1.2
Composer 安装命令:
composer require wptechnix/coding-standards
包简介
A modern PHP coding standard (PHPCS) compatible with PSR-12, featuring additional rules for strictness and type safety.
关键字:
README 文档
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:^2.0"
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:
- WPTechnix WordPress Coding Standards - Extends this standard with WordPress-specific rules and conventions
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.
统计信息
- 总下载量: 180
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 3
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-12