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
composer require justcoded/php-cs-fixer
for only CLI usage you could install as dev
dependency
composer require justcoded/php-cs-fixer --dev
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
The package provides the JustCoded\PhpCsFixer\Services\CodeFixer
class to run checks and fixes within your PHP code.
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 |
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;
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]);
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;
All methods return a CodeFixerResult
:
class CodeFixerResult
{
public bool $successful;
public string $output;
}
When used in a Laravel project, this package provides seamless integration out of the box.
-
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);
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.
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.
- 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.
composer test
This package is open-sourced software licensed under the MIT license.