|
1 | | -# php-cs-fixer |
| 1 | +# JustCoded PHP-CS-Fixer |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://packagist.org/packages/justcoded/php-cs-fixer"> |
| 5 | + <img src="https://img.shields.io/packagist/v/justcoded/php-cs-fixer.svg?style=flat-rounded" alt="Latest Stable Version"> |
| 6 | + </a> |
| 7 | + <a href="https://packagist.org/packages/justcoded/php-cs-fixer"> |
| 8 | + <img src="https://img.shields.io/packagist/dt/justcoded/php-cs-fixer.svg?style=flat-rounded" alt="Total Downloads"> |
| 9 | + </a> |
| 10 | + <a href="https://github.com/MasterRO94/laravel-mail-viewer/blob/master/LICENSE"> |
| 11 | + <img src="https://img.shields.io/github/license/MasterRO94/laravel-mail-viewer" alt="License"> |
| 12 | + </a> |
| 13 | +</p> |
| 14 | + |
| 15 | +<p align="center"> |
| 16 | + <a href="https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md"> |
| 17 | + <img src="https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg" alt="StandWithUkraine"> |
| 18 | + </a> |
| 19 | +</p> |
| 20 | + |
| 21 | +A preconfigured **[Easy Coding Standard](https://github.com/easy-coding-standard/easy-coding-standard) (ECS)** wrapper with a set of recommended rules. |
| 22 | +Provides an easy way to check and fix PHP code style both via CLI and programmatically. |
| 23 | + |
| 24 | +- ✅ Framework-agnostic |
| 25 | +- ✅ Built-in Laravel integration |
| 26 | +- ✅ Pre-configured with popular coding standards |
| 27 | +- ✅ Programmatic interface to check & fix code |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 📦 Installation |
| 32 | + |
| 33 | +```bash |
| 34 | +composer require justcoded/php-cs-fixer |
| 35 | +``` |
| 36 | +for only CLI usage you could install as `dev` dependency |
| 37 | +```bash |
| 38 | +composer require justcoded/php-cs-fixer --dev |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 🚀 Usage |
| 44 | + |
| 45 | +### CLI Usage |
| 46 | + |
| 47 | +After installation, the package exposes the ECS binary: |
| 48 | + |
| 49 | +```bash |
| 50 | +# Check code style: |
| 51 | +./vendor/bin/ecs check |
| 52 | + |
| 53 | +# Automatically fix code style issues: |
| 54 | +./vendor/bin/ecs check --fix |
| 55 | +``` |
| 56 | + |
| 57 | +You can also specify paths: |
| 58 | + |
| 59 | +```bash |
| 60 | +./vendor/bin/ecs check path/to/your/files |
| 61 | +``` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### Programmatic Usage |
| 66 | + |
| 67 | +The package provides the `JustCoded\PhpCsFixer\Services\CodeFixer` class to run checks and fixes within your PHP code. |
| 68 | + |
| 69 | +#### Constructor |
| 70 | + |
| 71 | +```php |
| 72 | +new CodeFixer(array $config = []) |
| 73 | +``` |
| 74 | + |
| 75 | +**Available Config Options:** |
| 76 | + |
| 77 | +| Option | Type | Default | Description | |
| 78 | +|-------|--------------|---------|---------------------------------------------------------------------------------------------------------------------------| |
| 79 | +| `tty` | bool \| null | false | Whether to enable TTY mode in the process. Useful for colored output. Uses `Process::isTtySupported()` if value set to null | |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +### Basic Example: |
| 84 | + |
| 85 | +```php |
| 86 | +use JustCoded\PhpCsFixer\Services\CodeFixer; |
| 87 | + |
| 88 | +$fixer = new CodeFixer(); |
| 89 | + |
| 90 | +// Check mode |
| 91 | +$result = $fixer->check(); |
| 92 | + |
| 93 | +if (! $result->successful) { |
| 94 | + echo "Code issues found:\n"; |
| 95 | + echo $result->output; |
| 96 | +} |
| 97 | + |
| 98 | +// Fix mode |
| 99 | +$result = $fixer->fix(); |
| 100 | +echo $result->output; |
| 101 | +``` |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +### Custom Paths & Config: |
| 106 | + |
| 107 | +```php |
| 108 | +use JustCoded\PhpCsFixer\Services\CodeFixer; |
| 109 | + |
| 110 | +$fixer = new CodeFixer(config: ['tty' => true]); |
| 111 | + |
| 112 | +// Check a specific path |
| 113 | +$result = $fixer->check(path: ['app/', 'tests/']); |
| 114 | + |
| 115 | +// Fix specific path with TTY disabled |
| 116 | +$result = $fixer->fix(path: 'src/', config: ['tty' => false]); |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +### Advanced Usage - Direct `execute()`: |
| 122 | + |
| 123 | +You can also use the more flexible `execute()` method: |
| 124 | + |
| 125 | +```php |
| 126 | +use JustCoded\PhpCsFixer\Services\CodeFixer; |
| 127 | + |
| 128 | +$fixer = new CodeFixer(); |
| 129 | + |
| 130 | +$result = $fixer->execute( |
| 131 | + path: 'src/', |
| 132 | + fix: true, |
| 133 | + config: ['tty' => false], |
| 134 | + callback: function ($type, $buffer) { |
| 135 | + echo $buffer; // Stream process output |
| 136 | + }, |
| 137 | +); |
| 138 | + |
| 139 | +echo $result->output; |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +### Result Object: |
| 145 | + |
| 146 | +All methods return a `CodeFixerResult`: |
| 147 | + |
| 148 | +```php |
| 149 | +class CodeFixerResult |
| 150 | +{ |
| 151 | + public bool $successful; |
| 152 | + public string $output; |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## ⚙️ Laravel Integration |
| 157 | + |
| 158 | +When used in a Laravel project, this package provides **seamless integration** out of the box. |
| 159 | + |
| 160 | +### Features: |
| 161 | + |
| 162 | +- **Service Provider Auto-Discovery** |
| 163 | + No manual registration is needed. The package auto-registers: |
| 164 | + |
| 165 | + ```php |
| 166 | + JustCoded\PhpCsFixer\Providers\PhpCsFixerServiceProvider |
| 167 | + ``` |
| 168 | + |
| 169 | +- **Configuration File** |
| 170 | + After installation, the package publishes a configuration file: |
| 171 | + |
| 172 | + ```bash |
| 173 | + php artisan vendor:publish --tag=php-cs-fixer-config |
| 174 | + ``` |
| 175 | + |
| 176 | + This creates `config/php-cs-fixer.php` containing: |
| 177 | + |
| 178 | + ```php |
| 179 | + return [ |
| 180 | + /* |
| 181 | + | If null Symfony\Component\Process\Process::isTtySupported() is used. |
| 182 | + | You probably want it to be false to be able to capture the output. |
| 183 | + */ |
| 184 | + 'tty' => env('PHP_CS_FIXER_TTY', false), |
| 185 | + ]; |
| 186 | + ``` |
| 187 | + |
| 188 | + You can adjust the default `tty` behavior by changing this config or setting the `PHP_CS_FIXER_TTY` environment variable. |
| 189 | + |
| 190 | +- **Service Container Binding** |
| 191 | + The package registers the `CodeFixer` class as a **scoped singleton** within Laravel’s service container. |
| 192 | + |
| 193 | + You can easily inject it wherever needed: |
| 194 | + |
| 195 | + ```php |
| 196 | + use JustCoded\PhpCsFixer\Services\CodeFixer; |
| 197 | + |
| 198 | + public function __construct( |
| 199 | + protected CodeFixer $fixer, |
| 200 | + ) {} |
| 201 | + |
| 202 | + public function run() |
| 203 | + { |
| 204 | + $result = $this->fixer->check(); |
| 205 | + |
| 206 | + echo $result->output; |
| 207 | + } |
| 208 | + ``` |
| 209 | + |
| 210 | + Or resolve it manually: |
| 211 | + |
| 212 | + ```php |
| 213 | + $fixer = app(JustCoded\PhpCsFixer\Services\CodeFixer::class); |
| 214 | + ``` |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +## 📚 Configured Standards |
| 219 | + |
| 220 | +The package includes: |
| 221 | + |
| 222 | +- **FriendsofPHP/php-cs-fixer** |
| 223 | +- **Symplify Easy Coding Standard** |
| 224 | +- **Slevomat Coding Standard** |
| 225 | +- **PHP_CodeSniffer** |
| 226 | +- **Custom fixers from kubawerlos/php-cs-fixer-custom-fixers** |
| 227 | + |
| 228 | +It ships pre-configured with best-practice rules. |
| 229 | +However, you can override the configuration by placing your own `ecs.php` in the project root. |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +Here’s a clear and concise section you can add to the README to explain how users can provide a custom `ecs.php` config file: |
| 234 | + |
| 235 | +--- |
| 236 | + |
| 237 | +## 🛠️ Custom `ecs.php` Configuration |
| 238 | + |
| 239 | +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. |
| 240 | + |
| 241 | +**How it works:** |
| 242 | + |
| 243 | +- 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). |
| 244 | +- If no custom config is found, the package's default configuration will be applied. |
| 245 | + |
| 246 | +### Example: Providing Custom `ecs.php` |
| 247 | + |
| 248 | +1. Create a file in your project root: |
| 249 | + |
| 250 | +```php |
| 251 | +// ecs.php |
| 252 | +<?php |
| 253 | + |
| 254 | +declare(strict_types=1); |
| 255 | + |
| 256 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 257 | +use Symplify\EasyCodingStandard\ValueObject\Option; |
| 258 | + |
| 259 | +return static function (ECSConfig $ecsConfig): void { |
| 260 | + $ecsConfig->paths([ |
| 261 | + __DIR__ . '/app', |
| 262 | + __DIR__ . '/src', |
| 263 | + __DIR__ . '/tests', |
| 264 | + ]); |
| 265 | + |
| 266 | + $ecsConfig->sets([ |
| 267 | + \Symplify\EasyCodingStandard\ValueObject\Set\SetList::PSR_12, |
| 268 | + ]); |
| 269 | +}; |
| 270 | +``` |
| 271 | + |
| 272 | +2. Run: |
| 273 | + |
| 274 | +```bash |
| 275 | +./vendor/bin/ecs check |
| 276 | +``` |
| 277 | + |
| 278 | +Your custom rules and paths will now be applied. |
| 279 | + |
| 280 | +--- |
| 281 | + |
| 282 | +**Note:** |
| 283 | +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. |
| 284 | + |
| 285 | +--- |
| 286 | + |
| 287 | +## 🧪 Testing |
| 288 | + |
| 289 | +```bash |
| 290 | +composer test |
| 291 | +``` |
| 292 | + |
| 293 | +--- |
| 294 | + |
| 295 | +## 📝 License |
| 296 | + |
| 297 | +This package is open-sourced software licensed under the [MIT license](LICENSE). |
| 298 | + |
| 299 | +--- |
0 commit comments