Skip to content

Commit 64ff353

Browse files
committed
Revert "Refactored case sniffs to allow choosing snake or camel case."
This reverts commit 6e7350e.
1 parent df40926 commit 64ff353

12 files changed

Lines changed: 154 additions & 660 deletions

CLAUDE.md

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44

55
## Project Overview
66

7-
This is a PHP_CodeSniffer (PHPCS) standard package (`drevops/phpcs-standard`) that enforces custom coding conventions, specifically focused on configurable naming (snakeCase or camelCase) for local variables and parameters.
7+
This is a PHP_CodeSniffer (PHPCS) standard package (`drevops/phpcs-standard`) that enforces custom coding conventions, specifically focused on snake_case naming for local variables and parameters.
88

99
**Key Goals:**
10-
- Enforce consistent naming conventions for local variables and function/method parameters
11-
- Support configurable formats: snakeCase (default) or camelCase
12-
- Exclude class properties from naming enforcement (properties follow different conventions)
10+
- Enforce snake_case for local variables and function/method parameters
11+
- Exclude class properties from snake_case enforcement (properties follow different conventions)
1312
- Preserve inherited parameter names from interfaces and parent classes
1413
- Provide auto-fixing support via `phpcbf`
1514
- Provide a standalone, reusable PHPCS standard for the DrevOps ecosystem
@@ -50,8 +49,8 @@ Coverage reports are generated in:
5049
Run a single test file:
5150
```bash
5251
./vendor/bin/phpunit tests/Unit/AbstractVariableSnakeCaseSniffTest.php
53-
./vendor/bin/phpunit tests/Unit/LocalVariableNamingSniffTest.php
54-
./vendor/bin/phpunit tests/Unit/ParameterNamingSniffTest.php
52+
./vendor/bin/phpunit tests/Unit/LocalVariableSnakeCaseSniffTest.php
53+
./vendor/bin/phpunit tests/Unit/ParameterSnakeCaseSniffTest.php
5554
```
5655

5756
Run only unit tests or functional tests:
@@ -87,15 +86,15 @@ composer normalize --dry-run
8786
### Directory Structure
8887
- `src/DrevOps/` - Source code for the PHPCS standard
8988
- `Sniffs/NamingConventions/`
90-
- `AbstractVariableNamingSniff.php` - Base class with shared functionality
91-
- `LocalVariableNamingSniff.php` - Enforces snake_case for local variables
92-
- `ParameterNamingSniff.php` - Enforces snake_case for parameters
89+
- `AbstractSnakeCaseSniff.php` - Base class with shared functionality
90+
- `LocalVariableSnakeCaseSniff.php` - Enforces snake_case for local variables
91+
- `ParameterSnakeCaseSniff.php` - Enforces snake_case for parameters
9392
- `ruleset.xml` - DrevOps standard definition
9493
- `tests/` - PHPUnit tests organized by type:
9594
- `Unit/` - Unit tests for individual sniff methods (using reflection)
9695
- `AbstractVariableSnakeCaseSniffTest.php` - Tests shared base class methods
97-
- `LocalVariableNamingSniffTest.php` - Tests local variable sniff
98-
- `ParameterNamingSniffTest.php` - Tests parameter sniff
96+
- `LocalVariableSnakeCaseSniffTest.php` - Tests local variable sniff
97+
- `ParameterSnakeCaseSniffTest.php` - Tests parameter sniff
9998
- `UnitTestCase.php` - Base test class with helper methods
10099
- `Functional/` - Integration tests that run actual phpcs commands
101100
- `Fixtures/` - Test fixture files with intentional violations
@@ -106,22 +105,15 @@ composer normalize --dry-run
106105

107106
The standard uses an **abstract base class pattern** with two concrete implementations:
108107

109-
#### AbstractVariableNamingSniff
108+
#### AbstractSnakeCaseSniff
110109

111-
Base class (src/DrevOps/Sniffs/NamingConventions/AbstractVariableNamingSniff.php) containing shared functionality:
112-
113-
**Public property:**
114-
- `$format` - Configurable naming convention ('snakeCase' or 'camelCase', default: 'snakeCase')
110+
Base class (src/DrevOps/Sniffs/NamingConventions/AbstractSnakeCaseSniff.php) containing shared functionality:
115111

