Skip to content
Open
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
1 change: 1 addition & 0 deletions conf/services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ services:
class: PHPStan\Type\FileTypeMapper
arguments:
phpParser: @stubParser
currentWorkingDirectory: %currentWorkingDirectory%
resolvedPhpDocBlockCacheCountMax: %cache.resolvedPhpDocBlockCacheCountMax%
nameScopeMapMemoryCacheCountMax: %cache.nameScopeMapMemoryCacheCountMax%
autowired: false
Expand Down
22 changes: 22 additions & 0 deletions e2e/bug-15752/bug-15752_test.sh
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 5 additions & 0 deletions e2e/bug-15752/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: max
tmpDir: tmp
paths:
- src
23 changes: 23 additions & 0 deletions e2e/bug-15752/src/box.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

interface Box
{
/** @return list<string> */
public function items(): array;
}

function box(): Box
{
return new class(['a']) implements Box
{
/** @param list<string> $items */
public function __construct(private array $items) {}

public function items(): array
{
return $this->items;
}
};
}
2 changes: 2 additions & 0 deletions e2e/bug-15752/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*
6 changes: 5 additions & 1 deletion src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%')]
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Type/FileTypeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Loading