PHP_CodeSniffer standard enforcing:
- Consistent naming conventions for local variables and function/method parameters (configurable:
snakeCaseorcamelCase) - PHPUnit data provider naming conventions and organization
composer require --dev drevops/phpcs-standardThe standard is automatically registered via phpcodesniffer-composer-installer.
Verify: vendor/bin/phpcs -i (should list DrevOps)
# Check code
vendor/bin/phpcs --standard=DrevOps path/to/code
# Auto-fix
vendor/bin/phpcbf --standard=DrevOps path/to/codeCreate 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>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>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.)
DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase(whenformat="snakeCase")DrevOps.NamingConventions.LocalVariableNaming.NotCamelCase(whenformat="camelCase")
// phpcs:ignore DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase
$myVariable = 'value';Enforces consistent naming convention for function/method parameters.
With snakeCase (default):
function processOrder($order_id, $user_data) { // ✓ Valid
function processOrder($orderId, $userData) { // ✗ Error: NotSnakeCaseWith camelCase:
function processOrder($orderId, $userData) { // ✓ Valid
function processOrder($order_id, $user_data) { // ✗ Error: NotCamelCaseExcludes:
- Parameters inherited from interfaces/parent classes
- Parameters in interface/abstract method declarations
- Class properties (including promoted constructor properties)
DrevOps.NamingConventions.ParameterNaming.NotSnakeCase(whenformat="snakeCase")DrevOps.NamingConventions.ParameterNaming.NotCamelCase(whenformat="camelCase")
// phpcs:ignore DrevOps.NamingConventions.ParameterNaming.NotSnakeCase
function process($legacyParam) {}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 [];
}
}Customize the required prefix:
<rule ref="DrevOps.TestingPractices.DataProviderPrefix">
<properties>
<property name="prefix" value="dataProvider"/>
</properties>
</rule>DrevOps.TestingPractices.DataProviderPrefix.InvalidPrefix
// phpcs:ignore DrevOps.TestingPractices.DataProviderPrefix.InvalidPrefix
public function providerCustom() {}This sniff supports auto-fixing with phpcbf:
- Renames provider methods to use the correct prefix
- Updates all
@dataProviderannotations to reference the new name
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
DrevOps.TestingPractices.DataProviderMatchesTestName.InvalidProviderName
// phpcs:ignore DrevOps.TestingPractices.DataProviderMatchesTestName.InvalidProviderName
public function dataProviderCustomName() {}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 [];
}
}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
DrevOps.TestingPractices.DataProviderOrder.ProviderBeforeTest- Provider appears before test (whenproviderPosition="after")DrevOps.TestingPractices.DataProviderOrder.ProviderAfterTest- Provider appears after test (whenproviderPosition="before")
// phpcs:ignore DrevOps.TestingPractices.DataProviderOrder.ProviderBeforeTest
public function dataProviderUserLogin() {}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 standardsGPL-3.0-or-later
This repository was created using the Scaffold project template
