Skip to content

Commit 2722d10

Browse files
xificurkclaude
andcommitted
Replace StubFilesExtensionLoaderTest with ResultCacheNoReflectionTest
Test that BetterReflection is not initialized when ResultCacheManager constructs the ResultCache, which is the real-world code path that exercises StubFilesExtensionLoader::getFiles(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e668664 commit 2722d10

5 files changed

Lines changed: 59 additions & 31 deletions

File tree

phpstan-baseline.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ parameters:
6060
count: 1
6161
path: src/Type/Symfony/InputInterfaceHasOptionDynamicReturnTypeExtension.php
6262

63+
-
64+
message: '#^Accessing PHPStan\\Analyser\\ResultCache\\ResultCacheManagerFactory\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
65+
identifier: phpstanApi.classConstant
66+
count: 1
67+
path: tests/Analyser/ResultCacheNoReflectionTest.php
68+
69+
-
70+
message: '#^Calling PHPStan\\Analyser\\ResultCache\\ResultCacheManagerFactory\:\:create\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version\.$#'
71+
identifier: phpstanApi.method
72+
count: 1
73+
path: tests/Analyser/ResultCacheNoReflectionTest.php
74+
75+
-
76+
message: '#^Calling PHPStan\\Analyser\\ResultCache\\ResultCacheManager\:\:restore\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version\.$#'
77+
identifier: phpstanApi.method
78+
count: 1
79+
path: tests/Analyser/ResultCacheNoReflectionTest.php
80+
6381
-
6482
message: '#^Accessing PHPStan\\Rules\\Methods\\CallMethodsRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
6583
identifier: phpstanApi.classConstant
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use LogicException;
6+
use PHPStan\Analyser\ResultCache\ResultCacheManagerFactory;
7+
use PHPStan\BetterReflection\Reflector\Reflector;
8+
use PHPStan\Command\Output;
9+
use PHPStan\Testing\PHPStanTestCase;
10+
11+
final class ResultCacheNoReflectionTest extends PHPStanTestCase
12+
{
13+
14+
public function testRestoreDoesNotUseSourceLocator(): void
15+
{
16+
$container = self::getContainer();
17+
18+
$manager = $container->getByType(ResultCacheManagerFactory::class)->create([]);
19+
$manager->restore([], true, false, null, $this->createMock(Output::class));
20+
21+
// Sanity check: verify ThrowingSourceLocator is actually wired up.
22+
// If restore() had triggered the source locator, the test would have already failed above.
23+
$this->expectException(LogicException::class);
24+
$this->expectExceptionMessage('must not be called during result cache construction');
25+
$container->getByType(Reflector::class)->reflectClass(self::class);
26+
}
27+
28+
public static function getAdditionalConfigFiles(): array
29+
{
30+
return [
31+
__DIR__ . '/../../extension.neon',
32+
__DIR__ . '/result-cache-no-reflection-test.neon',
33+
];
34+
}
35+
36+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php declare(strict_types = 1);
22

3-
namespace PHPStan\Stubs\Symfony;
3+
namespace PHPStan\Analyser;
44

5+
use LogicException;
56
use PHPStan\BetterReflection\Identifier\Identifier;
67
use PHPStan\BetterReflection\Identifier\IdentifierType;
78
use PHPStan\BetterReflection\Reflection\Reflection;
89
use PHPStan\BetterReflection\Reflector\Reflector;
910
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
10-
use PHPUnit\Framework\Assert;
1111

1212
final class ThrowingSourceLocator implements SourceLocator
1313
{
1414

1515
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
1616
{
17-
Assert::fail('SourceLocator::locateIdentifier must not be called during getFiles()');
17+
throw new LogicException('SourceLocator::locateIdentifier must not be called during result cache construction');
1818
}
1919

2020
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
2121
{
22-
Assert::fail('SourceLocator::locateIdentifiersByType must not be called during getFiles()');
22+
throw new LogicException('SourceLocator::locateIdentifiersByType must not be called during result cache construction');
2323
}
2424

2525
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
services:
22
betterReflectionSourceLocator:
33
class: PHPStan\BetterReflection\SourceLocator\Type\SourceLocator
4-
factory: PHPStan\Stubs\Symfony\ThrowingSourceLocator
4+
factory: PHPStan\Analyser\ThrowingSourceLocator
55
autowired: false

tests/Stubs/Symfony/StubFilesExtensionLoaderTest.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)