PHPackages                             justcoded/php-cs-fixer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. justcoded/php-cs-fixer

ActiveLibrary[Testing &amp; Quality](/categories/testing)

justcoded/php-cs-fixer
======================

Preconfigured ECS package wrapper with set of recommended rules. Allows code checking/fixing programmatically.

v0.1.2(10mo ago)21.9k↓44.4%MITPHPPHP &gt;=8.2CI passing

Since Mar 19Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/justcoded/php-cs-fixer)[ Packagist](https://packagist.org/packages/justcoded/php-cs-fixer)[ RSS](/packages/justcoded-php-cs-fixer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (8)Versions (4)Used By (0)

JustCoded PHP-CS-Fixer
======================

[](#justcoded-php-cs-fixer)

 [ ![Latest Stable Version](https://camo.githubusercontent.com/ab08d73540d8fedd14863b5999c17960d100291621841edcc415833adaf43b75/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a757374636f6465642f7068702d63732d66697865722e7376673f7374796c653d666c61742d726f756e646564) ](https://packagist.org/packages/justcoded/php-cs-fixer) [ ![Total Downloads](https://camo.githubusercontent.com/182ea907ddc5072444693c0555d91187d1424754a22e44dc5818ac062ef61fd4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a757374636f6465642f7068702d63732d66697865722e7376673f7374796c653d666c61742d726f756e646564) ](https://packagist.org/packages/justcoded/php-cs-fixer) [ ![License](https://camo.githubusercontent.com/3d45ec61811fc08fe600cdb888b1acd2af877d6ea3bc043c2e7ac3f1f09ac625/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d6173746572524f39342f6c61726176656c2d6d61696c2d766965776572) ](https://github.com/MasterRO94/laravel-mail-viewer/blob/master/LICENSE)

 [ ![StandWithUkraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg) ](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)

A preconfigured **[Easy Coding Standard](https://github.com/easy-coding-standard/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 &amp; fix code

---

📦 Installation
--------------

[](#-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
-------

[](#-usage)

### CLI 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

[](#programmatic-usage)

The package provides the `JustCoded\PhpCsFixer\Services\CodeFixer` class to run checks and fixes within your PHP code.

#### Constructor

[](#constructor)

```
new CodeFixer(array $config = [])
```

**Available Config Options:**

OptionTypeDefaultDescription`tty`bool | nullfalseWhether to enable TTY mode in the process. Useful for colored output. Uses `Process::isTtySupported()` if value set to null---

### Basic Example:

[](#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 &amp; Config:

[](#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()`:

[](#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:

[](#result-object)

All methods return a `CodeFixerResult`:

```
class CodeFixerResult
{
    public bool $successful;
    public string $output;
}
```

⚙️ Laravel Integration
----------------------

[](#️-laravel-integration)

When used in a Laravel project, this package provides **seamless integration** out of the box.

### Features:

[](#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 the `PHP_CS_FIXER_TTY` environment variable.
- **Service Container Binding**
    The package registers the `CodeFixer` 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
----------------------

[](#-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
---------------------------------

[](#️-custom-ecsphp-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 or specify `ecs_path` config param.

**How it works:**

- If a `ecs.php` file exists in specified `ecs_path` it will be automatically used.
- If a `ecs.php` file exists in your project's root directory, it will be automatically used when running `check` or `fix` 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`

[](#example-providing-custom-ecsphp)

1. Create a file in your project root:

```
// ecs.php
