← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

PHP-CS-Fixer vs PHP_CodeSniffer

style PHP 7.1+ Beginner

Also Known As

PHP-CS-Fixer PHP_CodeSniffer phpcs phpcbf code style

TL;DR

Two PHP code style tools: PHP_CodeSniffer detects and optionally fixes violations, PHP-CS-Fixer auto-fixes code to match a configured style — both enforce consistent formatting across a codebase.

Explanation

PHP_CodeSniffer (phpcs/phpcbf): detects violations with phpcs, fixes them with phpcbf. Supports PSR-1/2/12, custom sniffs, ignoring specific rules. PHP-CS-Fixer: declarative configuration, auto-fixes everything — designed for full automation. Typically: PHP-CS-Fixer for auto-formatting (run in CI --dry-run, run locally to fix), PHPStan/Psalm for static analysis, PHP_CodeSniffer for project-specific custom sniffs. Configure once in .php-cs-fixer.dist.php committed to the repo.

Common Misconception

Code style enforcement is optional for small teams — inconsistent style increases cognitive load during code review and causes noisy diffs; automation eliminates the debate entirely.

Why It Matters

Automated style enforcement removes all style debates from code review — reviewers focus on logic and architecture, not indentation and bracket placement.

Common Mistakes

  • Running PHP-CS-Fixer with --fix in CI — CI should check (--dry-run), not auto-fix; fixes go in a local pre-commit hook.
  • Not committing .php-cs-fixer.dist.php — team members use different configs, creating style inconsistencies.
  • Running on the whole codebase at once when starting — fix incrementally with --path-mode=intersection to avoid massive diffs.
  • Not using Prettier for JS alongside PHP-CS-Fixer for PHP — style tools should cover all file types in the project.

Code Examples

✗ Vulnerable
// No style enforcement — review noise:
// PR comments:
// 'Please add spaces around operators'
// 'Missing blank line before return'
// 'Use single quotes for strings'
// 'Closing brace should be on its own line'
// 10 style comments on every PR — reviewer time wasted
✓ Fixed
# .php-cs-fixer.dist.php:
<?php
$finder = PhpCsFixer\Finder::create()->in(__DIR__ . '/src');
return (new PhpCsFixer\Config())
    ->setRules([
        '@PSR12' => true,
        '@PHP82Migration' => true,
        'array_syntax' => ['syntax' => 'short'],
        'ordered_imports' => ['sort_algorithm' => 'alpha'],
    ])
    ->setFinder($finder);

# CI check:
php vendor/bin/php-cs-fixer fix --dry-run --diff

# Pre-commit hook: auto-fix locally
php vendor/bin/php-cs-fixer fix

Added 15 Mar 2026
Edited 22 Mar 2026
Views 28
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 1 ping T 0 pings W 0 pings T 2 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 7 Google 4 SEMrush 3 Unknown AI 2 Ahrefs 2
crawler 22 crawler_json 3
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Add a .php-cs-fixer.php config targeting PSR-12, run in CI with --dry-run --diff to catch violations, and in pre-commit hooks to auto-fix before committing
📦 Applies To
PHP 7.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Inconsistent PHP code style; mixed tabs/spaces; no .php-cs-fixer.php config in project
Auto-detectable: ✓ Yes php-cs-fixer phpcs rector
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Low ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant