Skip to content

Commit e169fde

Browse files
authored
ResultCache: allow customization of params not invalidating cache
1 parent 54159bb commit e169fde

File tree

5 files changed

+110
-12
lines changed

5 files changed

+110
-12
lines changed

Diff for: conf/config.neon

+14
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ parameters:
189189
- '1.1.1.2'
190190
tmpDir: %sysGetTempDir%/phpstan-fixer
191191
__validate: true
192+
parametersNotInvalidatingCache:
193+
- parameters.editorUrl
194+
- parameters.editorUrlTitle
195+
- parameters.errorFormat
196+
- parameters.ignoreErrors
197+
- parameters.reportUnmatchedIgnoredErrors
198+
- parameters.tipsOfTheDay
199+
- parameters.parallel
200+
- parameters.internalErrorsCountLimit
201+
- parameters.cache
202+
- parameters.memoryLimitFile
203+
- parameters.pro
204+
- parametersSchema
192205

193206
extensions:
194207
rules: PHPStan\DependencyInjection\RulesExtension
@@ -502,6 +515,7 @@ services:
502515
scanFiles: %scanFiles%
503516
scanDirectories: %scanDirectories%
504517
checkDependenciesOfProjectExtensionFiles: %resultCacheChecksProjectExtensionFilesDependencies%
518+
parametersNotInvalidatingCache: %parametersNotInvalidatingCache%
505519

506520
-
507521
class: PHPStan\Analyser\ResultCache\ResultCacheClearer

Diff for: conf/parametersSchema.neon

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ parametersSchema:
166166
])
167167
env: arrayOf(string(), anyOf(int(), string()))
168168
sysGetTempDir: string()
169+
parametersNotInvalidatingCache: listOf(string())
169170

170171
# playground mode
171172
sourceLocatorPlaygroundMode: bool()

Diff for: src/Analyser/ResultCache/ResultCacheManager.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\File\FileFinder;
1717
use PHPStan\File\FileHelper;
1818
use PHPStan\File\FileWriter;
19+
use PHPStan\Internal\ArrayHelper;
1920
use PHPStan\Internal\ComposerHelper;
2021
use PHPStan\PhpDoc\StubFilesProvider;
2122
use PHPStan\Reflection\ReflectionProvider;
@@ -29,6 +30,7 @@
2930
use function array_unique;
3031
use function array_values;
3132
use function count;
33+
use function explode;
3234
use function get_loaded_extensions;
3335
use function implode;
3436
use function is_array;
@@ -65,6 +67,7 @@ final class ResultCacheManager
6567
* @param string[] $bootstrapFiles
6668
* @param string[] $scanFiles
6769
* @param string[] $scanDirectories
70+
* @param list<string> $parametersNotInvalidatingCache
6871
*/
6972
public function __construct(
7073
private Container $container,
@@ -82,6 +85,7 @@ public function __construct(
8285
private array $scanFiles,
8386
private array $scanDirectories,
8487
private bool $checkDependenciesOfProjectExtensionFiles,
88+
private array $parametersNotInvalidatingCache,
8589
)
8690
{
8791
}
@@ -887,18 +891,9 @@ private function getMeta(array $allAnalysedFiles, ?array $projectConfigArray): a
887891
sort($extensions);
888892

889893
if ($projectConfigArray !== null) {
890-
unset($projectConfigArray['parameters']['editorUrl']);
891-
unset($projectConfigArray['parameters']['editorUrlTitle']);
892-
unset($projectConfigArray['parameters']['errorFormat']);
893-
unset($projectConfigArray['parameters']['ignoreErrors']);
894-
unset($projectConfigArray['parameters']['reportUnmatchedIgnoredErrors']);
895-
unset($projectConfigArray['parameters']['tipsOfTheDay']);
896-
unset($projectConfigArray['parameters']['parallel']);
897-
unset($projectConfigArray['parameters']['internalErrorsCountLimit']);
898-
unset($projectConfigArray['parameters']['cache']);
899-
unset($projectConfigArray['parameters']['memoryLimitFile']);
900-
unset($projectConfigArray['parameters']['pro']);
901-
unset($projectConfigArray['parametersSchema']);
894+
foreach ($this->parametersNotInvalidatingCache as $parameterPath) {
895+
ArrayHelper::unsetKeyAtPath($projectConfigArray, explode('.', $parameterPath));
896+
}
902897

903898
ksort($projectConfigArray);
904899
}

Diff for: src/Internal/ArrayHelper.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use function array_slice;
6+
use function count;
7+
8+
final class ArrayHelper
9+
{
10+
11+
/**
12+
* @param mixed[] $array
13+
* @param non-empty-list<string> $path
14+
*/
15+
public static function unsetKeyAtPath(array &$array, array $path): void
16+
{
17+
[$head, $tail] = [$path[0], array_slice($path, 1)];
18+
19+
if (count($tail) === 0) {
20+
unset($array[$head]);
21+
22+
} elseif (isset($array[$head])) {
23+
self::unsetKeyAtPath($array[$head], $tail);
24+
}
25+
}
26+
27+
}

Diff for: tests/PHPStan/Internal/ArrayHelperTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ArrayHelperTest extends TestCase
8+
{
9+
10+
public function testUnsetKeyAtPath(): void
11+
{
12+
$array = [
13+
'dep1a' => [
14+
'dep2a' => [
15+
'dep3a' => null,
16+
],
17+
'dep2b' => null,
18+
],
19+
'dep1b' => null,
20+
];
21+
22+
ArrayHelper::unsetKeyAtPath($array, ['dep1a', 'dep2a', 'dep3a']);
23+
24+
$this->assertSame([
25+
'dep1a' => [
26+
'dep2a' => [],
27+
'dep2b' => null,
28+
],
29+
'dep1b' => null,
30+
], $array);
31+
32+
ArrayHelper::unsetKeyAtPath($array, ['dep1a', 'dep2a']);
33+
34+
$this->assertSame([
35+
'dep1a' => [
36+
'dep2b' => null,
37+
],
38+
'dep1b' => null,
39+
], $array);
40+
41+
ArrayHelper::unsetKeyAtPath($array, ['dep1a']);
42+
43+
$this->assertSame([
44+
'dep1b' => null,
45+
], $array);
46+
47+
ArrayHelper::unsetKeyAtPath($array, ['dep1b']);
48+
49+
$this->assertSame([], $array);
50+
}
51+
52+
public function testUnsetKeyAtPathEmpty(): void
53+
{
54+
$array = [];
55+
56+
ArrayHelper::unsetKeyAtPath($array, ['foo', 'bar']);
57+
58+
$this->assertSame([], $array);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)