A custom PHP CS Fixer rule that ensures case statement bodies start on a new line, improving code readability and consistency with PSR-12 coding standards.
The Case Body New Line Fixer is a PHP CS Fixer plugin that automatically formats switch case and default statements by moving the statement body to a new line with proper indentation.
This fixer ensures that your code follows clean coding practices where case bodies are clearly separated from their labels, making the code more readable and maintainable.
- Detects
caseanddefaultstatements with code on the same line - Moves the code body to a new line
- Maintains proper indentation relative to the switch structure
- Leaves already-formatted code unchanged (idempotent)
- Works with nested switches and multiple cases
The fixer uses PHP CS Fixer's tokenizer system to:
- Analyze Switch Structures: Uses
ControlCaseStructuresAnalyzerto find all switch statements and their cases - Detect Same-Line Code: Checks if any meaningful code appears on the same line as the case/default colon
- Calculate Indentation: Determines the proper indentation by analyzing the current case line's indentation and adding 4 spaces
- Insert Newline: Replaces or inserts a newline token with the calculated indentation
- Preserve Comments: Skips cases where only comments follow the colon
Before:
<?php
switch ($status) {
case 'active': echo "Active";
break;
case 'inactive': $inactive = true;
break;
default: return null;
}After:
<?php
switch ($status) {
case 'active':
echo "Active";
break;
case 'inactive':
$inactive = true;
break;
default:
return null;
}- PHP 8.0 or higher
- PHP CS Fixer 3.94 or higher
Install the package via Composer:
composer require --dev orbeji/case-body-new-line-fixer- Clone or download this repository
- Add it to your project's
vendordirectory - Ensure your autoloader is configured correctly
Add the fixer to your .php-cs-fixer.dist.php configuration file:
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor');
return (new PhpCsFixer\Config())
->setFinder($finder)
->registerCustomFixers([
new Orbeji\Fixer\CaseBody\CaseBodyNewLineFixer(),
])
->setRules([
'@PSR12' => true,
'Orbeji/case_body_new_line' => true,
]);Run PHP CS Fixer as usual:
vendor/bin/php-cs-fixer fixOr for a specific file:
vendor/bin/php-cs-fixer fix path/to/file.phpRun the included test suite:
vendor/bin/phpunit tests/Or for a specific test:
vendor/bin/phpunit tests/Fixer/CaseBody/CaseBodyNewLineFixerTest.phpThe test suite includes:
- Simple case statements with code on the same line
- Multiple cases with mixed formatting
- Default statements with code on the same line
- Cases already formatted correctly (idempotency check)
- ✅ Idempotent: Running the fixer multiple times produces the same result
- ✅ Preserves Indentation: Correctly maintains and extends indentation
- ✅ Comment Safe: Doesn't interfere with comment-only lines
- ✅ Nested Switch Support: Works correctly with nested switch statements
- ✅ Non-Risky: Safe to use without risk of breaking code functionality
The fixer implements the FixerInterface from PHP CS Fixer and uses:
- ControlCaseStructuresAnalyzer: To analyze switch structures and locate cases/defaults
- Tokens API: To manipulate the token stream and insert newlines
- Whitespace Analysis: To detect and preserve proper indentation
getDefinition(): Returns fixer metadata and examplesisCandidate(): Checks if a file contains switch statementsfix(): Main entry point that applies the fixersupports(): Confirms the fixer works with PHP filesfixCase(): Core logic for fixing individual case statements
Contributions are welcome! Please follow PSR-12 coding standards and include tests for any new functionality.
This project is licensed under the MIT License. See the LICENSE file for details.
Created by orbeji