1 | <?php |
||
11 | class PhpCsFixer extends ActionAbstract |
||
12 | { |
||
13 | const NONE_LEVEL = 0; |
||
14 | const PSR0_LEVEL = 1; |
||
15 | const PSR1_LEVEL = 3; |
||
16 | const PSR2_LEVEL = 7; |
||
17 | const SYMFONY_LEVEL = 15; |
||
18 | const CONTRIB_LEVEL = 32; |
||
19 | |||
20 | /** |
||
21 | * @var FileList |
||
22 | */ |
||
23 | protected $files; |
||
24 | |||
25 | /** |
||
26 | * @var int|null |
||
27 | */ |
||
28 | protected $level; |
||
29 | |||
30 | /** |
||
31 | * @var string[]|null |
||
32 | */ |
||
33 | protected $fixers; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $addAutomatically; |
||
39 | |||
40 | /** |
||
41 | * Runs all the provided files through PHP-CS-Fixer, fixing any code style issues. |
||
42 | * |
||
43 | * @param FileList $files The files to check |
||
44 | * @param int|null $level (Optional, defaults to NONE_LEVEL) Fixer level to use |
||
45 | * @param string[]|null $fixers (Optional, defaults to null) Set the fixers to use |
||
46 | * @param bool $addAutomatically (Optional, default is true) Whether to add modified files to commit or not |
||
47 | */ |
||
48 | 3 | public function __construct(FileList $files, $level = self::NONE_LEVEL, $fixers = null, $addAutomatically = true) |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function getName() |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function isSupported() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 3 | public function execute(InputInterface $input, OutputInterface $output) |
|
84 | { |
||
85 | 3 | $fixer = $this->getCsFixer(); |
|
86 | 3 | $fixers = $this->resolveFixers($fixer, $this->level, $this->fixers); |
|
|
|||
87 | 3 | $cache = new \Symfony\CS\FileCacheManager(false, getcwd(), $fixers); |
|
88 | |||
89 | 3 | $progress = $this->createProgressBar($input, $output, $this->files->count()); |
|
90 | 3 | $progress->start(); |
|
91 | |||
92 | /** @var SfyFileInfo $file */ |
||
93 | 3 | foreach ($this->files->getSourceIterator() as $file) { |
|
94 | 2 | $progress->setMessage('Processing ' . $file->getRelativePathname() . '...'); |
|
95 | |||
96 | 2 | if ($fixer->fixFile($file, $fixers, false, false, $cache) && $this->addAutomatically) { |
|
97 | $process = new Process('git add ' . escapeshellarg($file->getRelativePathname())); |
||
98 | $process->mustRun(); |
||
99 | } |
||
100 | |||
101 | 2 | $progress->advance(); |
|
102 | } |
||
103 | |||
104 | 3 | $progress->finish(); |
|
105 | 3 | } |
|
106 | |||
107 | /** |
||
108 | * @return \Symfony\CS\Fixer |
||
109 | */ |
||
110 | 3 | private function getCsFixer() |
|
118 | |||
119 | /** |
||
120 | * @param \Symfony\CS\Fixer $fixer |
||
121 | * @param int $level |
||
122 | * @param string[] $fixers |
||
123 | * |
||
124 | * @return \Symfony\CS\FixerInterface[] |
||
125 | */ |
||
126 | 3 | private function resolveFixers($fixer, $level, $fixers) |
|
140 | } |
||
141 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.