Skip to content

Commit

Permalink
Finding out which files are excluded moved to FileExcluder
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 21, 2017
1 parent 802e663 commit 9c52617
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 42 deletions.
6 changes: 5 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ services:
-
class: PHPStan\Analyser\Analyser
arguments:
analyseExcludes: %excludes_analyse%
ignoreErrors: %ignoreErrors%
bootstrapFile: %bootstrap%

Expand Down Expand Up @@ -70,6 +69,11 @@ services:
arguments:
workingDirectory: %currentWorkingDirectory%

-
class: PHPStan\File\FileExcluder
arguments:
analyseExcludes: %excludes_analyse%

-
class: PHPStan\Parser\CachedParser
arguments:
Expand Down
47 changes: 10 additions & 37 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Analyser;

use PHPStan\Broker\Broker;
use PHPStan\File\FileExcluder;
use PHPStan\File\FileHelper;
use PHPStan\Parser\Parser;
use PHPStan\Rules\Registry;
Expand Down Expand Up @@ -36,11 +37,9 @@ class Analyser
private $printer;

/**
* Directories to exclude from analysing
*
* @var string[]
* @var \PHPStan\File\FileExcluder
*/
private $analyseExcludes;
private $fileExcluder;

/**
* @var string[]
Expand All @@ -52,7 +51,9 @@ class Analyser
*/
private $bootstrapFile;

/** @var \PHPStan\File\FileHelper */
/**
* @var \PHPStan\File\FileHelper
*/
private $fileHelper;

/**
Expand All @@ -61,7 +62,7 @@ class Analyser
* @param \PHPStan\Rules\Registry $registry
* @param \PHPStan\Analyser\NodeScopeResolver $nodeScopeResolver
* @param \PhpParser\PrettyPrinter\Standard $printer
* @param string[] $analyseExcludes
* @param \PHPStan\File\FileExcluder $fileExcluder
* @param string[] $ignoreErrors
* @param string|null $bootstrapFile
* @param \PHPStan\File\FileHelper $fileHelper
Expand All @@ -72,7 +73,7 @@ public function __construct(
Registry $registry,
NodeScopeResolver $nodeScopeResolver,
\PhpParser\PrettyPrinter\Standard $printer,
array $analyseExcludes,
FileExcluder $fileExcluder,
array $ignoreErrors,
string $bootstrapFile = null,
FileHelper $fileHelper
Expand All @@ -83,15 +84,7 @@ public function __construct(
$this->registry = $registry;
$this->nodeScopeResolver = $nodeScopeResolver;
$this->printer = $printer;
$this->analyseExcludes = array_map(function (string $exclude) use ($fileHelper): string {
$normalized = $fileHelper->normalizePath($exclude);

if ($this->isFnmatchPattern($normalized)) {
return $normalized;
}

return $fileHelper->absolutizePath($normalized);
}, $analyseExcludes);
$this->fileExcluder = $fileExcluder;
$this->ignoreErrors = $ignoreErrors;
$this->bootstrapFile = $bootstrapFile;
$this->fileHelper = $fileHelper;
Expand Down Expand Up @@ -136,7 +129,7 @@ public function analyse(array $files, bool $onlyFiles, \Closure $progressCallbac
$file = $this->fileHelper->normalizePath($file);

try {
if ($this->isExcludedFromAnalysing($file)) {
if ($this->fileExcluder->isExcludedFromAnalysing($file)) {
if ($progressCallback !== null) {
$progressCallback($file);
}
Expand Down Expand Up @@ -217,24 +210,4 @@ private function createErrors(\PhpParser\Node $node, string $file, array $messag
return $errors;
}

public function isExcludedFromAnalysing(string $file): bool
{
foreach ($this->analyseExcludes as $exclude) {
if (strpos($file, $exclude) === 0) {
return true;
}

if ($this->isFnmatchPattern($exclude) && fnmatch($exclude, $file, DIRECTORY_SEPARATOR === '\\' ? FNM_NOESCAPE : 0)) {
return true;
}
}

return false;
}

private function isFnmatchPattern(string $path): bool
{
return preg_match('~[*?[\]]~', $path) > 0;
}

}
51 changes: 51 additions & 0 deletions src/File/FileExcluder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace PHPStan\File;

class FileExcluder
{

/**
* Directories to exclude from analysing
*
* @var string[]
*/
private $analyseExcludes;

public function __construct(
FileHelper $fileHelper,
array $analyseExcludes
)
{
$this->analyseExcludes = array_map(function (string $exclude) use ($fileHelper): string {
$normalized = $fileHelper->normalizePath($exclude);

if ($this->isFnmatchPattern($normalized)) {
return $normalized;
}

return $fileHelper->absolutizePath($normalized);
}, $analyseExcludes);
}

public function isExcludedFromAnalysing(string $file): bool
{
foreach ($this->analyseExcludes as $exclude) {
if (strpos($file, $exclude) === 0) {
return true;
}

if ($this->isFnmatchPattern($exclude) && fnmatch($exclude, $file, DIRECTORY_SEPARATOR === '\\' ? FNM_NOESCAPE : 0)) {
return true;
}
}

return false;
}

private function isFnmatchPattern(string $path): bool
{
return preg_match('~[*?[\]]~', $path) > 0;
}

}
6 changes: 4 additions & 2 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Analyser;

use PHPStan\File\FileExcluder;
use PHPStan\File\FileHelper;
use PHPStan\Parser\DirectParser;
use PHPStan\Rules\AlwaysFailRule;
Expand Down Expand Up @@ -160,6 +161,7 @@ private function createAnalyser(

$broker = $this->createBroker();
$printer = new \PhpParser\PrettyPrinter\Standard();
$fileHelper = $this->getContainer()->getByType(FileHelper::class);
$analyser = new Analyser(
$broker,
new DirectParser(new \PhpParser\Parser\Php7(new \PhpParser\Lexer()), $traverser),
Expand All @@ -176,10 +178,10 @@ private function createAnalyser(
[]
),
$printer,
$analyseExcludes,
new FileExcluder($fileHelper, $analyseExcludes),
$ignoreErrors,
$bootstrapFile,
$this->getContainer()->getByType(FileHelper::class)
$fileHelper
);

return $analyser;
Expand Down
6 changes: 4 additions & 2 deletions tests/PHPStan/Rules/AbstractRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Error;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\File\FileExcluder;
use PHPStan\File\FileHelper;
use PHPStan\Type\FileTypeMapper;

Expand All @@ -31,6 +32,7 @@ private function getAnalyser(): Analyser

$broker = $this->createBroker();
$printer = new \PhpParser\PrettyPrinter\Standard();
$fileHelper = $this->getFileHelper();
$this->analyser = new Analyser(
$broker,
$this->getParser(),
Expand All @@ -47,10 +49,10 @@ private function getAnalyser(): Analyser
[]
),
$printer,
[],
new FileExcluder($fileHelper, []),
[],
null,
$this->getFileHelper()
$fileHelper
);
}

Expand Down

0 comments on commit 9c52617

Please sign in to comment.