|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Greywizard\Phpucd\Console; |
| 4 | + |
| 5 | +use Greywizard\Phpucd\Errors\ErrorInterface; |
| 6 | +use Greywizard\Phpucd\Phpucd; |
| 7 | +use Greywizard\Phpucd\Rules\StandardRules; |
| 8 | +use Greywizard\Phpucd\Tokens\Tokenizer; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Helper\ProgressBar; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | +use Symfony\Component\Finder\Finder; |
| 16 | +use Symfony\Component\Finder\SplFileInfo; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +class AnalyzeCommand extends Command |
| 22 | +{ |
| 23 | + const COMMAND_NAME = 'analyze'; |
| 24 | + const COMMAND_ALIAS = 'analyse'; |
| 25 | + const COMMAND_DESCRIPTION = 'Analyze PHP code'; |
| 26 | + |
| 27 | + const DEFAULT_EXT = '*.php'; |
| 28 | + |
| 29 | + protected function configure() |
| 30 | + { |
| 31 | + $this |
| 32 | + ->setName(static::COMMAND_NAME) |
| 33 | + ->setAliases([static::COMMAND_ALIAS]) |
| 34 | + ->setDescription(static::COMMAND_DESCRIPTION) |
| 35 | + ->addArgument('directory', InputArgument::REQUIRED) |
| 36 | + ->addOption( |
| 37 | + 'name', |
| 38 | + null, |
| 39 | + InputOption::VALUE_OPTIONAL, |
| 40 | + 'Pattern e.g. *.php', |
| 41 | + static::DEFAULT_EXT |
| 42 | + ) |
| 43 | + ; |
| 44 | + } |
| 45 | + |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 47 | + { |
| 48 | + $output->writeln(sprintf( |
| 49 | + '%s <info>%s</info>', |
| 50 | + Application::APPLICATION_NAME, |
| 51 | + Application::APPLICATION_VERSION |
| 52 | + )); |
| 53 | + $output->writeln(''); |
| 54 | + |
| 55 | + $finder = new Finder(); |
| 56 | + $dirArgument = rtrim($input->getArgument('directory'), '/\\'); |
| 57 | + $extOption = $input->getOption('name'); |
| 58 | + /** @var SplFileInfo[] $files */ |
| 59 | + $files = $finder->files()->in($dirArgument)->name($extOption); |
| 60 | + $progressBar = new ProgressBar($output, count($files)); |
| 61 | + $progressBar->setRedrawFrequency(1); |
| 62 | + $rules = new StandardRules(); |
| 63 | + $errors = []; |
| 64 | + $countErrors = 0; |
| 65 | + $countFiles = 0; |
| 66 | + $phpucd = new Phpucd(); |
| 67 | + |
| 68 | + $progressBar->display(); |
| 69 | + foreach ($files as $file) { |
| 70 | + $output->write(' ' . $file->getRelativePathname()); |
| 71 | + $fileErrors = \Greywizard\Phpucd\toArray($phpucd->getUnsafeTokens( |
| 72 | + Tokenizer::createFromFile($file), |
| 73 | + $rules |
| 74 | + )); |
| 75 | + if (count($fileErrors)) { |
| 76 | + $countErrors += count($fileErrors); |
| 77 | + $errors[$file->getPathname()] = $fileErrors; |
| 78 | + } |
| 79 | + $countFiles++; |
| 80 | + $progressBar->advance(); |
| 81 | + } |
| 82 | + |
| 83 | + $output->writeln(''); |
| 84 | + $output->writeln(''); |
| 85 | + |
| 86 | + if ($countErrors) { |
| 87 | + $this->handleErrors($output, $countErrors, $errors, $countFiles); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + $this->handleNoErrors($output, $countFiles); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param OutputInterface $output |
| 96 | + * @param int $countErrors |
| 97 | + * @param array $errors e.g. [filename => [TokenInterface, TokenInterface, ...], ...] |
| 98 | + * @param int $countFiles |
| 99 | + */ |
| 100 | + private function handleErrors(OutputInterface $output, $countErrors, $errors, $countFiles) |
| 101 | + { |
| 102 | + $margin = 2; |
| 103 | + $errorMsg = sprintf('Ooops... Found %d errors in %d files', $countErrors, $countFiles); |
| 104 | + $emptyLine = '<error>' . str_pad('', strlen($errorMsg) + $margin * 2, ' ') . '</error>'; |
| 105 | + $errorMsgFormat = '<error>' . str_pad('', $margin, ' ') . $errorMsg . str_pad('', $margin, ' ') . '</error>'; |
| 106 | + |
| 107 | + $output->writeln($emptyLine); |
| 108 | + $output->writeln($errorMsgFormat); |
| 109 | + $output->writeln($emptyLine); |
| 110 | + $output->writeln(''); |
| 111 | + |
| 112 | + $indent = ' * '; |
| 113 | + foreach ($errors as $filename => $fileErrors) { |
| 114 | + $output->writeln('<comment>' . $filename . ':</comment>'); |
| 115 | + foreach ($fileErrors as $error) { |
| 116 | + /** @var ErrorInterface $error */ |
| 117 | + $output->writeln($indent . '<comment>' . $error->getLine() . '</comment>: ' . $this->decorateSourceToken($error)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param OutputInterface $output |
| 124 | + * @param int $countFiles |
| 125 | + */ |
| 126 | + private function handleNoErrors(OutputInterface $output, $countFiles) |
| 127 | + { |
| 128 | + $output->writeln(sprintf('<info>Success - 0 errors in %d files.</info>', $countFiles)); |
| 129 | + } |
| 130 | + |
| 131 | + private function decorateSourceToken(ErrorInterface $error) |
| 132 | + { |
| 133 | + $result = preg_replace('{\\s+}', ' ', $error->getMessage()); |
| 134 | + if ($error->hasHighlightedPart()) { |
| 135 | + $result = str_replace( |
| 136 | + $error->getHighlightedPart(), |
| 137 | + '<error>' . $error->getHighlightedPart() . '</error>', |
| 138 | + $result |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + return $result; |
| 143 | + } |
| 144 | +} |
0 commit comments