|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHP Docblock Checker |
| 4 | + * |
| 5 | + * @copyright Copyright 2014, Block 8 Limited. |
| 6 | + * @license https://github.com/Block8/php-docblock-checker/blob/master/LICENSE.md |
| 7 | + * @link http://www.phptesting.org/ |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PhpDocblockChecker; |
| 11 | + |
| 12 | +use DirectoryIterator; |
| 13 | +use PHP_Token_Stream; |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Console command to check a directory of PHP files for Docblocks. |
| 21 | + * @author Dan Cryer <dan@block8.co.uk> |
| 22 | + */ |
| 23 | +class CheckerCommand extends Command |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $basePath = './'; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var bool |
| 32 | + */ |
| 33 | + protected $verbose = true; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $report = array(); |
| 39 | + |
| 40 | + /** |
| 41 | + * @var array |
| 42 | + */ |
| 43 | + protected $exclude = array(); |
| 44 | + |
| 45 | + /** |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + protected $skipClasses = false; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var bool |
| 52 | + */ |
| 53 | + protected $skipMethods = false; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var OutputInterface |
| 57 | + */ |
| 58 | + protected $output; |
| 59 | + |
| 60 | + protected function configure() |
| 61 | + { |
| 62 | + $this |
| 63 | + ->setName('check') |
| 64 | + ->setDescription('Check PHP files within a directory for appropriate use of Docblocks.') |
| 65 | + ->addOption('exclude', 'x', InputOption::VALUE_REQUIRED, 'Files and directories to exclude.', null) |
| 66 | + ->addOption('directory', 'd', InputOption::VALUE_REQUIRED, 'Directory to scan.', './') |
| 67 | + ->addOption('skip-classes', null, InputOption::VALUE_NONE, 'Don\'t check classes for docblocks.') |
| 68 | + ->addOption('skip-methods', null, InputOption::VALUE_NONE, 'Don\'t check methods for docblocks.') |
| 69 | + ->addOption('json', 'j', InputOption::VALUE_NONE, 'Output JSON instead of a log.'); |
| 70 | + } |
| 71 | + |
| 72 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 73 | + { |
| 74 | + // Process options: |
| 75 | + $exclude = $input->getOption('exclude'); |
| 76 | + $json = $input->getOption('json'); |
| 77 | + $this->basePath = $input->getOption('directory'); |
| 78 | + $this->verbose = !$json; |
| 79 | + $this->output = $output; |
| 80 | + $this->skipClasses = $input->getOption('skip-classes'); |
| 81 | + $this->skipMethods = $input->getOption('skip-methods'); |
| 82 | + |
| 83 | + // Set up excludes: |
| 84 | + if (!is_null($exclude)) { |
| 85 | + $this->exclude = array_map('trim', explode(',', $exclude)); |
| 86 | + } |
| 87 | + |
| 88 | + // Check base path ends with a slash: |
| 89 | + if (substr($this->basePath, -1) != '/') { |
| 90 | + $this->basePath .= '/'; |
| 91 | + } |
| 92 | + |
| 93 | + // Process: |
| 94 | + $this->processDirectory(); |
| 95 | + |
| 96 | + // Output JSON if requested: |
| 97 | + if ($json) { |
| 98 | + print json_encode($this->report); |
| 99 | + } |
| 100 | + |
| 101 | + return count($this->report) ? 1 : 0; |
| 102 | + } |
| 103 | + |
| 104 | + protected function processDirectory($path = '') |
| 105 | + { |
| 106 | + $dir = new DirectoryIterator($this->basePath . $path); |
| 107 | + |
| 108 | + foreach ($dir as $item) { |
| 109 | + if ($item->isDot()) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + $itemPath = $path . $item->getFilename(); |
| 114 | + |
| 115 | + if (in_array($itemPath, $this->exclude)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if ($item->isFile() && $item->getExtension() == 'php') { |
| 120 | + $this->processFile($itemPath); |
| 121 | + } |
| 122 | + |
| 123 | + if ($item->isDir()) { |
| 124 | + $this->processDirectory($itemPath . '/'); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + protected function processFile($file) |
| 130 | + { |
| 131 | + $stream = new PHP_Token_Stream($this->basePath . $file); |
| 132 | + |
| 133 | + foreach($stream->getClasses() as $name => $class) { |
| 134 | + $errors = false; |
| 135 | + |
| 136 | + if (!$this->skipClasses && is_null($class['docblock'])) { |
| 137 | + $errors = true; |
| 138 | + |
| 139 | + $this->report[] = array( |
| 140 | + 'type' => 'class', |
| 141 | + 'file' => $file, |
| 142 | + 'class' => $name, |
| 143 | + 'line' => $class['startLine'], |
| 144 | + ); |
| 145 | + |
| 146 | + if ($this->verbose) { |
| 147 | + $message = $class['file'] . ': ' . $class['startLine'] . ' - Class ' . $name . ' is missing a docblock.'; |
| 148 | + $this->output->writeln('<error>' . $message . '</error>'); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (!$this->skipMethods) { |
| 153 | + foreach ($class['methods'] as $methodName => $method) { |
| 154 | + if (is_null($method['docblock'])) { |
| 155 | + $errors = true; |
| 156 | + |
| 157 | + $this->report[] = array( |
| 158 | + 'type' => 'method', |
| 159 | + 'file' => $file, |
| 160 | + 'class' => $name, |
| 161 | + 'method' => $methodName, |
| 162 | + 'line' => $method['startLine'], |
| 163 | + ); |
| 164 | + |
| 165 | + if ($this->verbose) { |
| 166 | + $message = $class['file'] . ': ' . $method['startLine'] . ' - Method '.$name.'::'.$methodName.' is missing a docblock.'; |
| 167 | + $this->output->writeln('<error>' . $message . '</error>'); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (!$errors && $this->verbose) { |
| 174 | + $this->output->writeln($name . ' <info>OK</info>'); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + } |
| 180 | +} |
0 commit comments