Skip to content

PHP CodeSniffer Standard for snake_case variables

License

Notifications You must be signed in to change notification settings

drevops/phpcs-standard

Repository files navigation

DrevOps PHP_CodeSniffer Standard logo

DrevOps PHP_CodeSniffer Standard

GitHub IssuesGitHub Pull RequestsTest PHPcodecovGitHub release (latest by date)LICENSERenovate


PHP_CodeSniffer standard enforcing:

  • Consistent naming conventions for local variables and function/method parameters (configurable: snakeCase or camelCase)
  • 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"?> <rulesetname="Project Standards"> <ruleref="DrevOps"/> <file>src</file> <file>tests</file> </ruleset>

Use individual sniffs:

<rulesetname="Custom Standards"> <!-- Naming Conventions --> <ruleref="DrevOps.NamingConventions.LocalVariableNaming"/> <ruleref="DrevOps.NamingConventions.ParameterNaming"/> <!-- Testing Practices --> <ruleref="DrevOps.TestingPractices.DataProviderPrefix"/> <ruleref="DrevOps.TestingPractices.DataProviderMatchesTestName"/> <ruleref="DrevOps.TestingPractices.DataProviderOrder"/> </ruleset>

Configure naming convention

By default, both sniffs enforce snakeCase. Configure to use camelCase:

<rulesetname="Custom Standards"> <ruleref="DrevOps.NamingConventions.LocalVariableNaming"> <properties> <propertyname="format"value="camelCase"/> </properties> </rule> <ruleref="DrevOps.NamingConventions.ParameterNaming"> <properties> <propertyname="format"value="camelCase"/> </properties> </rule> </ruleset>

LocalVariableNaming

Enforces consistent naming convention for local variables inside functions/methods.

With snakeCase (default):

functionprocessOrder(){$order_id = 1; // ✓ Valid$orderId = 1; // ✗ Error: NotSnakeCase }

With camelCase:

functionprocessOrder(){$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 (when format="snakeCase")
  • DrevOps.NamingConventions.LocalVariableNaming.NotCamelCase (when format="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){// ✓ Validfunction processOrder($orderId, $userData){// ✗ Error: NotSnakeCase

With camelCase:

function processOrder($orderId, $userData){// ✓ Validfunction 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 (when format="snakeCase")
  • DrevOps.NamingConventions.ParameterNaming.NotCamelCase (when format="camelCase")

Ignore

// phpcs:ignore DrevOps.NamingConventions.ParameterNaming.NotSnakeCasefunctionprocess($legacyParam){}

DataProviderPrefix

Enforces consistent naming prefix for PHPUnit data provider methods.

class MyTest extends TestCase{/** * @dataProvider dataProviderUserLogin */publicfunctiontestUserLogin($data){} publicfunctiondataProviderUserLogin(){// ✓ Validreturn []} publicfunctionproviderUserLogin(){// ✗ Error: InvalidPrefixreturn []} }

Configuration

Customize the required prefix:

<ruleref="DrevOps.TestingPractices.DataProviderPrefix"> <properties> <propertyname="prefix"value="dataProvider"/> </properties> </rule>

Error code

DrevOps.TestingPractices.DataProviderPrefix.InvalidPrefix

Ignore

// phpcs:ignore DrevOps.TestingPractices.DataProviderPrefix.InvalidPrefixpublicfunctionproviderCustom(){}

Auto-fixing

This sniff supports auto-fixing with phpcbf:

  • Renames provider methods to use the correct prefix
  • Updates all @dataProvider annotations to reference the new name

DataProviderMatchesTestName

Ensures data provider method names match their test method names.

class MyTest extends TestCase{/** * @dataProvider dataProviderUserLogin */publicfunctiontestUserLogin($data){} publicfunctiondataProviderUserLogin(){// ✓ Valid - ends with "UserLogin"return []} publicfunctiondataProviderLogin(){// ✗ Error: InvalidProviderNamereturn []; // Expected: ends with "UserLogin" } }

Supported formats:

  • @dataProvider annotations
  • #[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.InvalidProviderNamepublicfunctiondataProviderCustomName(){}

DataProviderOrder

Enforces structural organization of test and data provider methods.

class MyTest extends TestCase{// ✓ Valid - provider after test (default)/** * @dataProvider dataProviderUserLogin */publicfunctiontestUserLogin($data){} publicfunctiondataProviderUserLogin(){return []} }

Helper methods between tests and providers are allowed:

class MyTest extends TestCase{/** * @dataProvider dataProviderUserLogin */publicfunctiontestUserLogin($data){} privatefunctionhelperMethod(){} // ✓ AllowedpublicfunctiondataProviderUserLogin(){return []} }

Configuration

Reverse the ordering (provider before test):

<ruleref="DrevOps.TestingPractices.DataProviderOrder"> <properties> <propertyname="providerPosition"value="before"/> </properties> </rule>

Options:

  • after (default) - Providers must appear after their test methods
  • before - Providers must appear before their test methods

Error codes

  • DrevOps.TestingPractices.DataProviderOrder.ProviderBeforeTest - Provider appears before test (when providerPosition="after")
  • DrevOps.TestingPractices.DataProviderOrder.ProviderAfterTest - Provider appears after test (when providerPosition="before")

Ignore

// phpcs:ignore DrevOps.TestingPractices.DataProviderOrder.ProviderBeforeTestpublicfunctiondataProviderUserLogin(){}

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

About

PHP CodeSniffer Standard for snake_case variables

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Languages