Skip to content

Commit

Permalink
Config option excludePaths that allows different lists for analyse an…
Browse files Browse the repository at this point in the history
…d analyse+scan
  • Loading branch information
ondrejmirtes committed Jan 5, 2021
1 parent 377e49c commit bf35a10
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 5 deletions.
50 changes: 47 additions & 3 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parameters:
- ../stubs/runtime/ReflectionUnionType.php
- ../stubs/runtime/Attribute.php
excludes_analyse: []
excludePaths: null
autoload_directories: []
autoload_files: []
level: null
Expand Down Expand Up @@ -139,6 +140,19 @@ parametersSchema:
bootstrap: schema(string(), nullable())
bootstrapFiles: listOf(string())
excludes_analyse: listOf(string())
excludePaths: schema(anyOf(
listOf(string()),
structure([
analyse: listOf(string()),
]),
structure([
analyseAndScan: listOf(string()),
])
structure([
analyse: listOf(string()),
analyseAndScan: listOf(string())
])
), nullable())
autoload_directories: listOf(string())
autoload_files: listOf(string())
level: schema(anyOf(int(), string()), nullable())
Expand Down Expand Up @@ -425,6 +439,8 @@ services:

-
class: PHPStan\Dependency\DependencyDumper
arguments:
fileFinder: @fileFinderAnalyse

-
class: PHPStan\Dependency\DependencyResolver
Expand Down Expand Up @@ -479,18 +495,44 @@ services:
workingDirectory: %currentWorkingDirectory%

-
class: PHPStan\File\FileExcluder
class: PHPStan\File\FileExcluderFactory
arguments:
analyseExcludes: %excludes_analyse%
stubFiles: %stubFiles%
obsoleteExcludesAnalyse: %excludes_analyse%
excludePaths: %excludePaths%

-
implement: PHPStan\File\FileExcluderRawFactory
arguments:
stubFiles: %stubFiles%

fileExcluderAnalyse:
class: PHPStan\File\FileExcluder
factory: @PHPStan\File\FileExcluderFactory::createAnalyseFileExcluder()
autowired: false

fileExcluderScan:
class: PHPStan\File\FileExcluder
factory: @PHPStan\File\FileExcluderFactory::createAnalyseFileExcluder()
autowired: false

fileFinderAnalyse:
class: PHPStan\File\FileFinder
arguments:
fileExcluder: @fileExcluderAnalyse
fileExtensions: %fileExtensions%
autowired: false

fileFinderScan:
class: PHPStan\File\FileFinder
arguments:
fileExcluder: @fileExcluderScan
fileExtensions: %fileExtensions%
autowired: false

-
class: PHPStan\File\FileMonitor
arguments:
fileFinder: @fileFinderAnalyse

-
class: PHPStan\NodeVisitor\StatementOrderVisitor
Expand Down Expand Up @@ -544,6 +586,8 @@ services:

-
implement: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory
arguments:
fileFinder: @fileFinderScan

-
class: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ parameters:
count: 1
path: src/DependencyInjection/ParametersSchemaExtension.php

-
message: "#^Parameter \\#1 \\$analyseExcludes of method PHPStan\\\\File\\\\FileExcluderRawFactory\\:\\:create\\(\\) expects array\\<string\\>, array\\<int\\|string, array\\<int, string\\>\\|string\\> given\\.$#"
count: 2
path: src/File/FileExcluderFactory.php

-
message: "#^Parameter \\#1 \\$input of function array_unique expects array, array\\<int, string\\>\\|string given\\.$#"
count: 2
path: src/File/FileExcluderFactory.php

-
message: "#^Variable method call on PHPStan\\\\Reflection\\\\ClassReflection\\.$#"
count: 2
Expand Down
13 changes: 12 additions & 1 deletion src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,22 @@ public static function begin(
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}

$excludesAnalyse = $container->getParameter('excludes_analyse');
$excludePaths = $container->getParameter('excludePaths');
if (count($excludesAnalyse) > 0 && $excludePaths !== null) {
$errorOutput->writeLineFormatted(sprintf('Configuration parameters <fg=cyan>excludes_analyse</> and <fg=cyan>excludePaths</> cannot be used at the same time.'));
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted(sprintf('Parameter <fg=cyan>excludes_analyse</> has been deprecated so use <fg=cyan>excludePaths</> only from now on.'));
$errorOutput->writeLineFormatted('');

throw new \PHPStan\Command\InceptionNotSuccessfulException();
}

