1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpNsFixer\Console; |
6
|
|
|
|
7
|
|
|
use PhpNsFixer\Finder\FileFinder; |
8
|
|
|
use PhpNsFixer\Runner\Runner; |
9
|
|
|
use PhpNsFixer\Runner\RunnerOptions; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Tightenco\Collect\Support\Collection; |
15
|
|
|
|
16
|
|
|
final class FixCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Configures the current command. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
5 |
|
protected function configure() |
24
|
|
|
{ |
25
|
5 |
|
$this->setName('fix')->setDescription('Fixes a directory'); |
26
|
5 |
|
$this->setDefinition([ |
27
|
5 |
|
new InputArgument('path', InputArgument::REQUIRED, 'The path.'), |
28
|
5 |
|
new InputOption('prefix', 'P', InputOption::VALUE_REQUIRED, 'Namespace prefix.'), |
29
|
5 |
|
new InputOption('skip-empty', 'E', InputOption::VALUE_NONE, 'Skip files without namespace.'), |
30
|
5 |
|
new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Only show which files would have been modified.') |
31
|
|
|
]); |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
38
|
|
|
{ |
39
|
5 |
|
$files = FileFinder::list(strval($input->getArgument('path'))); |
40
|
|
|
|
41
|
5 |
|
$this->progressStart($output, $files); |
42
|
5 |
|
$problematicFiles = $this->runFixer($this->resolveOptions($input, $files)); |
43
|
5 |
|
$this->progressFinish($output); |
44
|
|
|
|
45
|
5 |
|
$this->printResults($output, $problematicFiles, $this->getDryRunOption($input)); |
46
|
|
|
|
47
|
5 |
|
return $problematicFiles->count(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param RunnerOptions $options |
52
|
|
|
* @return Collection |
53
|
|
|
*/ |
54
|
5 |
|
private function runFixer(RunnerOptions $options): Collection |
55
|
|
|
{ |
56
|
5 |
|
return (new Runner($options, $this->dispatcher))->run(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param InputInterface $input |
61
|
|
|
* @param Collection $files |
62
|
|
|
* @return RunnerOptions |
63
|
|
|
*/ |
64
|
5 |
|
private function resolveOptions(InputInterface $input, Collection $files) |
65
|
|
|
{ |
66
|
5 |
|
return new RunnerOptions( |
67
|
5 |
|
$files, |
68
|
5 |
|
$this->getPrefixOption($input), |
69
|
5 |
|
$this->getSkipEmptyOption($input), |
70
|
5 |
|
$this->getDryRunOption($input) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Parse the prefix input option. |
76
|
|
|
* |
77
|
|
|
* @param InputInterface $input |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
5 |
|
private function getPrefixOption(InputInterface $input): string |
81
|
|
|
{ |
82
|
5 |
|
return strval($input->getOption('prefix')) ?? ''; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Parse the skip-empty input option. |
87
|
|
|
* |
88
|
|
|
* @param InputInterface $input |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
5 |
|
private function getSkipEmptyOption(InputInterface $input): bool |
92
|
|
|
{ |
93
|
5 |
|
return boolval($input->getOption('skip-empty')) ?? false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Parse the dry-run input option. |
98
|
|
|
* |
99
|
|
|
* @param InputInterface $input |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
5 |
|
private function getDryRunOption(InputInterface $input): bool |
103
|
|
|
{ |
104
|
5 |
|
return boolval($input->getOption('dry-run')) ?? false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|