From 5fb6e0678c7be4a3f58bf5cdde042fa58615c0cc Mon Sep 17 00:00:00 2001 From: AdzerKI Date: Mon, 16 Nov 2026 09:52:13 +0300 Subject: [PATCH] Do not reuse name scopes built in another working directory The name scopes of a file name its anonymous classes by AnonymousClassNameHelper, which hashes the path relative to the current working directory. The ftm- cache entry was validated against the file contents only, so a run started from another directory reused a map with other anonymous class names and the PHPDoc of every anonymous class in the file resolved to nothing, even with the result cache cleared. The working directory is now part of the variable key of the entry. --- conf/services.neon | 1 + e2e/bug-15752/bug-15752_test.sh | 22 ++++++++++++++++++++++ e2e/bug-15752/phpstan.neon | 5 +++++ e2e/bug-15752/src/box.php | 23 +++++++++++++++++++++++ e2e/bug-15752/tmp/.gitignore | 2 ++ src/Type/FileTypeMapper.php | 6 +++++- tests/PHPStan/Type/FileTypeMapperTest.php | 1 + 7 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 e2e/bug-15752/bug-15752_test.sh create mode 100644 e2e/bug-15752/phpstan.neon create mode 100644 e2e/bug-15752/src/box.php create mode 100644 e2e/bug-15752/tmp/.gitignore diff --git a/conf/services.neon b/conf/services.neon index 1ec53e8e20c..7e3ba60f467 100644 --- a/conf/services.neon +++ b/conf/services.neon @@ -136,6 +136,7 @@ services: class: PHPStan\Type\FileTypeMapper arguments: phpParser: @stubParser + currentWorkingDirectory: %currentWorkingDirectory% resolvedPhpDocBlockCacheCountMax: %cache.resolvedPhpDocBlockCacheCountMax% nameScopeMapMemoryCacheCountMax: %cache.nameScopeMapMemoryCacheCountMax% autowired: false diff --git a/e2e/bug-15752/bug-15752_test.sh b/e2e/bug-15752/bug-15752_test.sh new file mode 100644 index 00000000000..8c9f5e67934 --- /dev/null +++ b/e2e/bug-15752/bug-15752_test.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# @tags result-cache + +function set_up() { + e2e::set_up +} + +function tear_down() { + e2e::tear_down +} + +function test_bug_15752() { + # https://github.com/phpstan/phpstan/issues/15752 + # The name scopes of a file name its anonymous classes after the path relative + # to the working directory. A run from another directory must not reuse them, + # even with the result cache cleared. + ../../bin/phpstan analyse + ../../bin/phpstan clear-result-cache + cd src + ../../../bin/phpstan analyse -c ../phpstan.neon && CODE=0 || CODE=$? + assert_same "0" "$CODE" +} diff --git a/e2e/bug-15752/phpstan.neon b/e2e/bug-15752/phpstan.neon new file mode 100644 index 00000000000..9a4acf69e74 --- /dev/null +++ b/e2e/bug-15752/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: max + tmpDir: tmp + paths: + - src diff --git a/e2e/bug-15752/src/box.php b/e2e/bug-15752/src/box.php new file mode 100644 index 00000000000..7360f19e79c --- /dev/null +++ b/e2e/bug-15752/src/box.php @@ -0,0 +1,23 @@ + */ + public function items(): array; +} + +function box(): Box +{ + return new class(['a']) implements Box + { + /** @param list $items */ + public function __construct(private array $items) {} + + public function items(): array + { + return $this->items; + } + }; +} diff --git a/e2e/bug-15752/tmp/.gitignore b/e2e/bug-15752/tmp/.gitignore new file mode 100644 index 00000000000..125e34294bf --- /dev/null +++ b/e2e/bug-15752/tmp/.gitignore @@ -0,0 +1,2 @@ +* +!.* diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index b928944736a..dd06f1220dd 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -87,6 +87,8 @@ public function __construct( private FileHelper $fileHelper, private Cache $cache, private FileContentHasher $fileContentHasher, + #[AutowiredParameter(ref: '%currentWorkingDirectory%')] + private string $currentWorkingDirectory, #[AutowiredParameter(ref: '%cache.resolvedPhpDocBlockCacheCountMax%')] private int $resolvedPhpDocBlockCacheCountMax, #[AutowiredParameter(ref: '%cache.nameScopeMapMemoryCacheCountMax%')] @@ -398,7 +400,9 @@ private function getNameScopeMap(string $fileName): array // created under the same use statements // v8: the files are recorded with whether the map was built from their simplified AST; // v7 entries may be built from it even under the key of the full AST - $variableCacheKey = sprintf('v8-%s', ComposerHelper::getPhpDocParserVersion()); + // v9: the map names anonymous classes after their paths relative to the working directory, + // so an entry built in another directory does not have the names the analysis looks for + $variableCacheKey = sprintf('v9-%s-%s', ComposerHelper::getPhpDocParserVersion(), $this->currentWorkingDirectory); $cached = $this->loadCachedPhpDocNodeMap($cacheKey, $variableCacheKey); if ($cached === null) { [$nameScopeMap, $files] = $this->createPhpDocNodeMap($fileName, null, null, [], $fileName); diff --git a/tests/PHPStan/Type/FileTypeMapperTest.php b/tests/PHPStan/Type/FileTypeMapperTest.php index 49658c33707..daf5f2df8a2 100644 --- a/tests/PHPStan/Type/FileTypeMapperTest.php +++ b/tests/PHPStan/Type/FileTypeMapperTest.php @@ -301,6 +301,7 @@ private function createFileTypeMapper(CacheStorage $storage): FileTypeMapper $container->getByType(FileHelper::class), new Cache($storage), $container->getByType(FileContentHasher::class), + $container->getParameter('currentWorkingDirectory'), 2048, 128, );