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, );