-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.php-cs-fixer.php
More file actions
270 lines (236 loc) · 8.08 KB
/
Copy path.php-cs-fixer.php
File metadata and controls
270 lines (236 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
declare(strict_types=1);
use PhpCsFixer\Finder;
use PhpCsFixer\Config;
use PhpCsFixer\Console\ConfigurationResolver;
/**
* Custom finder.
*/
final class CustomFinder extends Finder
{
/** @var array */
private $excludes = [
'vendor',
];
/** @var \PhpCsFixer\Console\ConfigurationResolver|null */
private $configurationResolver = null;
/** @var string */
private $input = '';
/** @var array */
private $files = [];
/** @var array */
private $directories = [];
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->initConfigurationResolver();
$this->initFiles();
$this->initDirectories();
$this->initFinder();
$this->outputInfo();
}
/**
* Initialize configuration resolver.
*
* The ConfigurationResolver is the class instantiating this .php_cs file, and thus instantiating this CustomFinder.
* In order to be able to see if a file/directory has been passed on the CLI, we need the current instance of this
* ConfigurationResolver, which can be obtained by looping through the backtrace.
*/
private function initConfigurationResolver(): void
{
foreach (debug_backtrace() as $backtrace) {
if (isset($backtrace['object']) && $backtrace['object'] instanceof ConfigurationResolver) {
$this->configurationResolver = $backtrace['object'];
return;
}
}
die('Unable to initialize configuration resolver' . PHP_EOL);
}
/**
* Initialize files.
*/
private function initFiles(): void
{
if ($this->configurationResolver->getPath()) {
$this->initFilesFromCli();
} elseif (!posix_isatty(STDIN)) {
$this->initFilesFromStdin();
} else {
$this->initFilesFromGit();
}
}
/**
* Initialize files from CLI.
*/
private function initFilesFromCli(): void
{
$this->input = 'CLI';
$this->files = $this->findFiles($this->configurationResolver->getPath());
}
/**
* Initialize files from STDIN.
*/
private function initFilesFromStdin(): void
{
$this->input = 'STDIN';
$files = [];
$paths = explode(PHP_EOL, trim(stream_get_contents(STDIN)));
$paths = array_map(function ($path) {
return $this->findFiles($path);
}, $paths);
$files = array_merge($files, ...$paths);
$this->files = array_unique($files);
}
/**
* Initialize files from Git.
*/
private function initFilesFromGit(): void
{
$this->input = 'Git';
// Get destination branch from environment variable (required)
$destinationBranch = getenv('PHP_CS_FIXER_TARGET_BRANCH');
if ($destinationBranch === false || $destinationBranch === '') {
$destinationBranch = 'master';
}
$branchExists = $this->pipedExec(sprintf('git branch --remotes 2>/dev/null | grep --extended-regexp "^(\*| ) origin/%s( |$)" 2>/dev/null', $destinationBranch));
if ($branchExists === false) {
die(sprintf("fatal: Couldn't find remote ref %s", $destinationBranch) . PHP_EOL);
}
$this->pipedExec(sprintf('(git diff origin/%s.. --name-only --diff-filter=ACMRTUXB 2>/dev/null; git diff --cached --name-only --diff-filter=ACMRTUXB 2>/dev/null; git diff HEAD --name-only --diff-filter=ACMRTUXB 2>/dev/null) | grep "\.php$" 2>/dev/null | sort 2>/dev/null | uniq 2>/dev/null', $destinationBranch), $this->files);
$repositoryRoot = $this->pipedExec('git rev-parse --show-toplevel 2>/dev/null');
chdir($repositoryRoot);
}
/**
* Initialize directories from files.
*/
private function initDirectories(): void
{
$directories = [];
foreach ($this->files as $file) {
$directory = dirname($file);
foreach ($this->excludes as $exclude) {
if (strpos($directory . '/', $exclude . '/') === 0) {
continue 2;
}
}
$directories[] = $directory;
}
$this->directories = array_unique($directories);
}
/**
* Initialize finder.
*/
private function initFinder(): void
{
$files = &$this->files;
$this
->files()
->name('')
->depth('== 0')
->in('.')
->ignoreDotFiles(false)
->ignoreVCS(false)
->filter(function (SplFileInfo $fileinfo) use ($files) {
return in_array($fileinfo->__toString(), $files, false);
});
foreach ($this->files as $file) {
$this->name(basename($file));
}
foreach ($this->directories as $directory) {
$this->in($directory);
}
}
/**
* Output information.
*/
private function outputInfo(): void
{
echo sprintf('Loaded %d file(s) from %s', count($this), $this->input) . PHP_EOL;
}
/**
* Find files in path.
*
* @param string|mixed $path
*/
private function findFiles($path): array
{
if (is_file($path)) {
return (array) $path;
}
if (is_dir($path)) {
$finder = Finder::create()
->files()
->name('*.php')
->in($path)
->ignoreDotFiles(false)
->ignoreVCS(true);
return array_keys(iterator_to_array($finder, true));
}
return [];
}
/**
* Execute an external program without broken pipes.
*
* @return string|mixed|null
*/
private function pipedExec(string $command, array &$output = null, int &$returnVar = null)
{
$contents = '';
$handle = popen($command . '; echo $?', 'r');
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
pclose($handle);
$output = explode(PHP_EOL, trim($contents));
$returnVar = (int) array_pop($output);
return end($output);
}
}
return (new Config())
->setUsingCache(true)
->setRiskyAllowed(true)
->setRules([
// Symfony style includes PSR-12
'@Symfony' => true,
// see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/README.rst
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'no_blank_lines_before_namespace' => false,
'ordered_imports' => true,
'phpdoc_align' => false,
'general_phpdoc_tag_rename' => false,
'phpdoc_order' => true,
'no_unused_imports' => true,
'declare_strict_types' => true,
'final_internal_class' => false,
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author',
'copyright',
'category',
'version',
],
'case_sensitive' => false,
],
'global_namespace_import' => ['import_classes' => null],
'list_syntax' => ['syntax' => 'short'],
'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'],
'no_superfluous_elseif' => false,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true],
'php_unit_internal_class' => false,
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'php_unit_test_class_requires_covers' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'ordered_class_elements' => ['order' => ['use_trait', 'constant', 'property', 'construct', 'destruct', 'phpunit', 'method']],
'ternary_to_null_coalescing' => true,
'nullable_type_declaration_for_default_null_value' => true,
'modernize_types_casting' => true,
'use_arrow_functions' => true,
'class_definition' => ['single_line' => false],
])
->setFinder(CustomFinder::create());