justcoded / php-cs-fixer
Preconfigured ECS package wrapper with set of recommended rules. Allows code checking/fixing programmatically.
Requires
- php: >=8.2
- friendsofphp/php-cs-fixer: ^3.72
- kubawerlos/php-cs-fixer-custom-fixers: ^3.23
- slevomat/coding-standard: ^8.16
- squizlabs/php_codesniffer: ^3.12
- symfony/process: ^7.2
- symplify/coding-standard: ^12.2
- symplify/easy-coding-standard: ^12.5
Requires (Dev)
- pestphp/pest: ^3.7.4
README
A preconfigured Easy Coding Standard (ECS) wrapper with a set of recommended rules.
Provides an easy way to check and fix PHP code style both via CLI and programmatically.
- โ Framework-agnostic
- โ Built-in Laravel integration
- โ Pre-configured with popular coding standards
- โ Programmatic interface to check & fix code
๐ฆ Installation
composer require justcoded/php-cs-fixer
for only CLI usage you could install as dev
dependency
composer require justcoded/php-cs-fixer --dev
๐ Usage
CLI Usage
After installation, the package exposes the ECS binary:
# Check code style: ./vendor/bin/ecs check # Automatically fix code style issues: ./vendor/bin/ecs check --fix
You can also specify paths:
./vendor/bin/ecs check path/to/your/files
Programmatic Usage
The package provides the JustCoded\PhpCsFixer\Services\CodeFixer
class to run checks and fixes within your PHP code.
Constructor
new CodeFixer(array $config = [])
Available Config Options:
Option | Type | Default | Description |
---|---|---|---|
tty |
bool | null | false | Whether to enable TTY mode in the process. Useful for colored output. Uses Process::isTtySupported() if value set to null |
Basic Example:
use JustCoded\PhpCsFixer\Services\CodeFixer; $fixer = new CodeFixer(); // Check mode $result = $fixer->check(); if (! $result->successful) { echo "Code issues found:\n"; echo $result->output; } // Fix mode $result = $fixer->fix(); echo $result->output;
Custom Paths & Config:
use JustCoded\PhpCsFixer\Services\CodeFixer; $fixer = new CodeFixer(config: ['tty' => true]); // Check a specific path $result = $fixer->check(path: ['app/', 'tests/']); // Fix specific path with TTY disabled $result = $fixer->fix(path: 'src/', config: ['tty' => false]);
Advanced Usage - Direct execute()
:
You can also use the more flexible execute()
method:
use JustCoded\PhpCsFixer\Services\CodeFixer; $fixer = new CodeFixer(); $result = $fixer->execute( path: 'src/', fix: true, config: ['tty' => false], callback: function ($type, $buffer) { echo $buffer; // Stream process output }, ); echo $result->output;
Result Object:
All methods return a CodeFixerResult
:
class CodeFixerResult { public bool $successful; public string $output; }
โ๏ธ Laravel Integration
When used in a Laravel project, this package provides seamless integration out of the box.
Features:
-
Service Provider Auto-Discovery
No manual registration is needed. The package auto-registers:JustCoded\PhpCsFixer\Providers\PhpCsFixerServiceProvider
-
Configuration File
After installation, the package publishes a configuration file:php artisan vendor:publish --tag=php-cs-fixer-config
This creates
config/php-cs-fixer.php
containing:return [ /* | If null Symfony\Component\Process\Process::isTtySupported() is used. | You probably want it to be false to be able to capture the output. */ 'tty' => env('PHP_CS_FIXER_TTY', false), ];
You can adjust the default
tty
behavior by changing this config or setting thePHP_CS_FIXER_TTY
environment variable. -
Service Container Binding
The package registers theCodeFixer
class as a scoped singleton within Laravelโs service container.You can easily inject it wherever needed:
use JustCoded\PhpCsFixer\Services\CodeFixer; public function __construct( protected CodeFixer $fixer, ) {} public function run() { $result = $this->fixer->check(); echo $result->output; }
Or resolve it manually:
$fixer = app(JustCoded\PhpCsFixer\Services\CodeFixer::class);
๐ Configured Standards
The package includes:
- FriendsofPHP/php-cs-fixer
- Symplify Easy Coding Standard
- Slevomat Coding Standard
- PHP_CodeSniffer
- Custom fixers from kubawerlos/php-cs-fixer-custom-fixers
It ships pre-configured with best-practice rules.
However, you can override the configuration by placing your own ecs.php
in the project root.
๐ ๏ธ Custom ecs.php
Configuration
By default, PhpCsFixer comes with a preconfigured ecs.php
configuration file located inside the package. However, you can easily override this configuration by placing your own ecs.php
file at the root of your project.
How it works:
- If a
ecs.php
file exists in your project's root directory, it will be automatically used when runningcheck
orfix
commands (both CLI and programmatic usage). - If no custom config is found, the package's default configuration will be applied.
Example: Providing Custom ecs.php
- Create a file in your project root:
// ecs.php <?php declare(strict_types=1); use Symplify\EasyCodingStandard\Config\ECSConfig; use Symplify\EasyCodingStandard\ValueObject\Option; return static function (ECSConfig $ecsConfig): void { $ecsConfig->paths([ __DIR__ . '/app', __DIR__ . '/src', __DIR__ . '/tests', ]); $ecsConfig->sets([ \Symplify\EasyCodingStandard\ValueObject\Set\SetList::PSR_12, ]); };
- Run:
./vendor/bin/ecs check
Your custom rules and paths will now be applied.
Note:
When using Laravel integration, the same behavior applies. You can manage your own ecs.php
file without needing to modify the package. The CodeFixer
service will detect and use your config automatically.
๐งช Testing
composer test
๐ License
This package is open-sourced software licensed under the MIT license.