Skip to content

Commit bf35a10

Browse files
committed
Config option excludePaths that allows different lists for analyse and analyse+scan
1 parent 377e49c commit bf35a10

File tree

6 files changed

+160
-5
lines changed

6 files changed

+160
-5
lines changed

conf/config.neon

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
- ../stubs/runtime/ReflectionUnionType.php
77
- ../stubs/runtime/Attribute.php
88
excludes_analyse: []
9+
excludePaths: null
910
autoload_directories: []
1011
autoload_files: []
1112
level: null
@@ -139,6 +140,19 @@ parametersSchema:
139140
bootstrap: schema(string(), nullable())
140141
bootstrapFiles: listOf(string())
141142
excludes_analyse: listOf(string())
143+
excludePaths: schema(anyOf(
144+
listOf(string()),
145+
structure([
146+
analyse: listOf(string()),
147+
]),
148+
structure([
149+
analyseAndScan: listOf(string()),
150+
])
151+
structure([
152+
analyse: listOf(string()),
153+
analyseAndScan: listOf(string())
154+
])
155+
), nullable())
142156
autoload_directories: listOf(string())
143157
autoload_files: listOf(string())
144158
level: schema(anyOf(int(), string()), nullable())
@@ -425,6 +439,8 @@ services:
425439

426440
-
427441
class: PHPStan\Dependency\DependencyDumper
442+
arguments:
443+
fileFinder: @fileFinderAnalyse
428444

429445
-
430446
class: PHPStan\Dependency\DependencyResolver
@@ -479,18 +495,44 @@ services:
479495
workingDirectory: %currentWorkingDirectory%
480496

481497
-
482-
class: PHPStan\File\FileExcluder
498+
class: PHPStan\File\FileExcluderFactory
483499
arguments:
484-
analyseExcludes: %excludes_analyse%
485-
stubFiles: %stubFiles%
500+
obsoleteExcludesAnalyse: %excludes_analyse%
501+
excludePaths: %excludePaths%
486502

487503
-
504+
implement: PHPStan\File\FileExcluderRawFactory
505+
arguments:
506+
stubFiles: %stubFiles%
507+
508+
fileExcluderAnalyse:
509+
class: PHPStan\File\FileExcluder
510+
factory: @PHPStan\File\FileExcluderFactory::createAnalyseFileExcluder()
511+
autowired: false
512+
513+
fileExcluderScan:
514+
class: PHPStan\File\FileExcluder
515+
factory: @PHPStan\File\FileExcluderFactory::createAnalyseFileExcluder()
516+
autowired: false
517+
518+
fileFinderAnalyse:
519+
class: PHPStan\File\FileFinder
520+
arguments:
521+
fileExcluder: @fileExcluderAnalyse
522+
fileExtensions: %fileExtensions%
523+
autowired: false
524+
525+
fileFinderScan:
488526
class: PHPStan\File\FileFinder
489527
arguments:
528+
fileExcluder: @fileExcluderScan
490529
fileExtensions: %fileExtensions%
530+
autowired: false
491531

492532
-
493533
class: PHPStan\File\FileMonitor
534+
arguments:
535+
fileFinder: @fileFinderAnalyse
494536

495537
-
496538
class: PHPStan\NodeVisitor\StatementOrderVisitor
@@ -544,6 +586,8 @@ services:
544586

545587
-
546588
implement: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory
589+
arguments:
590+
fileFinder: @fileFinderScan
547591

548592
-
549593
class: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository

phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ parameters:
7575
count: 1
7676
path: src/DependencyInjection/ParametersSchemaExtension.php
7777

78+
-
79+
message: "#^Parameter \\#1 \\$analyseExcludes of method PHPStan\\\\File\\\\FileExcluderRawFactory\\:\\:create\\(\\) expects array\\<string\\>, array\\<int\\|string, array\\<int, string\\>\\|string\\> given\\.$#"
80+
count: 2
81+
path: src/File/FileExcluderFactory.php
82+
83+
-
84+
message: "#^Parameter \\#1 \\$input of function array_unique expects array, array\\<int, string\\>\\|string given\\.$#"
85+
count: 2
86+
path: src/File/FileExcluderFactory.php
87+
7888
-
7989
message: "#^Variable method call on PHPStan\\\\Reflection\\\\ClassReflection\\.$#"
8090
count: 2

src/Command/CommandHelper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,22 @@ public static function begin(
393393
throw new \PHPStan\Command\InceptionNotSuccessfulException();
394394
}
395395

