|
2 | 2 |
|
3 | 3 | namespace PHPStan\Rules\PHPUnit; |
4 | 4 |
|
5 | | -use PHPStan\Reflection\ReflectionProvider; |
6 | 5 | use PHPUnit\Framework\TestCase; |
| 6 | +use ReflectionClass; |
| 7 | +use ReflectionException; |
7 | 8 | use function dirname; |
8 | 9 | use function explode; |
9 | 10 | use function file_get_contents; |
10 | | -use function is_file; |
11 | 11 | use function json_decode; |
12 | 12 |
|
13 | 13 | class PHPUnitVersionDetector |
14 | 14 | { |
15 | 15 |
|
16 | | - private ReflectionProvider $reflectionProvider; |
17 | | - |
18 | | - public function __construct(ReflectionProvider $reflectionProvider) |
19 | | - { |
20 | | - $this->reflectionProvider = $reflectionProvider; |
21 | | - } |
22 | | - |
23 | 16 | public function createPHPUnitVersion(): PHPUnitVersion |
24 | 17 | { |
| 18 | + $file = false; |
25 | 19 | $majorVersion = null; |
26 | 20 | $minorVersion = null; |
27 | | - if ($this->reflectionProvider->hasClass(TestCase::class)) { |
28 | | - $testCase = $this->reflectionProvider->getClass(TestCase::class); |
29 | | - $file = $testCase->getFileName(); |
30 | | - if ($file !== null) { |
31 | | - $phpUnitRoot = dirname($file, 3); |
32 | | - $phpUnitComposer = $phpUnitRoot . '/composer.json'; |
33 | | - if (is_file($phpUnitComposer)) { |
34 | | - $composerJson = @file_get_contents($phpUnitComposer); |
35 | | - if ($composerJson !== false) { |
36 | | - $json = json_decode($composerJson, true); |
37 | | - $version = $json['extra']['branch-alias']['dev-main'] ?? null; |
38 | | - if ($version !== null) { |
39 | | - $versionParts = explode('.', $version); |
40 | | - $majorVersion = (int) $versionParts[0]; |
41 | | - $minorVersion = (int) $versionParts[1]; |
42 | | - } |
43 | | - } |
| 21 | + |
| 22 | + try { |
| 23 | + // uses runtime reflection to reduce unnecessary work while bootstrapping PHPStan. |
| 24 | + // static reflection would need to AST parse and build up reflection for a lot of files otherwise. |
| 25 | + $reflection = new ReflectionClass(TestCase::class); |
| 26 | + $file = $reflection->getFileName(); |
| 27 | + } catch (ReflectionException $e) { |
| 28 | + // PHPUnit might not be installed |
| 29 | + } |
| 30 | + |
| 31 | + if ($file !== false) { |
| 32 | + $phpUnitRoot = dirname($file, 3); |
| 33 | + $phpUnitComposer = $phpUnitRoot . '/composer.json'; |
| 34 | + |
| 35 | + $composerJson = @file_get_contents($phpUnitComposer); |
| 36 | + if ($composerJson !== false) { |
| 37 | + $json = json_decode($composerJson, true); |
| 38 | + $version = $json['extra']['branch-alias']['dev-main'] ?? null; |
| 39 | + if ($version !== null) { |
| 40 | + $versionParts = explode('.', $version); |
| 41 | + $majorVersion = (int) $versionParts[0]; |
| 42 | + $minorVersion = (int) $versionParts[1]; |
44 | 43 | } |
45 | 44 | } |
46 | 45 | } |
|
0 commit comments