116112
**Core methods:**
117113
- `register()` - Registers T_VARIABLE token for processing
118114
- `isReserved()` - Identifies PHP reserved variables ($this, $_GET, etc.)
119115
- `isSnakeCase()` - Validates snake_case format using regex
120-
- `isCamelCase()` - Validates camelCase format using regex
121-
- `isValidFormat()` - Validates variable name against configured format
122-
- `toSnakeCase()` - Converts variable name to snake_case
123-
- `toCamelCase()` - Converts variable name to camelCase
124-
- `toFormat()` - Converts variable name to the configured format
116+
- `toSnakeCase()` - Converts camelCase to snake_case for suggestions
125117

126118
**Helper methods:**
127119
- `getParameterNames()` - Extracts parameter names from function signature
@@ -132,33 +124,29 @@ Base class (src/DrevOps/Sniffs/NamingConventions/AbstractVariableNamingSniff.php
132124
- `isProperty()` - Distinguishes class properties from local variables
133125
- `isInheritedParameter()` - Detects parameters from interfaces/parent classes
134126

135-
#### LocalVariableNamingSniff
127+
#### LocalVariableSnakeCaseSniff
136128

137-
Enforces configurable naming convention for **local variables** inside functions/methods.
129+
Enforces snake_case for **local variables** inside functions/methods.
138130

139131
**What gets checked:**
140132
- ✅ Local variables inside function/method bodies
141-
- ❌ Function/method parameters (handled by ParameterNaming)
133+
- ❌ Function/method parameters (handled by ParameterSnakeCase)
142134
- ❌ Class properties (not enforced)
143135
- ❌ Reserved PHP variables ($this, superglobals, etc.)
144136

145-
**Error codes:**
146-
- `DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase` (when format='snakeCase')
147-
- `DrevOps.NamingConventions.LocalVariableNaming.NotCamelCase` (when format='camelCase')
137+
**Error code:** `DrevOps.NamingConventions.LocalVariableSnakeCase.NotSnakeCase`
148138

149-
#### ParameterNamingSniff
139+
#### ParameterSnakeCaseSniff
150140

151-
Enforces configurable naming convention for **function/method parameters**.
141+
Enforces snake_case for **function/method parameters**.
152142

153143
**What gets checked:**
154144
- ✅ Function and method parameters (in signature only)
155-
- ❌ Local variables (handled by LocalVariableNaming)
145+
- ❌ Local variables (handled by LocalVariableSnakeCase)
156146
- ❌ Parameters inherited from interfaces/parent classes/abstract methods
157147
- ❌ Promoted constructor properties
158148

159-
**Error codes:**
160-
- `DrevOps.NamingConventions.ParameterNaming.NotSnakeCase` (when format='snakeCase')
161-
- `DrevOps.NamingConventions.ParameterNaming.NotCamelCase` (when format='camelCase')
149+
**Error code:** `DrevOps.NamingConventions.ParameterSnakeCase.NotSnakeCase`
162150

163151
### PHPCS Standard Registration
164152

@@ -181,16 +169,16 @@ Tests are organized by class hierarchy:
181169
**AbstractVariableSnakeCaseSniffTest.php**
182170
- Tests all shared base class methods using reflection
183171
- Tests: `isSnakeCase()`, `toSnakeCase()`, `isReserved()`, `register()`, `getParameterNames()`, `isProperty()`, `isPromotedProperty()`, `isInheritedParameter()`
184-
- Each test uses concrete sniff instances (LocalVariableNamingSniff or ParameterNamingSniff) to access protected methods
172+
- Each test uses concrete sniff instances (LocalVariableSnakeCaseSniff or ParameterSnakeCaseSniff) to access protected methods
185173

186-
**LocalVariableNamingSniffTest.php**
174+
**LocalVariableSnakeCaseSniffTest.php**
187175
- Tests sniff-specific logic: error code constant and `process()` method
188-
- Configured to run only LocalVariableNaming sniff in isolation
176+
- Configured to run only LocalVariableSnakeCase sniff in isolation
189177
- Validates that local variables are checked and parameters are skipped
190178

191-
**ParameterNamingSniffTest.php**
179+
**ParameterSnakeCaseSniffTest.php**
192180
- Tests sniff-specific logic: error code constant, `register()`, and `process()` method
193-
- Configured to run only ParameterNaming sniff in isolation
181+
- Configured to run only ParameterSnakeCase sniff in isolation
194182
- Validates that parameters are checked and local variables are skipped
195183
- Includes tests for inherited parameter detection
196184

@@ -202,15 +190,15 @@ Tests are organized by class hierarchy:
202190

203191
#### 2. Functional Tests
204192

205-
**LocalVariableNamingSniffFunctionalTest.php**
193+
**LocalVariableSnakeCaseSniffFunctionalTest.php**
206194
- Run actual `phpcs` commands as external processes
207195
- Test complete PHPCS integration with JSON output parsing
208-
- Verify LocalVariableNaming sniff detection and error codes
196+
- Verify LocalVariableSnakeCase sniff detection and error codes
209197

210-
**ParameterNamingSniffFunctionalTest.php**
198+
**ParameterSnakeCaseSniffFunctionalTest.php**
211199
- Run actual `phpcs` commands as external processes
212200
- Test complete PHPCS integration with JSON output parsing
213-
- Verify ParameterNaming sniff detection and error codes
201+
- Verify ParameterSnakeCase sniff detection and error codes
214202

215203
Tests include:
216204
- Confirms violations are detected with correct error codes
@@ -251,7 +239,7 @@ When implementing or modifying sniffs:
251239

252240
1. Place sniff classes in `src/DrevOps/Sniffs/` following PHPCS naming conventions
253241
- Format: `CategoryName/SniffNameSniff.php`
254-
- Example: `NamingConventions/LocalVariableNamingSniff.php`
242+
- Example: `NamingConventions/LocalVariableSnakeCaseSniff.php`
255243
2. Consider using abstract base classes for shared functionality across related sniffs
256244
3. Implement the `Sniff` interface from `PHP_CodeSniffer\Sniffs\Sniff`
257245
4. Use `declare(strict_types=1);` at the top of all PHP files
@@ -266,10 +254,8 @@ When implementing or modifying sniffs:
266254
10. Create fixture files in `tests/Fixtures/` with intentional violations
267255
11. Follow error code naming: `StandardName.Category.SniffName.ErrorName`
268256
- Examples:
269-
- `DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase`
270-
- `DrevOps.NamingConventions.LocalVariableNaming.NotCamelCase`
271-
- `DrevOps.NamingConventions.ParameterNaming.NotSnakeCase`
272-
- `DrevOps.NamingConventions.ParameterNaming.NotCamelCase`
257+
- `DrevOps.NamingConventions.LocalVariableSnakeCase.NotSnakeCase`
258+
- `DrevOps.NamingConventions.ParameterSnakeCase.NotSnakeCase`
273259

274260
## CI/CD
275261

README.md

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
---
2121
PHP_CodeSniffer standard enforcing:
22-
- Consistent naming conventions for local variables and function/method parameters (configurable: `snakeCase` or `camelCase`)
22+
- `snake_case` naming for local variables and function/method parameters
2323
- PHPUnit data provider naming conventions and organization
2424

2525
## Installation
@@ -60,8 +60,8 @@ Use individual sniffs:
6060
```xml
6161
<ruleset name="Custom Standards">
6262
<!-- Naming Conventions -->
63-
<rule ref="DrevOps.NamingConventions.LocalVariableNaming"/>
64-
<rule ref="DrevOps.NamingConventions.ParameterNaming"/>
63+
<rule ref="DrevOps.NamingConventions.LocalVariableSnakeCase"/>
64+
<rule ref="DrevOps.NamingConventions.ParameterSnakeCase"/>
6565

6666
<!-- Testing Practices -->
6767
<rule ref="DrevOps.TestingPractices.DataProviderPrefix"/>
@@ -70,93 +70,55 @@ Use individual sniffs:
7070
</ruleset>
7171
```
7272

73-
### Configure naming convention
73+
## `LocalVariableSnakeCase`
7474

75-
By default, both sniffs enforce `snakeCase`. Configure to use `camelCase`:
75+
Enforces `snake_case` for local variables inside functions/methods.
7676

77-
```xml
78-
<ruleset name="Custom Standards">
79-
<rule ref="DrevOps.NamingConventions.LocalVariableNaming">
80-
<properties>
81-
<property name="format" value="camelCase"/>
82-
</properties>
83-
</rule>
84-
85-
<rule ref="DrevOps.NamingConventions.ParameterNaming">
86-
<properties>
87-
<property name="format" value="camelCase"/>
88-
</properties>
89-
</rule>
90-
</ruleset>
91-
```
92-
93-
## `LocalVariableNaming`
94-
95-
Enforces consistent naming convention for local variables inside functions/methods.
96-
97-
**With `snakeCase` (default):**
9877
```php
9978
function processOrder() {
10079
$order_id = 1; // ✓ Valid
101-
$orderId = 1; // ✗ Error: NotSnakeCase
102-
}
103-
```
104-
105-
**With `camelCase`:**
106-
```php
107-
function processOrder() {
108-
$orderId = 1; // ✓ Valid
109-
$order_id = 1; // ✗ Error: NotCamelCase
80+
$orderId = 1; // ✗ Error: VariableNotSnakeCase
11081
}
11182
```
11283

11384
Excludes:
114-
- Function/method parameters (handled by `ParameterNaming`)
85+
- Function/method parameters (handled by `ParameterSnakeCase`)
11586
- Class properties (not enforced)
11687
- Reserved variables (`$this`, `$_GET`, `$_POST`, etc.)
11788

118-
### Error codes
89+
### Error code
11990

120-
- `DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase` (when `format="snakeCase"`)
121-
- `DrevOps.NamingConventions.LocalVariableNaming.NotCamelCase` (when `format="camelCase"`)
91+
`DrevOps.NamingConventions.LocalVariableSnakeCase.NotSnakeCase`
12292

12393
### Ignore
12494

12595
```php
126-
// phpcs:ignore DrevOps.NamingConventions.LocalVariableNaming.NotSnakeCase
96+
// phpcs:ignore DrevOps.NamingConventions.LocalVariableSnakeCase.NotSnakeCase
12797
$myVariable = 'value';
12898
```
12999

130-
## `ParameterNaming`
100+
## `ParameterSnakeCase`
131101

132-
Enforces consistent naming convention for function/method parameters.
102+
Enforces `snake_case` for function/method parameters.
133103

134-
**With `snakeCase` (default):**
135104
```php
136105
function processOrder($order_id, $user_data) { // ✓ Valid
137-
function processOrder($orderId, $userData) { // ✗ Error: NotSnakeCase
138-
```
139-
140-
**With `camelCase`:**
141-
```php
142-
function processOrder($orderId, $userData) { // ✓ Valid
143-
function processOrder($order_id, $user_data) { // ✗ Error: NotCamelCase
106+
function processOrder($orderId, $userData) { // ✗ Error: ParameterNotSnakeCase
144107
```
145108

146109
Excludes:
147110
- Parameters inherited from interfaces/parent classes
148111
- Parameters in interface/abstract method declarations
149112
- Class properties (including promoted constructor properties)
150113

151-
### Error codes
114+
### Error code
152115

153-
- `DrevOps.NamingConventions.ParameterNaming.NotSnakeCase` (when `format="snakeCase"`)
154-
- `DrevOps.NamingConventions.ParameterNaming.NotCamelCase` (when `format="camelCase"`)
116+
`DrevOps.NamingConventions.ParameterSnakeCase.NotSnakeCase`
155117

156118
### Ignore
157119

158120
```php
159-
// phpcs:ignore DrevOps.NamingConventions.ParameterNaming.NotSnakeCase
121+
// phpcs:ignore DrevOps.NamingConventions.ParameterSnakeCase.NotSnakeCase
160122
function process($legacyParam) {}
161123
```
162124

composer.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
{
22
"name": "drevops/phpcs-standard",
3-
"description": "DrevOps PHP_CodeSniffer rules: enforce consistent naming (snakeCase or camelCase) for variables and parameters, PHPUnit data provider conventions.",
3+
"description": "DrevOps PHP_CodeSniffer rules: enforce snake_case for variables and parameters.",
44
"license": "GPL-2.0-or-later",
55
"type": "phpcodesniffer-standard",
6-
"keywords": [
7-
"phpcs",
8-
"phpcodesniffer",
9-
"standards",
10-
"snakeCase",
11-
"camelCase",
12-
"naming-conventions",
13-
"code-quality",
14-
"phpunit",
15-
"data-providers"
16-
],
176
"authors": [
187
{
198
"name": "Alex Skrypnyk",

0 commit comments

Comments
 (0)