Skip to content

ResultCache: allow customization of params not invalidating cache #3877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ parameters:
- '1.1.1.2'
tmpDir: %sysGetTempDir%/phpstan-fixer
__validate: true
parametersNotInvalidatingCache:
- parameters.editorUrl
- parameters.editorUrlTitle
- parameters.errorFormat
- parameters.ignoreErrors
- parameters.reportUnmatchedIgnoredErrors
- parameters.tipsOfTheDay
- parameters.parallel
- parameters.internalErrorsCountLimit
- parameters.cache
- parameters.memoryLimitFile
- parameters.pro
- parametersSchema

extensions:
rules: PHPStan\DependencyInjection\RulesExtension
Expand Down Expand Up @@ -502,6 +515,7 @@ services:
scanFiles: %scanFiles%
scanDirectories: %scanDirectories%
checkDependenciesOfProjectExtensionFiles: %resultCacheChecksProjectExtensionFilesDependencies%
parametersNotInvalidatingCache: %parametersNotInvalidatingCache%

-
class: PHPStan\Analyser\ResultCache\ResultCacheClearer
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ parametersSchema:
])
env: arrayOf(string(), anyOf(int(), string()))
sysGetTempDir: string()
parametersNotInvalidatingCache: listOf(string())

# playground mode
sourceLocatorPlaygroundMode: bool()
Expand Down
19 changes: 7 additions & 12 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\File\FileFinder;
use PHPStan\File\FileHelper;
use PHPStan\File\FileWriter;
use PHPStan\Internal\ArrayHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\PhpDoc\StubFilesProvider;
use PHPStan\Reflection\ReflectionProvider;
Expand All @@ -29,6 +30,7 @@
use function array_unique;
use function array_values;
use function count;
use function explode;
use function get_loaded_extensions;
use function implode;
use function is_array;
Expand Down Expand Up @@ -65,6 +67,7 @@ final class ResultCacheManager
* @param string[] $bootstrapFiles
* @param string[] $scanFiles
* @param string[] $scanDirectories
* @param list<string> $parametersNotInvalidatingCache
*/
public function __construct(
private Container $container,
Expand All @@ -82,6 +85,7 @@ public function __construct(
private array $scanFiles,
private array $scanDirectories,
private bool $checkDependenciesOfProjectExtensionFiles,
private array $parametersNotInvalidatingCache,
)
{
}
Expand Down Expand Up @@ -887,18 +891,9 @@ private function getMeta(array $allAnalysedFiles, ?array $projectConfigArray): a
sort($extensions);

if ($projectConfigArray !== null) {
unset($projectConfigArray['parameters']['editorUrl']);
unset($projectConfigArray['parameters']['editorUrlTitle']);
unset($projectConfigArray['parameters']['errorFormat']);
unset($projectConfigArray['parameters']['ignoreErrors']);
unset($projectConfigArray['parameters']['reportUnmatchedIgnoredErrors']);
unset($projectConfigArray['parameters']['tipsOfTheDay']);
unset($projectConfigArray['parameters']['parallel']);
unset($projectConfigArray['parameters']['internalErrorsCountLimit']);
unset($projectConfigArray['parameters']['cache']);
unset($projectConfigArray['parameters']['memoryLimitFile']);
unset($projectConfigArray['parameters']['pro']);
unset($projectConfigArray['parametersSchema']);
foreach ($this->parametersNotInvalidatingCache as $parameterPath) {
ArrayHelper::unsetKeyAtPath($projectConfigArray, explode('.', $parameterPath));
}

ksort($projectConfigArray);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Internal/ArrayHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace PHPStan\Internal;

use function array_slice;
use function count;

final class ArrayHelper
{

/**
* @param mixed[] $array
* @param non-empty-list<string> $path
*/
public static function unsetKeyAtPath(array &$array, array $path): void
{
[$head, $tail] = [$path[0], array_slice($path, 1)];

if (count($tail) === 0) {
unset($array[$head]);

} elseif (isset($array[$head])) {
self::unsetKeyAtPath($array[$head], $tail);
}
}

}
61 changes: 61 additions & 0 deletions tests/PHPStan/Internal/ArrayHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace PHPStan\Internal;

use PHPUnit\Framework\TestCase;

class ArrayHelperTest extends TestCase
{

public function testUnsetKeyAtPath(): void
{
$array = [
'dep1a' => [
'dep2a' => [
'dep3a' => null,
],
'dep2b' => null,
],
'dep1b' => null,
];

ArrayHelper::unsetKeyAtPath($array, ['dep1a', 'dep2a', 'dep3a']);

$this->assertSame([
'dep1a' => [
'dep2a' => [],
'dep2b' => null,
],
'dep1b' => null,
], $array);

ArrayHelper::unsetKeyAtPath($array, ['dep1a', 'dep2a']);

$this->assertSame([
'dep1a' => [
'dep2b' => null,
],
'dep1b' => null,
], $array);

ArrayHelper::unsetKeyAtPath($array, ['dep1a']);

$this->assertSame([
'dep1b' => null,
], $array);

ArrayHelper::unsetKeyAtPath($array, ['dep1b']);

$this->assertSame([], $array);
}

public function testUnsetKeyAtPathEmpty(): void
{
$array = [];

ArrayHelper::unsetKeyAtPath($array, ['foo', 'bar']);

$this->assertSame([], $array);
}

}
Loading