Skip to content

orbeji/CaseBodyNewLineFixer

Repository files navigation

Case Body New Line Fixer

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.

Description

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.

What It Does

  • Detects case and default statements 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

How It Works

The fixer uses PHP CS Fixer's tokenizer system to:

  1. Analyze Switch Structures: Uses ControlCaseStructuresAnalyzer to find all switch statements and their cases
  2. Detect Same-Line Code: Checks if any meaningful code appears on the same line as the case/default colon
  3. Calculate Indentation: Determines the proper indentation by analyzing the current case line's indentation and adding 4 spaces
  4. Insert Newline: Replaces or inserts a newline token with the calculated indentation
  5. Preserve Comments: Skips cases where only comments follow the colon

Example

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;
}

Installation

Requirements

  • PHP 8.0 or higher
  • PHP CS Fixer 3.94 or higher

Via Composer

Install the package via Composer:

composer require --dev orbeji/case-body-new-line-fixer

Manual Installation

  1. Clone or download this repository
  2. Add it to your project's vendor directory
  3. Ensure your autoloader is configured correctly

Usage

Configuration

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,
    ]);

Running the Fixer

Run PHP CS Fixer as usual:

vendor/bin/php-cs-fixer fix

Or for a specific file:

vendor/bin/php-cs-fixer fix path/to/file.php

Testing

Run the included test suite:

vendor/bin/phpunit tests/

Or for a specific test:

vendor/bin/phpunit tests/Fixer/CaseBody/CaseBodyNewLineFixerTest.php

The 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)

Features

  • 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

Implementation Details

Architecture

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

Key Methods

  • getDefinition(): Returns fixer metadata and examples
  • isCandidate(): Checks if a file contains switch statements
  • fix(): Main entry point that applies the fixer
  • supports(): Confirms the fixer works with PHP files
  • fixCase(): Core logic for fixing individual case statements

Contributing

Contributions are welcome! Please follow PSR-12 coding standards and include tests for any new functionality.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Created by orbeji

See Also

About

php-cs-fixer fixer to enforce a new line after a switch case

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages