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-fixerfor only CLI usage you could install as dev dependency
composer require justcoded/php-cs-fixer --devAfter installation, the package exposes the ECS binary:
# Check code style:
./vendor/bin/ecs check
# Automatically fix code style issues:
./vendor/bin/ecs check --fixYou can also specify paths:
./vendor/bin/ecs check path/to/your/filesThe 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.phpcontaining: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
ttybehavior by changing this config or setting thePHP_CS_FIXER_TTYenvironment variable. -
Service Container Binding
The package registers theCodeFixerclass 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 or specify ecs_path config param.
How it works:
- If a
ecs.phpfile exists in specifiedecs_pathit will be automatically used. - If a
ecs.phpfile exists in your project's root directory, it will be automatically used when runningcheckorfixcommands (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 checkYour 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 testThis package is open-sourced software licensed under the MIT license.