Skip to content

Commit 6aa9a9f

Browse files
author
Dan Cryer
committed
Initial commit.
0 parents  commit 6aa9a9f

File tree

7 files changed

+420
-0
lines changed

7 files changed

+420
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
vendor

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Copyright (c) 2014, Block 8 Limited
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Symfony\Component\Console\Application;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
15+
/**
16+
* Extension of Symfony's console application class to allow us to have a single, default command.
17+
* @package PhpDocblockChecker
18+
*/
19+
class CheckerApplication extends Application
20+
{
21+
/**
22+
* Override the default command name logic and return our check command.
23+
* @param InputInterface $input
24+
* @return string
25+
*/
26+
protected function getCommandName(InputInterface $input)
27+
{
28+
return 'check';
29+
}
30+
31+
/**
32+
* Overridden so that the application doesn't expect the command
33+
* name to be the first argument.
34+
*/
35+
public function getDefinition()
36+
{
37+
$inputDefinition = parent::getDefinition();
38+
// clear out the normal first argument, which is the command name
39+
$inputDefinition->setArguments();
40+
41+
return $inputDefinition;
42+
}
43+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
}

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name" : "block8/php-docblock-checker",
3+
"description" : "A simple tool for checking that your PHP classes and methods use docblocks.",
4+
"minimum-stability": "stable",
5+
"type" : "library",
6+
"keywords" : ["php", "phpci", "testing", "docblock", "comment", "checker", "code quality"],
7+
"homepage" : "https://www.phptesting.org/",
8+
"license" : "BSD-2-Clause",
9+
10+
"authors": [
11+
{
12+
"name" : "Dan Cryer",
13+
"email" : "dan.cryer@block8.co.uk",
14+
"homepage": "http://www.block8.co.uk",
15+
"role" : "Developer"
16+
}
17+
],
18+
19+
"support": {
20+
"issues": "https://github.com/Block8/php-docblock-checker/issues",
21+
"source": "https://github.com/Block8/php-docblock-checker"
22+
},
23+
24+
"require": {
25+
"php": ">=5.3.3",
26+
"phpunit/php-token-stream": "1.*",
27+
"symfony/console": "~2.1"
28+
},
29+
30+
"bin": [
31+
"phpdoccheck"
32+
],
33+
34+
"config": {
35+
"bin-dir": "bin"
36+
}
37+
}

0 commit comments

Comments
 (0)