Skip to content

Commit e0f9c01

Browse files
committed
feat: JC PHP CS Fixer
1 parent cdbecc5 commit e0f9c01

File tree

19 files changed

+7145
-1
lines changed

19 files changed

+7145
-1
lines changed

.github/workflows/tests.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [8.2, 8.3, 8.4]
17+
stability: [prefer-stable]
18+
19+
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v3
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php }}
29+
extensions: dom, curl, libxml, mbstring, zip
30+
coverage: none
31+
32+
- name: Install dependencies
33+
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
34+
35+
- name: Execute tests
36+
run: composer test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 JustCoded
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 299 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,299 @@
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

Comments
 (0)