1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povils\PHPMND\Console; |
4
|
|
|
|
5
|
|
|
use Povils\PHPMND\Detector; |
6
|
|
|
use Povils\PHPMND\ExtensionResolver; |
7
|
|
|
use Povils\PHPMND\FileReportList; |
8
|
|
|
use Povils\PHPMND\HintList; |
9
|
|
|
use Povils\PHPMND\PHPFinder; |
10
|
|
|
use Povils\PHPMND\Printer; |
11
|
|
|
use SebastianBergmann\Timer\Timer; |
12
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
13
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Command |
21
|
|
|
* |
22
|
|
|
* @package Povils\PHPMND\Console |
23
|
|
|
*/ |
24
|
|
|
class Command extends BaseCommand |
25
|
|
|
{ |
26
|
|
|
const EXIT_CODE_SUCCESS = 0; |
27
|
|
|
const EXIT_CODE_FAILURE = 1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
protected function configure() |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
|
|
->setName('phpmnd') |
36
|
|
|
->setDefinition( |
37
|
|
|
[ |
38
|
|
|
new InputArgument( |
39
|
|
|
'directory', |
40
|
|
|
InputArgument::REQUIRED, |
41
|
|
|
'Directory to analyze' |
42
|
|
|
) |
43
|
|
|
] |
44
|
|
|
) |
45
|
|
|
->addOption( |
46
|
|
|
'extensions', |
47
|
|
|
null, |
48
|
|
|
InputOption::VALUE_REQUIRED, |
49
|
|
|
'A comma-separated list of extensions' |
50
|
|
|
) |
51
|
|
|
->addOption( |
52
|
|
|
'ignore-numbers', |
53
|
|
|
null, |
54
|
|
|
InputOption::VALUE_REQUIRED, |
55
|
|
|
'A comma-separated list of numbers to ignore', |
56
|
|
|
'0, 1' |
57
|
|
|
) |
58
|
|
|
->addOption( |
59
|
|
|
'ignore-funcs', |
60
|
|
|
null, |
61
|
|
|
InputOption::VALUE_REQUIRED, |
62
|
|
|
'A comma-separated list of functions to ignore when using "argument" extension' |
63
|
|
|
) |
64
|
|
|
->addOption( |
65
|
|
|
'exclude', |
66
|
|
|
null, |
67
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
68
|
|
|
'Exclude a directory from code analysis (must be relative to source)' |
69
|
|
|
) |
70
|
|
|
->addOption( |
71
|
|
|
'exclude-path', |
72
|
|
|
null, |
73
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
74
|
|
|
'Exclude a path from code analysis (must be relative to source)' |
75
|
|
|
) |
76
|
|
|
->addOption( |
77
|
|
|
'exclude-file', |
78
|
|
|
null, |
79
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
80
|
|
|
'Exclude a file from code analysis (must be relative to source)' |
81
|
|
|
) |
82
|
|
|
->addOption( |
83
|
|
|
'suffixes', |
84
|
|
|
null, |
85
|
|
|
InputOption::VALUE_REQUIRED, |
86
|
|
|
'Comma-separated string of valid source code filename extensions', |
87
|
|
|
'php' |
88
|
|
|
) |
89
|
|
|
->addOption( |
90
|
|
|
'progress', |
91
|
|
|
null, |
92
|
|
|
InputOption::VALUE_NONE, |
93
|
|
|
'Show progress bar' |
94
|
|
|
) |
95
|
|
|
->addOption( |
96
|
|
|
'hint', |
97
|
|
|
null, |
98
|
|
|
InputOption::VALUE_NONE, |
99
|
|
|
'Suggest replacements for magic numbers' |
100
|
|
|
) |
101
|
|
|
->addOption( |
102
|
|
|
'non-zero-exit-on-violation', |
103
|
|
|
null, |
104
|
|
|
InputOption::VALUE_NONE, |
105
|
|
|
'Return a non zero exit code when there are magic numbers' |
106
|
|
|
) |
107
|
|
|
->addOption( |
108
|
|
|
'strings', |
109
|
|
|
null, |
110
|
|
|
InputOption::VALUE_NONE, |
111
|
|
|
'Include strings literal search in code analysis' |
112
|
|
|
) |
113
|
|
|
->addOption( |
114
|
|
|
'ignore-strings', |
115
|
|
|
null, |
116
|
|
|
InputOption::VALUE_REQUIRED, |
117
|
|
|
'A comma-separated list of strings to ignore when using "strings" option' |
118
|
|
|
) |
119
|
|
|
->addOption( |
120
|
|
|
'include-numeric-string', |
121
|
|
|
null, |
122
|
|
|
InputOption::VALUE_NONE, |
123
|
|
|
'Include strings which are numeric' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
131
|
|
|
{ |
132
|
|
|
$finder = $this->createFinder($input); |
133
|
|
|
|
134
|
|
|
if (0 === $finder->count()) { |
135
|
|
|
$output->writeln('No files found to scan'); |
136
|
|
|
return self::EXIT_CODE_SUCCESS; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$progressBar = null; |
140
|
|
|
if ($input->getOption('progress')) { |
141
|
|
|
$progressBar = new ProgressBar($output, $finder->count()); |
142
|
|
|
$progressBar->start(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$hintList = new HintList; |
146
|
|
|
$detector = new Detector($this->createOption($input), $hintList); |
147
|
|
|
|
148
|
|
|
$fileReportList = new FileReportList(); |
149
|
|
|
$printer = new Printer(); |
150
|
|
|
foreach ($finder as $file) { |
151
|
|
|
try { |
152
|
|
|
$fileReport = $detector->detect($file); |
153
|
|
|
if ($fileReport->hasMagicNumbers()) { |
154
|
|
|
$fileReportList->addFileReport($fileReport); |
155
|
|
|
} |
156
|
|
|
} catch (\Exception $e) { |
157
|
|
|
$output->writeln($e->getMessage()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($input->getOption('progress')) { |
161
|
|
|
$progressBar->advance(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($input->getOption('progress')) { |
166
|
|
|
$progressBar->finish(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($output->getVerbosity() !== OutputInterface::VERBOSITY_QUIET) { |
170
|
|
|
$output->writeln(''); |
171
|
|
|
$printer->printData($output, $fileReportList, $hintList); |
172
|
|
|
$output->writeln('<info>' . Timer::resourceUsage() . '</info>'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ($input->getOption('non-zero-exit-on-violation') && $fileReportList->hasMagicNumbers()) { |
176
|
|
|
return self::EXIT_CODE_FAILURE; |
177
|
|
|
} |
178
|
|
|
return self::EXIT_CODE_SUCCESS; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param InputInterface $input |
183
|
|
|
* @return Option |
184
|
|
|
* @throws \Exception |
185
|
|
|
*/ |
186
|
|
|
private function createOption(InputInterface $input) |
187
|
|
|
{ |
188
|
|
|
$option = new Option; |
189
|
|
|
$option->setIgnoreNumbers(array_map([$this, 'castToNumber'], $this->getCSVOption($input, 'ignore-numbers'))); |
190
|
|
|
$option->setIgnoreFuncs($this->getCSVOption($input, 'ignore-funcs')); |
191
|
|
|
$option->setIncludeStrings($input->getOption('strings')); |
192
|
|
|
$option->setNumericStrings($input->getOption('include-numeric-string')); |
193
|
|
|
$option->setIgnoreStrings($this->getCSVOption($input, 'ignore-strings')); |
194
|
|
|
$option->setGiveHint($input->getOption('hint')); |
195
|
|
|
$option->setExtensions( |
196
|
|
|
(new ExtensionResolver())->resolve($this->getCSVOption($input, 'extensions')) |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
return $option; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param InputInterface $input |
204
|
|
|
* @param string $option |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
private function getCSVOption(InputInterface $input, $option) |
209
|
|
|
{ |
210
|
|
|
$result = $input->getOption($option); |
211
|
|
|
if (false === is_array($result)) { |
212
|
|
|
return array_filter( |
213
|
|
|
explode(',', $result), |
214
|
|
|
function ($value) { |
215
|
|
|
return false === empty($value); |
216
|
|
|
} |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if (null === $result) { |
221
|
|
|
return []; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $result; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param InputInterface $input |
229
|
|
|
* |
230
|
|
|
* @return PHPFinder |
231
|
|
|
*/ |
232
|
|
|
protected function createFinder(InputInterface $input) |
233
|
|
|
{ |
234
|
|
|
return new PHPFinder( |
235
|
|
|
$input->getArgument('directory'), |
236
|
|
|
$input->getOption('exclude'), |
237
|
|
|
$input->getOption('exclude-path'), |
238
|
|
|
$input->getOption('exclude-file'), |
239
|
|
|
$this->getCSVOption($input, 'suffixes') |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $value |
245
|
|
|
* |
246
|
|
|
* @return int|float|string |
247
|
|
|
*/ |
248
|
|
|
private function castToNumber($value) |
249
|
|
|
{ |
250
|
|
|
if (is_numeric($value)) { |
251
|
|
|
$value += 0; // '2' -> (int) 2, '2.' -> (float) 2.0 |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $value; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|