Skip to content

Commit f63b423

Browse files
committed
TestCaseSourceLocatorFactory - skip polyfills based on PHP version
1 parent e1566e7 commit f63b423

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\BetterReflection\SourceLocator;
4+
5+
use Override;
6+
use PHPStan\BetterReflection\Identifier\Identifier;
7+
use PHPStan\BetterReflection\Identifier\IdentifierType;
8+
use PHPStan\BetterReflection\Reflection\Reflection;
9+
use PHPStan\BetterReflection\Reflection\ReflectionClass;
10+
use PHPStan\BetterReflection\Reflection\ReflectionConstant;
11+
use PHPStan\BetterReflection\Reflection\ReflectionFunction;
12+
use PHPStan\BetterReflection\Reflector\Reflector;
13+
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
14+
use PHPStan\Php\PhpVersion;
15+
use function str_contains;
16+
use function str_replace;
17+
18+
final class SkipPolyfillSourceLocator implements SourceLocator
19+
{
20+
21+
public function __construct(private SourceLocator $sourceLocator, private PhpVersion $phpVersion)
22+
{
23+
}
24+
25+
#[Override]
26+
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
27+
{
28+
$reflection = $this->sourceLocator->locateIdentifier($reflector, $identifier);
29+
if ($reflection === null) {
30+
return null;
31+
}
32+
33+
if ($reflection instanceof ReflectionClass || $reflection instanceof ReflectionFunction || $reflection instanceof ReflectionConstant) {
34+
$fileName = $reflection->getFileName();
35+
if ($fileName !== null) {
36+
$normalized = str_replace('\\', '/', $fileName);
37+
if (str_contains($normalized, '/symfony/polyfill-php80/') && $this->phpVersion->getVersionId() >= 80000) {
38+
return null;
39+
}
40+
if (str_contains($normalized, '/symfony/polyfill-php81/') && $this->phpVersion->getVersionId() >= 80100) {
41+
return null;
42+
}
43+
if (str_contains($normalized, '/symfony/polyfill-php82/') && $this->phpVersion->getVersionId() >= 80200) {
44+
return null;
45+
}
46+
if (str_contains($normalized, '/symfony/polyfill-php83/') && $this->phpVersion->getVersionId() >= 80300) {
47+
return null;
48+
}
49+
if (str_contains($normalized, '/symfony/polyfill-php84/') && $this->phpVersion->getVersionId() >= 80400) {
50+
return null;
51+
}
52+
if (str_contains($normalized, '/symfony/polyfill-php85/') && $this->phpVersion->getVersionId() >= 80500) {
53+
return null;
54+
}
55+
}
56+
}
57+
58+
return $reflection;
59+
}
60+
61+
#[Override]
62+
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
63+
{
64+
return $this->sourceLocator->locateIdentifiersByType($reflector, $identifierType);
65+
}
66+
67+
}

src/Testing/TestCaseSourceLocatorFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
1919
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository;
2020
use PHPStan\Reflection\BetterReflection\SourceLocator\PhpVersionBlacklistSourceLocator;
21+
use PHPStan\Reflection\BetterReflection\SourceLocator\SkipPolyfillSourceLocator;
2122
use ReflectionClass;
2223
use function dirname;
2324
use function is_file;
@@ -84,7 +85,7 @@ public function create(): SourceLocator
8485
$composerLocators[] = $composerSourceLocator;
8586
}
8687

87-
self::$composerSourceLocatorsCache[$cacheKey] = $composerLocators;
88+
self::$composerSourceLocatorsCache[$cacheKey] = [new SkipPolyfillSourceLocator(new AggregateSourceLocator($composerLocators), $this->phpVersion)];
8889
}
8990

9091
$locators = self::$composerSourceLocatorsCache[$cacheKey] ?? [];

0 commit comments

Comments
 (0)