Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit 1d62ee0

Browse files
authored
Merge pull request #31 from philwc/add-tests
Add tests
2 parents 3e915f6 + 7189c40 commit 1d62ee0

20 files changed

+682
-111
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"jakub-onderka/php-parallel-lint": "0.8.*",
3838
"phpstan/phpstan": "^0.11.5",
3939
"squizlabs/php_codesniffer": "^3.4",
40-
"phperf/xh-tool": "^1.1"
40+
"phperf/xh-tool": "^1.1",
41+
"phpunit/phpunit": "^4.8"
4142
},
4243
"bin": [
4344
"bin/phpdoccheck"
@@ -49,17 +50,20 @@
4950
},
5051
"scripts": {
5152
"lint": "parallel-lint -e php --exclude vendor .",
53+
"test": "phpunit",
5254
"doccheck": "./bin/phpdoccheck",
5355
"phpcs": "phpcs --extensions=php --cache=.phpcs-cache",
5456
"static": "phpstan analyse src --level=7",
5557
"check": [
5658
"@lint",
59+
"@test",
5760
"@phpcs",
5861
"@static"
5962
]
6063
},
6164
"scripts-descriptions": {
6265
"lint": "Run the php linter to check for valid PHP",
66+
"test": "Run the phpunit tests",
6367
"doccheck": "Run the docblock checker on all files in the project",
6468
"phpcs": "Run the code sniffer on all files in the project",
6569
"static": "Run phpstan on the src directory",

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
beStrictAboutCoversAnnotation="true"
6+
beStrictAboutOutputDuringTests="true"
7+
beStrictAboutTodoAnnotatedTests="true"
8+
verbose="true">
9+
<testsuite name="default">
10+
<directory suffix="Test.php">tests</directory>
11+
</testsuite>
12+
13+
<filter>
14+
<whitelist processUncoveredFilesFromWhitelist="true">
15+
<directory suffix=".php">src</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

src/Command/CheckerCommand.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
137137

138138
$config = (new ConfigProcessor(new ConfigParser($input, $this->getDefinition())))->processConfig();
139139

140-
141140
// Get files to check:
142-
$files = FileProviderFactory::getFileProvider($config)->getFiles();
141+
$files = FileProviderFactory::getFileProvider($config)->getFileIterator();
143142

144143
// Check files:
145144
$filesPerLine = $config->getFilesPerLine();
146-
$totalFiles = count($files);
147-
$files = array_chunk($files, $filesPerLine);
145+
$totalFiles = iterator_count($files);
146+
//$files = array_chunk($files, $filesPerLine);
148147
$processed = 0;
149148

150149
$fileChecker = new FileChecker(
@@ -164,33 +163,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
164163
$output->writeln('');
165164
}
166165

167-
while (count($files)) {
168-
$chunk = array_shift($files);
169-
$chunkFiles = count($chunk);
170-
171-
while (count($chunk)) {
172-
$processed++;
173-
$file = array_shift($chunk);
174-
175-
$status = $fileChecker->checkFile($file);
176-
$statusCollection->addFileStatus($status);
177-
178-
if ($config->isVerbose()) {
179-
if ($status->hasErrors()) {
180-
$output->write('<fg=red>F</>');
181-
} elseif ($status->hasWarnings()) {
182-
$output->write('<fg=yellow>W</>');
183-
} else {
184-
$output->write('<info>.</info>');
185-
}
166+
/** @var \SplFileInfo $file */
167+
foreach ($files as $file) {
168+
$processed++;
169+
170+
$status = $fileChecker->checkFile($file->getPathname());
171+
$statusCollection->addFileStatus($status);
172+
173+
if ($config->isVerbose()) {
174+
if ($status->hasErrors()) {
175+
$output->write('<fg=red>F</>');
176+
} elseif ($status->hasWarnings()) {
177+
$output->write('<fg=yellow>W</>');
178+
} else {
179+
$output->write('<info>.</info>');
186180
}
187181
}
188182

189-
if ($config->isVerbose()) {
183+
if ($processed % $config->getFilesPerLine() === 0 && $config->isVerbose()) {
190184
$output->writeln(
191185
sprintf(
192186
'%s %s/%d (%d%%)',
193-
str_pad('', $filesPerLine - $chunkFiles),
187+
str_pad('', $filesPerLine - $processed),
194188
str_pad((string)$processed, strlen((string)$totalFiles), ' ', STR_PAD_LEFT),
195189
$totalFiles,
196190
floor((100 / $totalFiles) * $processed)
@@ -199,7 +193,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
199193
}
200194
}
201195

202-
203196
if ($config->isVerbose()) {
204197
$time = round(microtime(true) - $startTime, 2);
205198
$output->writeln('');

src/DocblockParser/TagCollection.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,22 @@ public function hasTag($name)
4848
*/
4949
public function getParamTags()
5050
{
51-
return array_filter($this->tags, static function (Tag $tag) {
52-
return $tag instanceof ParamTag;
53-
});
51+
return array_values(
52+
array_filter($this->tags, static function (Tag $tag) {
53+
return $tag instanceof ParamTag;
54+
})
55+
);
5456
}
5557

5658
/**
5759
* @return ReturnTag[]
5860
*/
5961
public function getReturnTags()
6062
{
61-
return array_filter($this->tags, static function (Tag $tag) {
62-
return $tag instanceof ReturnTag;
63-
});
63+
return array_values(
64+
array_filter($this->tags, static function (Tag $tag) {
65+
return $tag instanceof ReturnTag;
66+
})
67+
);
6468
}
6569
}

src/FileParser/FileParser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PhpDocBlockChecker\FileParser;
44

5-
use Exception;
65
use PhpDocBlockChecker\DocblockParser\DocblockParser;
76
use PhpDocBlockChecker\DocblockParser\ReturnTag;
87
use PhpDocBlockChecker\FileInfo;

src/FileProvider/DirectoryFileProvider.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace PhpDocBlockChecker\FileProvider;
44

5+
use FilesystemIterator;
56
use RecursiveDirectoryIterator;
67
use RecursiveIteratorIterator;
7-
use RecursiveRegexIterator;
8-
use RegexIterator;
98

10-
class DirectoryFileProvider extends FileProvider
9+
class DirectoryFileProvider implements FileProviderInterface
1110
{
11+
private $excludes;
1212
/**
1313
* @var string
1414
*/
@@ -26,26 +26,13 @@ public function __construct($directory, array $excludes)
2626
}
2727

2828
/**
29-
* @return string[]
29+
* @return \Iterator
3030
*/
31-
public function getFiles()
31+
public function getFileIterator()
3232
{
33-
$directory = new RecursiveDirectoryIterator($this->directory);
34-
$iterator = new RecursiveIteratorIterator($directory);
35-
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
36-
$worklist = [];
37-
38-
foreach ($regex as $match) {
39-
if (!isset($match[0])) {
40-
continue;
41-
}
42-
$file = $match[0];
33+
$directory = new RecursiveDirectoryIterator($this->directory, FilesystemIterator::SKIP_DOTS);
4334

44-
if (!$this->isFileExcluded(str_replace($this->directory, '', $file))) {
45-
$worklist[] = $file;
46-
}
47-
}
48-
49-
return $worklist;
35+
$iterator = new RecursiveIteratorIterator($directory);
36+
return new FileExclusionFilter($iterator, $this->directory, $this->excludes);
5037
}
5138
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
4+
namespace PhpDocBlockChecker\FileProvider;
5+
6+
trait ExcludeFileTrait
7+
{
8+
/**
9+
* @var array
10+
*/
11+
protected $excludes = [];
12+
13+
/**
14+
* @param string $baseDirectory
15+
* @param \SplFileInfo $file
16+
* @return bool
17+
*/
18+
protected function isFileExcluded($baseDirectory, \SplFileInfo $file)
19+
{
20+
if ($file->getExtension() !== 'php') {
21+
return true;
22+
}
23+
24+
$filePath = str_replace($baseDirectory, '', $file->getPathname());
25+
26+
if (in_array($filePath, $this->excludes, true)) {
27+
return true;
28+
}
29+
30+
foreach ($this->excludes as $pattern) {
31+
if (fnmatch($pattern, $filePath)) {
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PhpDocBlockChecker\FileProvider;
4+
5+
use Iterator;
6+
7+
class FileExclusionFilter extends \FilterIterator
8+
{
9+
use ExcludeFileTrait;
10+
/**
11+
* @var string
12+
*/
13+
private $baseDirectory;
14+
15+
/**
16+
* FileExclusionFilter constructor.
17+
* @param Iterator $iterator
18+
* @param string $baseDirectory
19+
* @param array $excludes
20+
*/
21+
public function __construct(Iterator $iterator, $baseDirectory, array $excludes)
22+
{
23+
parent::__construct($iterator);
24+
$this->baseDirectory = $baseDirectory;
25+
$this->excludes = $excludes;
26+
}
27+
28+
/**
29+
* Check whether the current element of the iterator is acceptable
30+
* @link https://php.net/manual/en/filteriterator.accept.php
31+
* @return bool true if the current element is acceptable, otherwise false.
32+
* @since 5.1.0
33+
*/
34+
public function accept()
35+
{
36+
$file = $this->getInnerIterator()->current();
37+
return !$this->isFileExcluded($this->baseDirectory, $file);
38+
}
39+
}

src/FileProvider/FileProvider.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/FileProvider/FileProviderFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpDocBlockChecker\FileProvider;
44

55
use PhpDocBlockChecker\Config\Config;
6+
use RuntimeException;
67

78
class FileProviderFactory
89
{
@@ -13,7 +14,11 @@ class FileProviderFactory
1314
public static function getFileProvider(Config $config)
1415
{
1516
if ($config->isFromStdin()) {
16-
return new StdinFileProvider($config->getExclude());
17+
$handle = fopen('php://stdin', 'rb');
18+
if ($handle === false) {
19+
throw new RuntimeException('Unable to open stdin for reading');
20+
}
21+
return new StdinFileProvider($handle, $config->getExclude());
1722
}
1823
return new DirectoryFileProvider($config->getDirectory(), $config->getExclude());
1924
}

0 commit comments

Comments
 (0)