|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Composer Plugin QA. |
| 5 | + * |
| 6 | + * @author Webysther Nunes <webysther@gmail.com> |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Webs\QA\Command\Lint; |
| 10 | + |
| 11 | +use Composer\Command\BaseCommand; |
| 12 | +use RecursiveDirectoryIterator; |
| 13 | +use RecursiveIteratorIterator; |
| 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 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 19 | +use Symfony\Component\Process\Process; |
| 20 | +use Webs\QA\Command\Util; |
| 21 | + |
| 22 | +/** |
| 23 | + * Run PHP -l (lint) for all PHP files in project |
| 24 | + */ |
| 25 | +class Lint extends BaseCommand |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Console description. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $description = 'Lint'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Console params configuration. |
| 36 | + */ |
| 37 | + protected function configure() |
| 38 | + { |
| 39 | + $this->setName('qa:lint') |
| 40 | + ->setDescription($this->description) |
| 41 | + ->addArgument( |
| 42 | + 'source', |
| 43 | + InputArgument::IS_ARRAY | InputArgument::OPTIONAL, |
| 44 | + 'List of directories/files to search <comment>[Default:"src,app,tests"]</>' |
| 45 | + ) |
| 46 | + ->addOption( |
| 47 | + 'diff', |
| 48 | + null, |
| 49 | + InputOption::VALUE_NONE, |
| 50 | + 'Use `git status -s` to search files to check' |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Execution. |
| 56 | + * |
| 57 | + * @param InputInterface $input Input console |
| 58 | + * @param OutputInterface $output Output console |
| 59 | + * |
| 60 | + * @return int Exit code |
| 61 | + */ |
| 62 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 63 | + { |
| 64 | + $start = microtime(true); |
| 65 | + $this->output = $output; |
| 66 | + $command = $this; |
| 67 | + $style = new SymfonyStyle($input, $output); |
| 68 | + $style->title($this->description); |
| 69 | + |
| 70 | + $util = new Util(); |
| 71 | + $source = $util->checkSource($input); |
| 72 | + if ($input->getOption('diff')) { |
| 73 | + $source = $util->getDiffSource(); |
| 74 | + } |
| 75 | + |
| 76 | + $files = array(); |
| 77 | + $sources = explode(' ', $source); |
| 78 | + foreach ($sources as $source) { |
| 79 | + if(!is_dir($source)){ |
| 80 | + $files[] = $source; |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + $recursiveFiles = new RecursiveIteratorIterator( |
| 85 | + new RecursiveDirectoryIterator($source) |
| 86 | + ); |
| 87 | + foreach ($recursiveFiles as $file) { |
| 88 | + if ($file->isDir()){ |
| 89 | + continue; |
| 90 | + } |
| 91 | + $files[] = $file->getPathname(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $sources = $files; |
| 96 | + $exitCode = 0; |
| 97 | + $columnSize = 80; |
| 98 | + $errors = ''; |
| 99 | + foreach ($sources as $line => $source) { |
| 100 | + if( ($line % $columnSize) == 0 ) { |
| 101 | + $style->newLine(); |
| 102 | + } |
| 103 | + |
| 104 | + $cmd = 'php -l '.$source; |
| 105 | + $process = new Process($cmd); |
| 106 | + $process->setTimeout(3600)->run(function ($type, $buffer) use ($command, &$errors) { |
| 107 | + if (strpos($buffer, 'No syntax errors') !== false || Process::ERR == $type) { |
| 108 | + $command->output->write('.'); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + $command->output->write('<error>F</>'); |
| 113 | + $errors = $buffer; |
| 114 | + }); |
| 115 | + |
| 116 | + if (!$exitCode) { |
| 117 | + $exitCode = $process->getExitCode(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + $end = microtime(true); |
| 122 | + $time = round($end - $start); |
| 123 | + |
| 124 | + $style->newLine(); |
| 125 | + $output->write($errors); |
| 126 | + $style->newLine(); |
| 127 | + |
| 128 | + $style->section('Results'); |
| 129 | + $output->writeln('<info>Command: php -l FILE</>'); |
| 130 | + $output->writeln('<info>Files: ' . count($files) . '</>'); |
| 131 | + $output->writeln('<info>Time: '.$time.' seconds</>'); |
| 132 | + $style->newLine(); |
| 133 | + |
| 134 | + return $exitCode; |
| 135 | + } |
| 136 | +} |
0 commit comments