|
2 | 2 |
|
3 | 3 | namespace PHPStan\Reflection\BetterReflection\SourceLocator;
|
4 | 4 |
|
| 5 | +use PHPStan\BetterReflection\Identifier\IdentifierType; |
| 6 | +use PHPStan\BetterReflection\Reflection\Reflection; |
5 | 7 | use PHPStan\BetterReflection\Reflector\DefaultReflector;
|
6 | 8 | use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
|
7 | 9 | use PHPStan\Reflection\InitializerExprContext;
|
|
10 | 12 | use PHPStan\Type\VerbosityLevel;
|
11 | 13 | use SingleFileSourceLocatorTestClass;
|
12 | 14 | use TestSingleFileSourceLocator\AFoo;
|
| 15 | +use function array_map; |
13 | 16 | use const PHP_VERSION_ID;
|
14 | 17 |
|
15 | 18 | class OptimizedSingleFileSourceLocatorTest extends PHPStanTestCase
|
@@ -51,6 +54,73 @@ public function dataClass(): iterable
|
51 | 54 | ];
|
52 | 55 | }
|
53 | 56 |
|
| 57 | + public function dataForIdenifiersByType(): iterable |
| 58 | + { |
| 59 | + yield from [ |
| 60 | + 'classes wrapped in conditions' => [ |
| 61 | + new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
| 62 | + [ |
| 63 | + 'TestSingleFileSourceLocator\AFoo', |
| 64 | + 'TestSingleFileSourceLocator\InCondition', |
| 65 | + 'TestSingleFileSourceLocator\InCondition', |
| 66 | + 'TestSingleFileSourceLocator\InCondition', |
| 67 | + ], |
| 68 | + __DIR__ . '/data/a.php', |
| 69 | + ], |
| 70 | + 'class with function in same file' => [ |
| 71 | + new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
| 72 | + ['SingleFileSourceLocatorTestClass'], |
| 73 | + __DIR__ . '/data/b.php', |
| 74 | + ], |
| 75 | + 'class bug-5525' => [ |
| 76 | + new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
| 77 | + ['Faker\Provider\nl_BE\Text'], |
| 78 | + __DIR__ . '/data/bug-5525.php', |
| 79 | + ], |
| 80 | + 'file without classes' => [ |
| 81 | + new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
| 82 | + [], |
| 83 | + __DIR__ . '/data/const.php', |
| 84 | + ], |
| 85 | + 'plain function in complex file' => [ |
| 86 | + new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION), |
| 87 | + [ |
| 88 | + 'TestSingleFileSourceLocator\doFoo', |
| 89 | + ], |
| 90 | + __DIR__ . '/data/a.php', |
| 91 | + ], |
| 92 | + 'function with class in same file' => [ |
| 93 | + new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION), |
| 94 | + ['singleFileSourceLocatorTestFunction'], |
| 95 | + __DIR__ . '/data/b.php', |
| 96 | + ], |
| 97 | + 'file without functions' => [ |
| 98 | + new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION), |
| 99 | + [], |
| 100 | + __DIR__ . '/data/only-class.php', |
| 101 | + ], |
| 102 | + 'constants' => [ |
| 103 | + new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT), |
| 104 | + [], |
| 105 | + __DIR__ . '/data/const.php', |
| 106 | + ], |
| 107 | + ]; |
| 108 | + |
| 109 | + if (PHP_VERSION_ID < 80100) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + yield 'enums as classes' => [ |
| 114 | + new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
| 115 | + [ |
| 116 | + 'OptimizedDirectory\BackedByStringWithoutSpace', |
| 117 | + 'OptimizedDirectory\TestEnum', |
| 118 | + 'OptimizedDirectory\UppercaseEnum', |
| 119 | + ], |
| 120 | + __DIR__ . '/data/directory/enum.php', |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
54 | 124 | /**
|
55 | 125 | * @dataProvider dataClass
|
56 | 126 | */
|
@@ -165,4 +235,28 @@ public function testConstUnknown(string $constantName): void
|
165 | 235 | $reflector->reflectConstant($constantName);
|
166 | 236 | }
|
167 | 237 |
|
| 238 | + /** |
| 239 | + * @dataProvider dataForIdenifiersByType |
| 240 | + * @param class-string[] $expectedIdentifiers |
| 241 | + */ |
| 242 | + public function testLocateIdentifiersByType( |
| 243 | + IdentifierType $identifierType, |
| 244 | + array $expectedIdentifiers, |
| 245 | + string $file, |
| 246 | + ): void |
| 247 | + { |
| 248 | + /** @var OptimizedSingleFileSourceLocatorFactory $factory */ |
| 249 | + $factory = self::getContainer()->getByType(OptimizedSingleFileSourceLocatorFactory::class); |
| 250 | + $locator = $factory->create($file); |
| 251 | + $reflector = new DefaultReflector($locator); |
| 252 | + |
| 253 | + $reflections = $locator->locateIdentifiersByType( |
| 254 | + $reflector, |
| 255 | + $identifierType, |
| 256 | + ); |
| 257 | + |
| 258 | + $actualIdentifiers = array_map(static fn (Reflection $reflection) => $reflection->getName(), $reflections); |
| 259 | + $this->assertEqualsCanonicalizing($expectedIdentifiers, $actualIdentifiers); |
| 260 | + } |
| 261 | + |
168 | 262 | }
|
0 commit comments