396+
$excludesAnalyse = $container->getParameter('excludes_analyse');
397+
$excludePaths = $container->getParameter('excludePaths');
398+
if (count($excludesAnalyse) > 0 && $excludePaths !== null) {
399+
$errorOutput->writeLineFormatted(sprintf('Configuration parameters <fg=cyan>excludes_analyse</> and <fg=cyan>excludePaths</> cannot be used at the same time.'));
400+
$errorOutput->writeLineFormatted('');
401+
$errorOutput->writeLineFormatted(sprintf('Parameter <fg=cyan>excludes_analyse</> has been deprecated so use <fg=cyan>excludePaths</> only from now on.'));
402+
$errorOutput->writeLineFormatted('');
403+
404+
throw new \PHPStan\Command\InceptionNotSuccessfulException();
405+
}
406+
396407
$tempResultCachePath = $container->getParameter('tempResultCachePath');
397408
$createDir($tempResultCachePath);
398409

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

402413
/** @var \Closure(): (array{string[], bool}) $filesCallback */
403414
$filesCallback = static function () use ($fileFinder, $paths): array {

src/DependencyInjection/NeonAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class NeonAdapter implements Adapter
1515
{
1616

17-
public const CACHE_KEY = 'v10';
17+
public const CACHE_KEY = 'v11-excludePaths';
1818

1919
private const PREVENT_MERGING_SUFFIX = '!';
2020

@@ -92,6 +92,9 @@ public function process(array $arr, string $fileKey, string $file): array
9292
'[parameters][autoload_directories][]',
9393
'[parameters][paths][]',
9494
'[parameters][excludes_analyse][]',
95+
'[parameters][excludePaths][]',
96+
'[parameters][excludePaths][analyse][]',
97+
'[parameters][excludePaths][analyseAndScan][]',
9598
'[parameters][ignoreErrors][][paths][]',
9699
'[parameters][ignoreErrors][][path]',
97100
'[parameters][bootstrap]',

src/File/FileExcluderFactory.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
class FileExcluderFactory
6+
{
7+
8+
private FileExcluderRawFactory $fileExcluderRawFactory;
9+
10+
/** @var string[] */
11+
private array $obsoleteExcludesAnalyse;
12+
13+
/** @var array<int, string>|array{analyse?: array<int, string>, analyseAndScan?: array<int, string>}|null */
14+
private ?array $excludePaths;
15+
16+
/**
17+
* @param FileExcluderRawFactory $fileExcluderRawFactory
18+
* @param string[] $obsoleteExcludesAnalyse
19+
* @param array<int, string>|array{analyse?: array<int, string>, analyseAndScan?: array<int, string>}|null $excludePaths
20+
*/
21+
public function __construct(
22+
FileExcluderRawFactory $fileExcluderRawFactory,
23+
array $obsoleteExcludesAnalyse,
24+
?array $excludePaths
25+
)
26+
{
27+
$this->fileExcluderRawFactory = $fileExcluderRawFactory;
28+
$this->obsoleteExcludesAnalyse = $obsoleteExcludesAnalyse;
29+
$this->excludePaths = $excludePaths;
30+
}
31+
32+
public function createAnalyseFileExcluder(): FileExcluder
33+
{
34+
if ($this->excludePaths === null) {
35+
return $this->fileExcluderRawFactory->create($this->obsoleteExcludesAnalyse);
36+
}
37+
38+
if (!array_key_exists('analyse', $this->excludePaths) && !array_key_exists('analyseAndScan', $this->excludePaths)) {
39+
return $this->fileExcluderRawFactory->create($this->excludePaths);
40+
}
41+
42+
$paths = [];
43+
if (array_key_exists('analyse', $this->excludePaths)) {
44+
$paths = $this->excludePaths['analyse'];
45+
}
46+
if (array_key_exists('analyseAndScan', $this->excludePaths)) {
47+
$paths = $this->excludePaths['analyseAndScan'];
48+
}
49+
50+
return $this->fileExcluderRawFactory->create(array_values(array_unique($paths)));
51+
}
52+
53+
public function createScanFileExcluder(): FileExcluder
54+
{
55+
if ($this->excludePaths === null) {
56+
return $this->fileExcluderRawFactory->create($this->obsoleteExcludesAnalyse);
57+
}
58+
59+
if (!array_key_exists('analyse', $this->excludePaths) && !array_key_exists('analyseAndScan', $this->excludePaths)) {
60+
return $this->fileExcluderRawFactory->create($this->excludePaths);
61+
}
62+
63+
$paths = [];
64+
if (array_key_exists('analyseAndScan', $this->excludePaths)) {
65+
$paths = $this->excludePaths['analyseAndScan'];
66+
}
67+
68+
return $this->fileExcluderRawFactory->create(array_values(array_unique($paths)));
69+
}
70+
71+
}

src/File/FileExcluderRawFactory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
interface FileExcluderRawFactory
6+
{
7+
8+
/**
9+
* @param string[] $analyseExcludes
10+
* @return FileExcluder
11+
*/
12+
public function create(
13+
array $analyseExcludes
14+
): FileExcluder;
15+
16+
}

0 commit comments

Comments
 (0)