|
3 | 3 | namespace PHPStan\Reflection\BetterReflection\SourceLocator; |
4 | 4 |
|
5 | 5 | use OptimizedDirectory\BFoo; |
| 6 | +use PHPStan\BetterReflection\Identifier\IdentifierType; |
| 7 | +use PHPStan\BetterReflection\Reflection\Reflection; |
6 | 8 | use PHPStan\BetterReflection\Reflector\DefaultReflector; |
7 | 9 | use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; |
8 | 10 | use PHPStan\Testing\PHPStanTestCase; |
9 | 11 | use TestDirectorySourceLocator\AFoo; |
| 12 | +use function array_map; |
10 | 13 | use function basename; |
11 | 14 | use const PHP_VERSION_ID; |
12 | 15 |
|
@@ -135,6 +138,49 @@ public function testFunctionExists(string $functionName, string $expectedFunctio |
135 | 138 | $this->assertSame($file, basename($functionReflection->getFileName())); |
136 | 139 | } |
137 | 140 |
|
| 141 | + public function testLocateIdentifiersByType(): void |
| 142 | + { |
| 143 | + /** @var OptimizedDirectorySourceLocatorFactory $factory */ |
| 144 | + $factory = self::getContainer()->getByType(OptimizedDirectorySourceLocatorFactory::class); |
| 145 | + $locator = $factory->createByDirectory(__DIR__ . '/data/directory'); |
| 146 | + $reflector = new DefaultReflector($locator); |
| 147 | + |
| 148 | + $classIdentifiers = $locator->locateIdentifiersByType( |
| 149 | + $reflector, |
| 150 | + new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
| 151 | + ); |
| 152 | + |
| 153 | + $expectedClasses = [ |
| 154 | + 'TestDirectorySourceLocator\AFoo', |
| 155 | + 'OptimizedDirectory\BFoo', |
| 156 | + 'CFoo', |
| 157 | + ]; |
| 158 | + if (PHP_VERSION_ID >= 80100) { |
| 159 | + $expectedClasses[] = 'OptimizedDirectory\TestEnum'; |
| 160 | + $expectedClasses[] = 'OptimizedDirectory\BackedByStringWithoutSpace'; |
| 161 | + $expectedClasses[] = 'OptimizedDirectory\UppercaseEnum'; |
| 162 | + } |
| 163 | + |
| 164 | + $actualClasses = array_map(static fn (Reflection $reflection) => $reflection->getName(), $classIdentifiers); |
| 165 | + $this->assertEqualsCanonicalizing($expectedClasses, $actualClasses); |
| 166 | + |
| 167 | + $functionIdentifiers = $locator->locateIdentifiersByType( |
| 168 | + $reflector, |
| 169 | + new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION), |
| 170 | + ); |
| 171 | + |
| 172 | + $actualFunctions = array_map(static fn (Reflection $reflection) => $reflection->getName(), $functionIdentifiers); |
| 173 | + |
| 174 | + $this->assertEqualsCanonicalizing([ |
| 175 | + 'TestDirectorySourceLocator\doLorem', |
| 176 | + 'OptimizedDirectory\doBar', |
| 177 | + 'OptimizedDirectory\doBaz', |
| 178 | + 'OptimizedDirectory\get_smarty', |
| 179 | + 'OptimizedDirectory\get_smarty2', |
| 180 | + 'OptimizedDirectory\upperCaseFunction', |
| 181 | + ], $actualFunctions); |
| 182 | + } |
| 183 | + |
138 | 184 | public function dataFunctionDoesNotExist(): array |
139 | 185 | { |
140 | 186 | return [ |
|
0 commit comments