Skip to content

Commit

Permalink
Merge pull request #31 from philwc/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
dancryer committed Apr 18, 2019
2 parents 3e915f6 + 7189c40 commit 1d62ee0
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 111 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"jakub-onderka/php-parallel-lint": "0.8.*",
"phpstan/phpstan": "^0.11.5",
"squizlabs/php_codesniffer": "^3.4",
"phperf/xh-tool": "^1.1"
"phperf/xh-tool": "^1.1",
"phpunit/phpunit": "^4.8"
},
"bin": [
"bin/phpdoccheck"
Expand All @@ -49,17 +50,20 @@
},
"scripts": {
"lint": "parallel-lint -e php --exclude vendor .",
"test": "phpunit",
"doccheck": "./bin/phpdoccheck",
"phpcs": "phpcs --extensions=php --cache=.phpcs-cache",
"static": "phpstan analyse src --level=7",
"check": [
"@lint",
"@test",
"@phpcs",
"@static"
]
},
"scripts-descriptions": {
"lint": "Run the php linter to check for valid PHP",
"test": "Run the phpunit tests",
"doccheck": "Run the docblock checker on all files in the project",
"phpcs": "Run the code sniffer on all files in the project",
"static": "Run phpstan on the src directory",
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
45 changes: 19 additions & 26 deletions src/Command/CheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

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


// Get files to check:
$files = FileProviderFactory::getFileProvider($config)->getFiles();
$files = FileProviderFactory::getFileProvider($config)->getFileIterator();

// Check files:
$filesPerLine = $config->getFilesPerLine();
$totalFiles = count($files);
$files = array_chunk($files, $filesPerLine);
$totalFiles = iterator_count($files);
//$files = array_chunk($files, $filesPerLine);
$processed = 0;

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

while (count($files)) {
$chunk = array_shift($files);
$chunkFiles = count($chunk);

while (count($chunk)) {
$processed++;
$file = array_shift($chunk);

$status = $fileChecker->checkFile($file);
$statusCollection->addFileStatus($status);

if ($config->isVerbose()) {
if ($status->hasErrors()) {
$output->write('<fg=red>F</>');
} elseif ($status->hasWarnings()) {
$output->write('<fg=yellow>W</>');
} else {
$output->write('<info>.</info>');
}
/** @var \SplFileInfo $file */
foreach ($files as $file) {
$processed++;

$status = $fileChecker->checkFile($file->getPathname());
$statusCollection->addFileStatus($status);

if ($config->isVerbose()) {
if ($status->hasErrors()) {
$output->write('<fg=red>F</>');
} elseif ($status->hasWarnings()) {
$output->write('<fg=yellow>W</>');
} else {
$output->write('<info>.</info>');
}
}

if ($config->isVerbose()) {
if ($processed % $config->getFilesPerLine() === 0 && $config->isVerbose()) {
$output->writeln(
sprintf(
'%s %s/%d (%d%%)',
str_pad('', $filesPerLine - $chunkFiles),
str_pad('', $filesPerLine - $processed),
str_pad((string)$processed, strlen((string)$totalFiles), ' ', STR_PAD_LEFT),
$totalFiles,
floor((100 / $totalFiles) * $processed)
Expand All @@ -199,7 +193,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}


if ($config->isVerbose()) {
$time = round(microtime(true) - $startTime, 2);
$output->writeln('');
Expand Down
16 changes: 10 additions & 6 deletions src/DocblockParser/TagCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ public function hasTag($name)
*/
public function getParamTags()
{
return array_filter($this->tags, static function (Tag $tag) {
return $tag instanceof ParamTag;
});
return array_values(
array_filter($this->tags, static function (Tag $tag) {
return $tag instanceof ParamTag;
})
);
}

/**
* @return ReturnTag[]
*/
public function getReturnTags()
{
return array_filter($this->tags, static function (Tag $tag) {
return $tag instanceof ReturnTag;
});
return array_values(
array_filter($this->tags, static function (Tag $tag) {
return $tag instanceof ReturnTag;
})
);
}
}
1 change: 0 additions & 1 deletion src/FileParser/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PhpDocBlockChecker\FileParser;

use Exception;
use PhpDocBlockChecker\DocblockParser\DocblockParser;
use PhpDocBlockChecker\DocblockParser\ReturnTag;
use PhpDocBlockChecker\FileInfo;
Expand Down
29 changes: 8 additions & 21 deletions src/FileProvider/DirectoryFileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace PhpDocBlockChecker\FileProvider;

use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;

class DirectoryFileProvider extends FileProvider
class DirectoryFileProvider implements FileProviderInterface
{
private $excludes;
/**
* @var string
*/
Expand All @@ -26,26 +26,13 @@ public function __construct($directory, array $excludes)
}

/**
* @return string[]
* @return \Iterator
*/
public function getFiles()
public function getFileIterator()
{
$directory = new RecursiveDirectoryIterator($this->directory);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
$worklist = [];

foreach ($regex as $match) {
if (!isset($match[0])) {
continue;
}
$file = $match[0];
$directory = new RecursiveDirectoryIterator($this->directory, FilesystemIterator::SKIP_DOTS);

if (!$this->isFileExcluded(str_replace($this->directory, '', $file))) {
$worklist[] = $file;
}
}

return $worklist;
$iterator = new RecursiveIteratorIterator($directory);
return new FileExclusionFilter($iterator, $this->directory, $this->excludes);
}
}
38 changes: 38 additions & 0 deletions src/FileProvider/ExcludeFileTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace PhpDocBlockChecker\FileProvider;

trait ExcludeFileTrait
{
/**
* @var array
*/
protected $excludes = [];

/**
* @param string $baseDirectory
* @param \SplFileInfo $file
* @return bool
*/
protected function isFileExcluded($baseDirectory, \SplFileInfo $file)
{
if ($file->getExtension() !== 'php') {
return true;
}

$filePath = str_replace($baseDirectory, '', $file->getPathname());

if (in_array($filePath, $this->excludes, true)) {
return true;
}

foreach ($this->excludes as $pattern) {
if (fnmatch($pattern, $filePath)) {
return true;
}
}

return false;
}
}
39 changes: 39 additions & 0 deletions src/FileProvider/FileExclusionFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace PhpDocBlockChecker\FileProvider;

use Iterator;

class FileExclusionFilter extends \FilterIterator
{
use ExcludeFileTrait;
/**
* @var string
*/
private $baseDirectory;

/**
* FileExclusionFilter constructor.
* @param Iterator $iterator
* @param string $baseDirectory
* @param array $excludes
*/
public function __construct(Iterator $iterator, $baseDirectory, array $excludes)
{
parent::__construct($iterator);
$this->baseDirectory = $baseDirectory;
$this->excludes = $excludes;
}

/**
* Check whether the current element of the iterator is acceptable
* @link https://php.net/manual/en/filteriterator.accept.php
* @return bool true if the current element is acceptable, otherwise false.
* @since 5.1.0
*/
public function accept()
{
$file = $this->getInnerIterator()->current();
return !$this->isFileExcluded($this->baseDirectory, $file);
}
}
30 changes: 0 additions & 30 deletions src/FileProvider/FileProvider.php

This file was deleted.

7 changes: 6 additions & 1 deletion src/FileProvider/FileProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpDocBlockChecker\FileProvider;

use PhpDocBlockChecker\Config\Config;
use RuntimeException;

class FileProviderFactory
{
Expand All @@ -13,7 +14,11 @@ class FileProviderFactory
public static function getFileProvider(Config $config)
{
if ($config->isFromStdin()) {
return new StdinFileProvider($config->getExclude());
$handle = fopen('php://stdin', 'rb');
if ($handle === false) {
throw new RuntimeException('Unable to open stdin for reading');
}
return new StdinFileProvider($handle, $config->getExclude());
}
return new DirectoryFileProvider($config->getDirectory(), $config->getExclude());
}
Expand Down
4 changes: 2 additions & 2 deletions src/FileProvider/FileProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface FileProviderInterface
{
/**
* @return string[]
* @return \Traversable
*/
public function getFiles();
public function getFileIterator();
}
Loading

0 comments on commit 1d62ee0

Please sign in to comment.