forked from PHP-CS-Fixer/PHP-CS-Fixer
/
HelpCommand.php
670 lines (517 loc) · 22.6 KB
/
HelpCommand.php
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Console\Command;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerConfiguration\AliasedFixerOption;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
use PhpCsFixer\FixerConfiguration\DeprecatedFixerOption;
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\Preg;
use PhpCsFixer\RuleSet\RuleSet;
use PhpCsFixer\RuleSet\RuleSets;
use PhpCsFixer\Utils;
use Symfony\Component\Console\Command\HelpCommand as BaseHelpCommand;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Fabien Potencier <fabien@symfony.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author SpacePossum
*
* @internal
*/
final class HelpCommand extends BaseHelpCommand
{
protected static $defaultName = 'help';
/**
* Returns help-copy suitable for console output.
*
* @return string
*/
public static function getHelpCopy()
{
$template =
<<<'EOF'
The <info>%command.name%</info> command tries to fix as much coding standards
problems as possible on a given file or files in a given directory and its subdirectories:
<info>$ php %command.full_name% /path/to/dir</info>
<info>$ php %command.full_name% /path/to/file</info>
By default <comment>--path-mode</comment> is set to `override`, which means, that if you specify the path to a file or a directory via
command arguments, then the paths provided to a `Finder` in config file will be ignored. You can use <comment>--path-mode=intersection</comment>
to merge paths from the config file and from the argument:
<info>$ php %command.full_name% --path-mode=intersection /path/to/dir</info>
The <comment>--format</comment> option for the output format. Supported formats are `txt` (default one), `json`, `xml`, `checkstyle`, `junit` and `gitlab`.
NOTE: the output for the following formats are generated in accordance with XML schemas
* `checkstyle` follows the common `"checkstyle" xml schema </doc/report-schema/checkstyle.xsd>`_
* `junit` follows the `JUnit xml schema from Jenkins </doc/report-schema/junit-10.xsd>`_
The <comment>--quiet</comment> Do not output any message.
The <comment>--verbose</comment> option will show the applied rules. When using the `txt` format it will also display progress notifications.
NOTE: if there is an error like "errors reported during linting after fixing", you can use this to be even more verbose for debugging purpose
* `-v`: verbose
* `-vv`: very verbose
* `-vvv`: debug
The <comment>--rules</comment> option limits the rules to apply to the
project:
<info>$ php %command.full_name% /path/to/project --rules=@PSR2</info>
By default the PSR1 and PSR2 rules are used.
The <comment>--rules</comment> option lets you choose the exact rules to
apply (the rule names must be separated by a comma):
<info>$ php %command.full_name% /path/to/dir --rules=line_ending,full_opening_tag,indentation_type</info>
You can also exclude the rules you don't want by placing a dash in front of the rule name, if this is more convenient,
using <comment>-name_of_fixer</comment>:
<info>$ php %command.full_name% /path/to/dir --rules=-full_opening_tag,-indentation_type</info>
When using combinations of exact and exclude rules, applying exact rules along with above excluded results:
<info>$ php %command.full_name% /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_statement,strict_comparison</info>
Complete configuration for rules can be supplied using a `json` formatted string.
<info>$ php %command.full_name% /path/to/project --rules='{"concat_space": {"spacing": "none"}}'</info>
The <comment>--dry-run</comment> flag will run the fixer without making changes to your files.
The <comment>--diff</comment> flag can be used to let the fixer output all the changes it makes.
The <comment>--diff-format</comment> option allows to specify in which format the fixer should output the changes it makes:
* <comment>udiff</comment>: unified diff format;
* <comment>sbd</comment>: Sebastianbergmann/diff format (default when using `--diff` without specifying `diff-format`).
The <comment>--allow-risky</comment> option (pass `yes` or `no`) allows you to set whether risky rules may run. Default value is taken from config file.
A rule is considered risky if it could change code behaviour. By default no risky rules are run.
The <comment>--stop-on-violation</comment> flag stops the execution upon first file that needs to be fixed.
The <comment>--show-progress</comment> option allows you to choose the way process progress is rendered:
* <comment>none</comment>: disables progress output;
* <comment>run-in</comment>: [deprecated] simple single-line progress output;
* <comment>estimating</comment>: [deprecated] multiline progress output with number of files and percentage on each line. Note that with this option, the files list is evaluated before processing to get the total number of files and then kept in memory to avoid using the file iterator twice. This has an impact on memory usage so using this option is not recommended on very large projects;
* <comment>estimating-max</comment>: [deprecated] same as <comment>dots</comment>;
* <comment>dots</comment>: same as <comment>estimating</comment> but using all terminal columns instead of default 80.
If the option is not provided, it defaults to <comment>run-in</comment> unless a config file that disables output is used, in which case it defaults to <comment>none</comment>. This option has no effect if the verbosity of the command is less than <comment>verbose</comment>.
<info>$ php %command.full_name% --verbose --show-progress=estimating</info>
The command can also read from standard input, in which case it won't
automatically fix anything:
<info>$ cat foo.php | php %command.full_name% --diff -</info>
Finally, if you don't need BC kept on CLI level, you might use `PHP_CS_FIXER_FUTURE_MODE` to start using options that
would be default in next MAJOR release (unified differ, estimating, full-width progress indicator):
<info>$ PHP_CS_FIXER_FUTURE_MODE=1 php %command.full_name% -v --diff</info>
Rules
-----
Use the following command to quickly understand what a rule will do to your code:
<info>$ php php-cs-fixer.phar describe align_multiline_comment</info>
To visualize all the rules that belong to a ruleset:
<info>$ php php-cs-fixer.phar describe @PSR2</info>
Choose from the list of available rules:
%%%FIXERS_DETAILS%%%
The <comment>--dry-run</comment> option displays the files that need to be
fixed but without actually modifying them:
<info>$ php %command.full_name% /path/to/code --dry-run</info>
Config file
-----------
Instead of using command line options to customize the rule, you can save the
project configuration in a <comment>.php-cs-fixer.dist.php</comment> file in the root directory of your project.
The file must return an instance of `PhpCsFixer\ConfigInterface` (<url>%%%CONFIG_INTERFACE_URL%%%</url>)
which lets you configure the rules, the files and directories that
need to be analyzed. You may also create <comment>.php-cs-fixer.php</comment> file, which is
the local configuration that will be used instead of the project configuration. It
is a good practice to add that file into your <comment>.gitignore</comment> file.
With the <comment>--config</comment> option you can specify the path to the
<comment>.php-cs-fixer.php</comment> file.
The example below will add two rules to the default list of PSR2 set rules:
<?php
$finder = PhpCsFixer\Finder::create()
->exclude('somedir')
->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php')
->in(__DIR__)
;
$config = new Config();
return $config
->setRules([
'@PSR2' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder)
;
?>
**NOTE**: `exclude` will work only for directories, so if you need to exclude file, try `notPath`.
Both `exclude` and `notPath` methods accept only relative paths to the ones defined with the `in` method.
See `Symfony\Finder` (<url>https://symfony.com/doc/current/components/finder.html</url>)
online documentation for other `Finder` methods.
You may also use an exclude list for the rules instead of the above shown include approach.
The following example shows how to use all `Symfony` rules but the `full_opening_tag` rule.
<?php
$finder = PhpCsFixer\Finder::create()
->exclude('somedir')
->in(__DIR__)
;
$config = new Config();
return $config
->setRules([
'@Symfony' => true,
'full_opening_tag' => false,
])
->setFinder($finder)
;
?>
You may want to use non-linux whitespaces in your project. Then you need to
configure them in your config file.
<?php
$config = new Config();
return $config
->setIndent("\t")
->setLineEnding("\r\n")
;
?>
By using `--using-cache` option with `yes` or `no` you can set if the caching
mechanism should be used.
Caching
-------
The caching mechanism is enabled by default. This will speed up further runs by
fixing only files that were modified since the last run. The tool will fix all
files if the tool version has changed or the list of rules has changed.
Cache is supported only for tool downloaded as phar file or installed via
composer.
Cache can be disabled via `--using-cache` option or config file:
<?php
$config = new Config();
return $config->setUsingCache(false);
?>
Cache file can be specified via `--cache-file` option or config file:
<?php
$config = new Config();
return $config->setCacheFile(__DIR__.'/.php_cs.cache');
?>
Using PHP CS Fixer on CI
------------------------
Require `friendsofphp/php-cs-fixer` as a `dev` dependency:
$ ./composer.phar require --dev friendsofphp/php-cs-fixer
Then, add the following command to your CI:
%%%CI_INTEGRATION%%%
Where `$COMMIT_RANGE` is your range of commits, e.g. `$TRAVIS_COMMIT_RANGE` or `HEAD~..HEAD`.
Exit code
---------
Exit code of the fix command is built using following bit flags:
* 0 - OK.
* 1 - General error (or PHP minimal requirement not matched).
* 4 - Some files have invalid syntax (only in dry-run mode).
* 8 - Some files need fixing (only in dry-run mode).
* 16 - Configuration error of the application.
* 32 - Configuration error of a Fixer.
* 64 - Exception raised within the application.
EOF
;
return strtr($template, [
'%%%CONFIG_INTERFACE_URL%%%' => sprintf(
'https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v%s/src/ConfigInterface.php',
self::getLatestReleaseVersionFromChangeLog()
),
'%%%CI_INTEGRATION%%%' => implode("\n", array_map(
static function ($line) { return ' $ '.$line; },
\array_slice(file(__DIR__.'/../../../ci-integration.sh', FILE_IGNORE_NEW_LINES), 3)
)),
'%%%FIXERS_DETAILS%%%' => self::getFixersHelp(),
]);
}
/**
* @param mixed $value
*
* @return string
*/
public static function toString($value)
{
return \is_array($value)
? static::arrayToString($value)
: static::scalarToString($value)
;
}
/**
* Returns the allowed values of the given option that can be converted to a string.
*
* @return null|array
*/
public static function getDisplayableAllowedValues(FixerOptionInterface $option)
{
$allowed = $option->getAllowedValues();
if (null !== $allowed) {
$allowed = array_filter($allowed, static function ($value) {
return !($value instanceof \Closure);
});
usort($allowed, static function ($valueA, $valueB) {
if ($valueA instanceof AllowedValueSubset) {
return -1;
}
if ($valueB instanceof AllowedValueSubset) {
return 1;
}
return strcasecmp(
self::toString($valueA),
self::toString($valueB)
);
});
if (0 === \count($allowed)) {
$allowed = null;
}
}
return $allowed;
}
/**
* @throws \RuntimeException when failing to parse the change log file
*
* @return string
*/
public static function getLatestReleaseVersionFromChangeLog()
{
static $version = null;
if (null !== $version) {
return $version;
}
$changelogFile = self::getChangeLogFile();
if (null === $changelogFile) {
$version = Application::VERSION;
return $version;
}
$changelog = @file_get_contents($changelogFile);
if (false === $changelog) {
$error = error_get_last();
throw new \RuntimeException(sprintf(
'Failed to read content of the changelog file "%s".%s',
$changelogFile,
$error ? ' '.$error['message'] : ''
));
}
for ($i = Application::getMajorVersion(); $i > 0; --$i) {
if (1 === Preg::match('/Changelog for v('.$i.'.\d+.\d+)/', $changelog, $matches)) {
$version = $matches[1];
break;
}
}
if (null === $version) {
throw new \RuntimeException(sprintf('Failed to parse changelog data of "%s".', $changelogFile));
}
return $version;
}
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$output->getFormatter()->setStyle('url', new OutputFormatterStyle('blue'));
}
/**
* @return null|string
*/
private static function getChangeLogFile()
{
$changelogFile = __DIR__.'/../../../CHANGELOG.md';
return is_file($changelogFile) ? $changelogFile : null;
}
/**
* @return string
*/
private static function getFixersHelp()
{
$help = '';
$fixerFactory = new FixerFactory();
/** @var AbstractFixer[] $fixers */
$fixers = $fixerFactory->registerBuiltInFixers()->getFixers();
// sort fixers by name
usort(
$fixers,
static function (FixerInterface $a, FixerInterface $b) {
return strcmp($a->getName(), $b->getName());
}
);
$ruleSets = [];
foreach (RuleSets::getSetDefinitionNames() as $setName) {
$ruleSets[$setName] = new RuleSet([$setName => true]);
}
$getSetsWithRule = static function ($rule) use ($ruleSets) {
$sets = [];
foreach ($ruleSets as $setName => $ruleSet) {
if ($ruleSet->hasRule($rule)) {
$sets[] = $setName;
}
}
return $sets;
};
$count = \count($fixers) - 1;
foreach ($fixers as $i => $fixer) {
$sets = $getSetsWithRule($fixer->getName());
$description = $fixer->getDefinition()->getSummary();
if ($fixer instanceof DeprecatedFixerInterface) {
$successors = $fixer->getSuccessorsNames();
$message = [] === $successors
? 'will be removed on next major version'
: sprintf('use %s instead', Utils::naturalLanguageJoinWithBackticks($successors));
$description .= sprintf(' DEPRECATED: %s.', $message);
}
$description = implode("\n | ", self::wordwrap(
Preg::replace('/(`.+?`)/', '<info>$1</info>', $description),
72
));
if (!empty($sets)) {
$help .= sprintf(" * <comment>%s</comment> [%s]\n | %s\n", $fixer->getName(), implode(', ', $sets), $description);
} else {
$help .= sprintf(" * <comment>%s</comment>\n | %s\n", $fixer->getName(), $description);
}
if ($fixer->isRisky()) {
$help .= sprintf(
" | *Risky rule: %s.*\n",
Preg::replace(
'/(`.+?`)/',
'<info>$1</info>',
lcfirst(Preg::replace('/\.$/', '', $fixer->getDefinition()->getRiskyDescription()))
)
);
}
if ($fixer instanceof ConfigurationDefinitionFixerInterface) {
$configurationDefinition = $fixer->getConfigurationDefinition();
$configurationDefinitionOptions = $configurationDefinition->getOptions();
if (\count($configurationDefinitionOptions)) {
$help .= " |\n | Configuration options:\n";
usort(
$configurationDefinitionOptions,
static function (FixerOptionInterface $optionA, FixerOptionInterface $optionB) {
return strcmp($optionA->getName(), $optionB->getName());
}
);
foreach ($configurationDefinitionOptions as $option) {
$line = '<info>'.OutputFormatter::escape($option->getName()).'</info>';
$allowed = self::getDisplayableAllowedValues($option);
if (null !== $allowed) {
foreach ($allowed as &$value) {
if ($value instanceof AllowedValueSubset) {
$value = 'a subset of <comment>'.self::toString($value->getAllowedValues()).'</comment>';
} else {
$value = '<comment>'.self::toString($value).'</comment>';
}
}
} else {
$allowed = array_map(
static function ($type) {
return '<comment>'.$type.'</comment>';
},
$option->getAllowedTypes()
);
}
if (null !== $allowed) {
$line .= ' ('.implode(', ', $allowed).')';
}
$line .= ': '.Preg::replace(
'/(`.+?`)/',
'<info>$1</info>',
lcfirst(Preg::replace('/\.$/', '', OutputFormatter::escape($option->getDescription())))
).'; ';
if ($option->hasDefault()) {
$line .= 'defaults to <comment>'.self::toString($option->getDefault()).'</comment>';
} else {
$line .= 'required';
}
if ($option instanceof DeprecatedFixerOption) {
$line .= '. DEPRECATED: '.Preg::replace(
'/(`.+?`)/',
'<info>$1</info>',
lcfirst(Preg::replace('/\.$/', '', OutputFormatter::escape($option->getDeprecationMessage())))
);
}
if ($option instanceof AliasedFixerOption) {
$line .= '; DEPRECATED alias: <comment>'.$option->getAlias().'</comment>';
}
foreach (self::wordwrap($line, 72) as $index => $line) {
$help .= (0 === $index ? ' | - ' : ' | ').$line."\n";
}
}
}
} elseif ($fixer instanceof ConfigurableFixerInterface) {
$help .= " | *Configurable rule.*\n";
}
if ($count !== $i) {
$help .= "\n";
}
}
// prevent "\</foo>" from being rendered as an escaped literal style tag
return Preg::replace('#\\\\(</.*?>)#', '<<$1', $help);
}
/**
* Wraps a string to the given number of characters, ignoring style tags.
*
* @param string $string
* @param int $width
*
* @return string[]
*/
private static function wordwrap($string, $width)
{
$result = [];
$currentLine = 0;
$lineLength = 0;
foreach (explode(' ', $string) as $word) {
$wordLength = \strlen(Preg::replace('~</?(\w+)>~', '', $word));
if (0 !== $lineLength) {
++$wordLength; // space before word
}
if ($lineLength + $wordLength > $width) {
++$currentLine;
$lineLength = 0;
}
$result[$currentLine][] = $word;
$lineLength += $wordLength;
}
return array_map(static function ($line) {
return implode(' ', $line);
}, $result);
}
/**
* @param mixed $value
*
* @return string
*/
private static function scalarToString($value)
{
$str = var_export($value, true);
return Preg::replace('/\bNULL\b/', 'null', $str);
}
/**
* @return string
*/
private static function arrayToString(array $value)
{
if (0 === \count($value)) {
return '[]';
}
$isHash = static::isHash($value);
$str = '[';
foreach ($value as $k => $v) {
if ($isHash) {
$str .= static::scalarToString($k).' => ';
}
$str .= \is_array($v)
? static::arrayToString($v).', '
: static::scalarToString($v).', '
;
}
return substr($str, 0, -2).']';
}
/**
* @return bool
*/
private static function isHash(array $array)
{
$i = 0;
foreach ($array as $k => $v) {
if ($k !== $i) {
return true;
}
++$i;
}
return false;
}
}