check-code-style
Code Style Check
Analyze PHP code for style consistency and PSR-12 compliance.
Detection Patterns
1. PSR-12 Violations
// BAD: Opening brace on same line for class
class User {
}
// GOOD: Opening brace on new line
class User
{
}
// BAD: Opening brace on new line for methods
public function getName()
{
}
// GOOD: Opening brace on same line for control structures
if ($condition) {
}
// BAD: No space after keywords
if($condition) {}
foreach($items as $item) {}
// GOOD: Space after keywords
if ($condition) {}
foreach ($items as $item) {}
2. Line Length
// BAD: Exceeds 120 characters
$result = $this->veryLongServiceName->processWithManyParameters($parameter1, $parameter2, $parameter3, $parameter4, $parameter5);
// GOOD: Broken into multiple lines
$result = $this->veryLongServiceName->processWithManyParameters(
$parameter1,
$parameter2,
$parameter3,
$parameter4,
$parameter5
);
3. Indentation
// BAD: Mixed tabs and spaces
class User
{
private string $name; // Tab
private int $age; // Spaces
}
// GOOD: Consistent 4 spaces
class User
{
private string $name;
private int $age;
}
4. Blank Lines
// BAD: No blank line after namespace
namespace App\Service;
use App\Repository;
// GOOD: Blank line after namespace
namespace App\Service;
use App\Repository;
// BAD: No blank line between methods
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
// GOOD: Blank line between methods
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
5. Trailing Whitespace
// BAD: Trailing spaces at end of line
$name = 'John';
$age = 25;
// GOOD: No trailing whitespace
$name = 'John';
$age = 25;
6. Use Statement Ordering
// BAD: Unordered use statements
use App\Service\OrderService;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\UserRepository;
// GOOD: Grouped and alphabetized
use App\Entity\User;
use App\Repository\UserRepository;
use App\Service\OrderService;
use Symfony\Component\HttpFoundation\Request;
7. Array Syntax
// BAD: Long array syntax
$items = array(1, 2, 3);
$config = array(
'key' => 'value',
);
// GOOD: Short array syntax
$items = [1, 2, 3];
$config = [
'key' => 'value',
];
8. Operator Spacing
// BAD: Inconsistent spacing
$sum=$a+$b;
$name = $first.$last;
$condition = $a>$b;
// GOOD: Consistent spacing
$sum = $a + $b;
$name = $first . $last;
$condition = $a > $b;
9. Comma Spacing
// BAD: No space after comma
$result = calculate($a,$b,$c);
$array = [1,2,3];
// GOOD: Space after comma
$result = calculate($a, $b, $c);
$array = [1, 2, 3];
10. Return Type Declaration
// BAD: Space before colon
public function getName() : string {}
// GOOD: No space before colon
public function getName(): string {}
// BAD: No space after colon
public function getName():string {}
// GOOD: Space after colon
public function getName(): string {}
Grep Patterns
# Opening brace on same line for class
Grep: "class\s+\w+\s*\{" --glob "**/*.php"
# No space after if/foreach/while
Grep: "(if|foreach|while|for|switch)\(" --glob "**/*.php"
# Lines over 120 chars (needs manual check)
Grep: "^.{121,}$" --glob "**/*.php"
# array() instead of []
Grep: "array\s*\(" --glob "**/*.php"
# Missing space around operators
Grep: "[^ ]==[^ ]|[^ ]!=[^ ]" --glob "**/*.php"
Severity Classification
| Pattern | Severity |
|---|---|
| Mixed tabs/spaces | π Major |
| PSR-12 brace placement | π‘ Minor |
| Line length > 120 | π‘ Minor |
| Operator spacing | π’ Suggestion |
| Use statement order | π’ Suggestion |
Automated Fixing
Most style issues can be automatically fixed:
# PHP-CS-Fixer
php-cs-fixer fix --rules=@PSR12
# PHP_CodeSniffer
phpcbf --standard=PSR12 src/
Output Format
### Style Issue: [Description]
**Severity:** π /π‘/π’
**Location:** `file.php:line`
**Rule:** PSR-12 / Project Standard
**Issue:**
[Description of the style violation]
**Current:**
```php
if($condition){
Suggested:
if ($condition) {
Auto-fix:
Run php-cs-fixer fix --rules=@PSR12
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12