drevops/phpcs-standard
Composer 安装命令:
composer require drevops/phpcs-standard
包简介
DrevOps PHP_CodeSniffer rules: enforce consistent naming (snakeCase or camelCase) for variables and parameters, PHPUnit data provider conventions.
关键字:
README 文档
README
DrevOps PHP_CodeSniffer Standard
PHP_CodeSniffer standard enforcing:
- Consistent naming conventions for local variables and function/method parameters (configurable:
snakeCaseorcamelCase) - PHPUnit data provider naming conventions and organization
Installation
composer require --dev drevops/phpcs-standard
The standard is automatically registered via phpcodesniffer-composer-installer.
Verify: vendor/bin/phpcs -i (should list DrevOps)
Usage
# Check code vendor/bin/phpcs --standard=DrevOps path/to/code # Auto-fix vendor/bin/phpcbf --standard=DrevOps path/to/code
Configuration
Create phpcs.xml:
<?xml version="1.0"?> <ruleset name="Project Standards"> <rule ref="DrevOps"/> <file>src</file> <file>tests</file> </ruleset>
Use individual sniffs:
<ruleset name="Custom Standards"> <!-- Naming Conventions --> <rule ref="DrevOps.NamingConventions.LocalVariableNaming"/> <rule ref="DrevOps.NamingConventions.ParameterNaming"/> <!-- Testing Practices --> <rule ref="DrevOps.TestingPractices.DataProviderPrefix"/> <rule ref="DrevOps.TestingPractices.DataProviderMatchesTestName"/> <rule ref="DrevOps.TestingPractices.DataProviderOrder"/> </ruleset>
Configure naming convention
By default, both sniffs enforce snakeCase. Configure to use camelCase:
<ruleset name="Custom Standards"> <rule ref="DrevOps.NamingConventions.LocalVariableNaming"> <properties> <property name="format" value="camelCase"/> </properties> </rule> <rule ref="DrevOps.NamingConventions.ParameterNaming"> <properties> <property name="format" value="camelCase"/> </properties> </rule> </ruleset>
LocalVariableNaming
Enforces consistent naming convention for local variables inside functions/methods.
With snakeCase (default):
function processOrder() { $order_id = 1; // ✓ Valid $orderId = 1; // ✗ Error: NotSnakeCase }
With camelCase:
function processOrder() { $orderId = 1; // ✓ Valid $order_id = 1; // ✗ Error: NotCamelCase }
Excludes:
- Function/method parameters (handled by
ParameterNaming) - Class properties (not enforced)
- Reserved variables (
$this,$_GET,$_POST, etc.)
Error codes
DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase(whenformat="snakeCase")DrevOps.NamingConventions.LocalVariableNaming.NotCamelCase(whenformat="camelCase")
Ignore
// phpcs:ignore DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase $myVariable = 'value';
ParameterNaming
Enforces consistent naming convention for function/method parameters.
With snakeCase (default):
function processOrder($order_id, $user_data) { // ✓ Valid function processOrder($orderId, $userData) { // ✗ Error: NotSnakeCase
With camelCase:
function processOrder($orderId, $userData) { // ✓ Valid function processOrder($order_id, $user_data) { // ✗ Error: NotCamelCase
Excludes:
- Parameters inherited from interfaces/parent classes
- Parameters in interface/abstract method declarations
- Class properties (including promoted constructor properties)
Error codes
DrevOps.NamingConventions.ParameterNaming.NotSnakeCase(whenformat="snakeCase")DrevOps.NamingConventions.ParameterNaming.NotCamelCase(whenformat="camelCase")
Ignore
// phpcs:ignore DrevOps.NamingConventions.ParameterNaming.NotSnakeCase function process($legacyParam) {}
DataProviderPrefix
Enforces consistent naming prefix for PHPUnit data provider methods.
class MyTest extends TestCase { /** * @dataProvider dataProviderUserLogin */ public function testUserLogin($data) {} public function dataProviderUserLogin() { // ✓ Valid return []; } public function providerUserLogin() { // ✗ Error: InvalidPrefix return []; } }
Configuration
Customize the required prefix:
<rule ref="DrevOps.TestingPractices.DataProviderPrefix"> <properties> <property name="prefix" value="dataProvider"/> </properties> </rule>
Error code
DrevOps.TestingPractices.DataProviderPrefix.InvalidPrefix
Ignore
// phpcs:ignore DrevOps.TestingPractices.DataProviderPrefix.InvalidPrefix public function providerCustom() {}
Auto-fixing
This sniff supports auto-fixing with phpcbf:
- Renames provider methods to use the correct prefix
- Updates all
@dataProviderannotations to reference the new name
DataProviderMatchesTestName
Ensures data provider method names match their test method names.
class MyTest extends TestCase { /** * @dataProvider dataProviderUserLogin */ public function testUserLogin($data) {} public function dataProviderUserLogin() { // ✓ Valid - ends with "UserLogin" return []; } public function dataProviderLogin() { // ✗ Error: InvalidProviderName return []; // Expected: ends with "UserLogin" } }
Supported formats:
@dataProviderannotations#[DataProvider('methodName')]attributes (PHP 8+)
Excludes:
- External providers (
ClassName::methodName) - Non-test methods
- Non-test classes
Error code
DrevOps.TestingPractices.DataProviderMatchesTestName.InvalidProviderName
Ignore
// phpcs:ignore DrevOps.TestingPractices.DataProviderMatchesTestName.InvalidProviderName public function dataProviderCustomName() {}
DataProviderOrder
Enforces structural organization of test and data provider methods.
class MyTest extends TestCase { // ✓ Valid - provider after test (default) /** * @dataProvider dataProviderUserLogin */ public function testUserLogin($data) {} public function dataProviderUserLogin() { return []; } }
Helper methods between tests and providers are allowed:
class MyTest extends TestCase { /** * @dataProvider dataProviderUserLogin */ public function testUserLogin($data) {} private function helperMethod() {} // ✓ Allowed public function dataProviderUserLogin() { return []; } }
Configuration
Reverse the ordering (provider before test):
<rule ref="DrevOps.TestingPractices.DataProviderOrder"> <properties> <property name="providerPosition" value="before"/> </properties> </rule>
Options:
after(default) - Providers must appear after their test methodsbefore- Providers must appear before their test methods
Error codes
DrevOps.TestingPractices.DataProviderOrder.ProviderBeforeTest- Provider appears before test (whenproviderPosition="after")DrevOps.TestingPractices.DataProviderOrder.ProviderAfterTest- Provider appears after test (whenproviderPosition="before")
Ignore
// phpcs:ignore DrevOps.TestingPractices.DataProviderOrder.ProviderBeforeTest public function dataProviderUserLogin() {}
Development
composer install # Install dependencies composer test # Run tests composer test-coverage # Run tests with coverage composer lint # Check code standards composer lint-fix # Fix code standards
License
GPL-3.0-or-later
This repository was created using the Scaffold project template
drevops/phpcs-standard 适用场景与选型建议
drevops/phpcs-standard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 102.17k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 「standards」 「camelcase」 「snakecase」 「phpcs」 「PHPCodeSniffer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 drevops/phpcs-standard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drevops/phpcs-standard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 drevops/phpcs-standard 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP_CodeSniffer custom Sniff for Laravel coding standard
HiDev plugin for PHPUnit
A set of abstractions that define core services provided by the DVSA
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
This is the ZooRoyal coding standard.
统计信息
- 总下载量: 102.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 44
- 依赖项目数: 20
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2025-11-01
