1 | <?php |
||
21 | class Command extends BaseCommand |
||
22 | { |
||
23 | /** |
||
24 | * @inheritdoc |
||
25 | */ |
||
26 | protected function configure() |
||
27 | { |
||
28 | $this |
||
29 | ->setName('phpmnd') |
||
30 | ->setDefinition( |
||
31 | [ |
||
32 | new InputArgument( |
||
33 | 'directory', |
||
34 | InputArgument::REQUIRED, |
||
35 | 'Directory to analyze' |
||
36 | ) |
||
37 | ] |
||
38 | ) |
||
39 | ->addOption( |
||
40 | 'extensions', |
||
41 | null, |
||
42 | InputOption::VALUE_REQUIRED, |
||
43 | 'A comma-separated list of extensions', |
||
44 | [] |
||
45 | ) |
||
46 | ->addOption( |
||
47 | 'ignore-numbers', |
||
48 | null, |
||
49 | InputOption::VALUE_REQUIRED, |
||
50 | 'A comma-separated list of numbers to ignore', |
||
51 | [0, 1] |
||
52 | ) |
||
53 | ->addOption( |
||
54 | 'ignore-funcs', |
||
55 | null, |
||
56 | InputOption::VALUE_REQUIRED, |
||
57 | 'A comma-separated list of functions to ignore when using "argument" extension', |
||
58 | [] |
||
59 | ) |
||
60 | ->addOption( |
||
61 | 'exclude', |
||
62 | null, |
||
63 | InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
||
64 | 'Exclude a directory from code analysis (must be relative to source)' |
||
65 | ) |
||
66 | ->addOption( |
||
67 | 'progress', |
||
68 | null, |
||
69 | InputOption::VALUE_NONE, |
||
70 | 'Show progress bar' |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | protected function execute(InputInterface $input, OutputInterface $output) |
||
78 | { |
||
79 | $finder = new PHPFinder(); |
||
80 | $finder |
||
81 | ->in($input->getArgument('directory')) |
||
82 | ->exclude(array_merge(['vendor'], $input->getOption('exclude'))); |
||
83 | |||
84 | if (0 === $finder->count()) { |
||
85 | $output->writeln('No files found to scan'); |
||
86 | exit(1); |
||
87 | } |
||
88 | |||
89 | $progressBar = null; |
||
90 | if ($input->getOption('progress')) { |
||
91 | $progressBar = new ProgressBar($output, $finder->count()); |
||
92 | $progressBar->start(); |
||
93 | } |
||
94 | |||
95 | $detector = new Detector($this->createOption($input)); |
||
96 | |||
97 | $printer = new Printer(); |
||
98 | foreach ($finder as $file) { |
||
99 | try { |
||
100 | $fileReport = $detector->detect($file); |
||
101 | if ($fileReport->hasMagicNumbers()) { |
||
102 | $printer->addFileReport($fileReport); |
||
103 | } |
||
104 | } catch (\Exception $e) { |
||
105 | $output->writeln($e->getMessage()); |
||
106 | } |
||
107 | |||
108 | if ($input->getOption('progress')) { |
||
109 | $progressBar->advance(); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | if ($input->getOption('progress')) { |
||
114 | $progressBar->finish(); |
||
115 | } |
||
116 | |||
117 | if ($output->getVerbosity() !== OutputInterface::VERBOSITY_QUIET) { |
||
118 | $output->writeln(''); |
||
119 | $printer->printData($output); |
||
120 | $output->writeln('<info>' . \PHP_Timer::resourceUsage() . '</info>'); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param InputInterface $input |
||
126 | * @param string $option |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | private function getCSVOption(InputInterface $input, $option) |
||
140 | |||
141 | /** |
||
142 | * @param string $value |
||
143 | * |
||
144 | * @return int|float|string |
||
145 | */ |
||
146 | private function castToNumber($value) |
||
154 | |||
155 | /** |
||
156 | * @param InputInterface $input |
||
157 | * @return Option |
||
158 | * @throws \Exception |
||
159 | */ |
||
160 | private function createOption(InputInterface $input) |
||
172 | } |
||
173 |