$tempResultCachePath = $container->getParameter('tempResultCachePath');
$createDir($tempResultCachePath);

/** @var FileFinder $fileFinder */
$fileFinder = $container->getByType(FileFinder::class);
$fileFinder = $container->getService('fileFinderAnalyse');

/** @var \Closure(): (array{string[], bool}) $filesCallback */
$filesCallback = static function () use ($fileFinder, $paths): array {
Expand Down
5 changes: 4 additions & 1 deletion src/DependencyInjection/NeonAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class NeonAdapter implements Adapter
{

public const CACHE_KEY = 'v10';
public const CACHE_KEY = 'v11-excludePaths';

private const PREVENT_MERGING_SUFFIX = '!';

Expand Down Expand Up @@ -92,6 +92,9 @@ public function process(array $arr, string $fileKey, string $file): array
'[parameters][autoload_directories][]',
'[parameters][paths][]',
'[parameters][excludes_analyse][]',
'[parameters][excludePaths][]',
'[parameters][excludePaths][analyse][]',
'[parameters][excludePaths][analyseAndScan][]',
'[parameters][ignoreErrors][][paths][]',
'[parameters][ignoreErrors][][path]',
'[parameters][bootstrap]',
Expand Down
71 changes: 71 additions & 0 deletions src/File/FileExcluderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);

namespace PHPStan\File;

class FileExcluderFactory
{

private FileExcluderRawFactory $fileExcluderRawFactory;

/** @var string[] */
private array $obsoleteExcludesAnalyse;

/** @var array<int, string>|array{analyse?: array<int, string>, analyseAndScan?: array<int, string>}|null */
private ?array $excludePaths;

/**
* @param FileExcluderRawFactory $fileExcluderRawFactory
* @param string[] $obsoleteExcludesAnalyse
* @param array<int, string>|array{analyse?: array<int, string>, analyseAndScan?: array<int, string>}|null $excludePaths
*/
public function __construct(
FileExcluderRawFactory $fileExcluderRawFactory,
array $obsoleteExcludesAnalyse,
?array $excludePaths
)
{
$this->fileExcluderRawFactory = $fileExcluderRawFactory;
$this->obsoleteExcludesAnalyse = $obsoleteExcludesAnalyse;
$this->excludePaths = $excludePaths;
}

public function createAnalyseFileExcluder(): FileExcluder
{
if ($this->excludePaths === null) {
return $this->fileExcluderRawFactory->create($this->obsoleteExcludesAnalyse);
}

if (!array_key_exists('analyse', $this->excludePaths) && !array_key_exists('analyseAndScan', $this->excludePaths)) {
return $this->fileExcluderRawFactory->create($this->excludePaths);
}

$paths = [];
if (array_key_exists('analyse', $this->excludePaths)) {
$paths = $this->excludePaths['analyse'];
}
if (array_key_exists('analyseAndScan', $this->excludePaths)) {
$paths = $this->excludePaths['analyseAndScan'];
}

return $this->fileExcluderRawFactory->create(array_values(array_unique($paths)));
}

public function createScanFileExcluder(): FileExcluder
{
if ($this->excludePaths === null) {
return $this->fileExcluderRawFactory->create($this->obsoleteExcludesAnalyse);
}

if (!array_key_exists('analyse', $this->excludePaths) && !array_key_exists('analyseAndScan', $this->excludePaths)) {
return $this->fileExcluderRawFactory->create($this->excludePaths);
}

$paths = [];
if (array_key_exists('analyseAndScan', $this->excludePaths)) {
$paths = $this->excludePaths['analyseAndScan'];
}

return $this->fileExcluderRawFactory->create(array_values(array_unique($paths)));
}

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

namespace PHPStan\File;

interface FileExcluderRawFactory
{

/**
* @param string[] $analyseExcludes
* @return FileExcluder
*/
public function create(
array $analyseExcludes
): FileExcluder;

}

0 comments on commit bf35a10

Please sign in to comment.