Skip to content

Commit eed4e53

Browse files
committed
Initial commit
0 parents  commit eed4e53

File tree

9 files changed

+336
-0
lines changed

9 files changed

+336
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Enforce Unix newlines
2+
* text=lf
3+
4+
# Exclude unused files (see: https://redd.it/2jzp6k)
5+
/.editorconfig export-ignore
6+
/.gitattributes export-ignore
7+
/.github export-ignore
8+
/.gitignore export-ignore

.github/workflows/ci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
3+
on: push
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
11+
include:
12+
- php-versions: 8.4
13+
PHP_CS_FIXER_IGNORE_ENV: 1
14+
name: PHP ${{ matrix.php-versions }}
15+
steps:
16+
- name: 📤 Checkout project
17+
uses: actions/checkout@v4
18+
19+
- name: 🐘 Install PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-versions }}
23+
24+
- name: 📦 Install dependencies
25+
run: composer update --no-progress --no-interaction --prefer-dist
26+
27+
- name: ✅ Run code style check
28+
env:
29+
PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }}
30+
run: composer check-linting

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.lock
2+
vendor/
3+
.php-cs-fixer.cache
4+
.php_cs.cache
5+
.idea

.php-cs-fixer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
return EsfahanAhan\PhpCsFixer\Config::fromFolders(['src']);

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<h1 align="center">
2+
<br>
3+
<img src="assets/logo.png" alt="EsfahanAhan" width="400">
4+
<br><br>
5+
EsfahanAhan PHP-CS-Fixer configuration
6+
<br>
7+
</h1>
8+
9+
<h4 align="center">
10+
11+
The default [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) configuration for EsfahanAhan projects.
12+
13+
</h4>
14+
15+
<div align="center">
16+
17+
![ci](https://github.com/esfahanahan/php-cs-fixer-config/actions/workflows/ci.yml/badge.svg)
18+
[![pull requests](https://img.shields.io/github/issues-pr/esfahanahan/php-cs-fixer-config)](https://github.com/esfahanahan/php-cs-fixer-config/pulls)
19+
[![contributors](https://img.shields.io/github/contributors/esfahanahan/php-cs-fixer-config)](https://github.com/esfahanahan/php-cs-fixer-config/graphs/contributors)
20+
21+
</div>
22+
23+
<div align="center">
24+
<sub>Built with :heart:︎ and :tea: by heroes at <a href="https://esfahanahan.com">EsfahanAhan</a>.</sub>
25+
</div>
26+
27+
## Installation
28+
29+
```bash
30+
composer require --dev esfahanahan/php-cs-fixer-config
31+
```
32+
33+
## Usage
34+
35+
### Basic usage
36+
37+
**`.php-cs-fixer.php`**
38+
39+
```php
40+
<?php
41+
42+
require 'vendor/autoload.php';
43+
44+
return EsfahanAhan\PhpCsFixer\Config::fromFolders(['src']);
45+
```
46+
47+
To exclude a subfolder:
48+
49+
```php
50+
<?php
51+
52+
require 'vendor/autoload.php';
53+
54+
return EsfahanAhan\PhpCsFixer\Config::fromFolders(['src'], null, ['ignoreThisDir']);
55+
```
56+
This will skip `src/ignoreThisDir` or `src/foo/bar/ignoreThisDir`. (_The folder has to be relative to the ones in the first argument._)
57+
58+
You can also override rules per-project without overriding the core rules like this:
59+
60+
**`.php-cs-fixer.php`**
61+
62+
```php
63+
<?php
64+
65+
require 'vendor/autoload.php';
66+
67+
return EsfahanAhan\PhpCsFixer\Config::fromFolders(['src'])->mergeRules([
68+
'php_unit_strict' => false,
69+
]);
70+
```
71+
72+
### Usage within a Laravel project
73+
74+
You can also preconfigure the configuration for a Laravel project by calling a special factory method:
75+
76+
**`.php-cs-fixer.php`**
77+
78+
```php
79+
<?php
80+
81+
require 'vendor/autoload.php';
82+
83+
return EsfahanAhan\PhpCsFixer\Config::forLaravel();
84+
```
85+
86+
If need be, you can also append folders to fix in addition to Laravel's:
87+
88+
**`.php-cs-fixer.php`**
89+
90+
```php
91+
<?php
92+
93+
require 'vendor/autoload.php';
94+
95+
return EsfahanAhan\PhpCsFixer\Config::forLaravel(['some_other_folder']);
96+
```
97+
98+
### Targeting a specific PHP version
99+
100+
By default, the configuration will check the version of PHP you run the tool with, and proceed to enable/disable fixers depending on it.
101+
102+
You can override the _target_ PHP version by passing it either as constructor argument, or as second argument to `fromFolders`:
103+
104+
**`.php-cs-fixer.php`**
105+
106+
```php
107+
<?php
108+
109+
require 'vendor/autoload.php';
110+
111+
return EsfahanAhan\PhpCsFixer\Config::fromFolders(['src'], '8.3');
112+
113+
// or
114+
115+
return EsfahanAhan\PhpCsFixer\Config::forLaravel([], '8.3');
116+
```

assets/logo.png

20.4 KB
Loading

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "esfahanahan/php-cs-fixer-config",
3+
"description": "Prebuilt PHP CS Fixer configuration for EsfahanAhan projects",
4+
"license": "MIT",
5+
"keywords": [
6+
"dev",
7+
"php-cs-fixer"
8+
],
9+
"authors": [
10+
{
11+
"role": "Owner",
12+
"name": "Esfahan Ahan Apadana",
13+
"email": "[email protected]",
14+
"homepage": "https://esfahanahan.com"
15+
},
16+
{
17+
"role": "Maintainer",
18+
"name": "Hossein Hosni",
19+
"email": "[email protected]",
20+
"homepage": "https://github.com/hosni"
21+
}
22+
],
23+
"require": {
24+
"php": "^7.4|^8.0",
25+
"friendsofphp/php-cs-fixer": "^3.87"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"EsfahanAhan\\PhpCsFixer\\": "src"
30+
}
31+
},
32+
"scripts": {
33+
"check-linting": "vendor/bin/php-cs-fixer fix --dry-run -vvv",
34+
"lint": "php-cs-fixer fix"
35+
}
36+
}

src/Config.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EsfahanAhan\PhpCsFixer;
6+
7+
use PhpCsFixer\ConfigInterface;
8+
use PhpCsFixer\Finder;
9+
10+
class Config extends \PhpCsFixer\Config
11+
{
12+
public static function defaultRules(): array
13+
{
14+
return [
15+
'@Symfony' => true,
16+
'declare_strict_types' => true,
17+
'modernize_types_casting' => true,
18+
'no_useless_return' => true,
19+
'ordered_class_elements' => [
20+
'order' => [
21+
'use_trait',
22+
'constant_public',
23+
'constant_protected',
24+
'constant_private',
25+
'property_public_static',
26+
'property_protected_static',
27+
'property_private_static',
28+
'method_public_static',
29+
'method_protected_static',
30+
'method_private_static',
31+
'property_public',
32+
'property_protected',
33+
'property_private',
34+
'case',
35+
'construct',
36+
'destruct',
37+
'magic',
38+
'method_public',
39+
'method_protected',
40+
'method_private',
41+
],
42+
],
43+
'php_unit_method_casing' => false,
44+
'protected_to_private' => true,
45+
'static_lambda' => true,
46+
'strict_param' => true,
47+
'single_quote' => true,
48+
'trailing_comma_in_multiline' => [
49+
'elements' => [
50+
'arrays',
51+
],
52+
],
53+
'use_arrow_functions' => true,
54+
'whitespace_after_comma_in_array' => true,
55+
];
56+
}
57+
58+
/**
59+
* @param string[] $folders
60+
* @param string[] $exclude folder to exclude
61+
*/
62+
public static function fromFolders(array $folders, ?string $target = null, array $exclude = []): ConfigInterface
63+
{
64+
$config = new static($target);
65+
66+
return $config->setFinder(
67+
Finder::create()->in($folders)->exclude($exclude)
68+
);
69+
}
70+
71+
/**
72+
* @param string[] $folders
73+
*/
74+
public static function forLaravel(array $folders = [], ?string $target = null): ConfigInterface
75+
{
76+
$folders = array_merge(['app', 'config', 'database', 'routes', 'tests'], $folders);
77+
78+
return static::fromFolders($folders, $target);
79+
}
80+
/**
81+
* The PHP version of the application.
82+
*/
83+
protected string $target;
84+
85+
/**
86+
* Create a new MWL configuration.
87+
*/
88+
public function __construct(?string $target = null)
89+
{
90+
parent::__construct('esfahanahan');
91+
92+
$this->target = $target ?: PHP_VERSION;
93+
94+
$this
95+
->setRiskyAllowed(true)
96+
->setRules(
97+
static::defaultRules()
98+
);
99+
}
100+
101+
/**
102+
* Merge a set of rules with the core ones.
103+
*/
104+
public function mergeRules(array $rules): ConfigInterface
105+
{
106+
return $this->setRules(array_merge(
107+
$this->getRules(),
108+
$rules
109+
));
110+
}
111+
112+
public function enablePhpunitRules(): ConfigInterface
113+
{
114+
return $this->mergeRules([
115+
'php_unit_dedicate_assert' => true,
116+
'php_unit_dedicate_assert_internal_type' => true,
117+
'php_unit_expectation' => true,
118+
'php_unit_internal_class' => true,
119+
'php_unit_mock' => true,
120+
'php_unit_namespaced' => true,
121+
'php_unit_no_expectation_annotation' => true,
122+
]);
123+
}
124+
}

0 commit comments

Comments
 (0)