forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHPUnitVersionDetector.php
More file actions
50 lines (41 loc) · 1.31 KB
/
PHPUnitVersionDetector.php
File metadata and controls
50 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionException;
use function dirname;
use function explode;
use function file_get_contents;
use function json_decode;
class PHPUnitVersionDetector
{
public function createPHPUnitVersion(): PHPUnitVersion
{
$file = false;
$majorVersion = null;
$minorVersion = null;
try {
// uses runtime reflection to reduce unnecessary work while bootstrapping PHPStan.
// static reflection would need to AST parse and build up reflection for a lot of files otherwise.
$reflection = new ReflectionClass(TestCase::class);
$file = $reflection->getFileName();
} catch (ReflectionException $e) {
// PHPUnit might not be installed
}
if ($file !== false) {
$phpUnitRoot = dirname($file, 3);
$phpUnitComposer = $phpUnitRoot . '/composer.json';
$composerJson = @file_get_contents($phpUnitComposer);
if ($composerJson !== false) {
$json = json_decode($composerJson, true);
$version = $json['extra']['branch-alias']['dev-main'] ?? null;
if ($version !== null) {
$versionParts = explode('.', $version);
$majorVersion = (int) $versionParts[0];
$minorVersion = (int) $versionParts[1];
}
}
}
return new PHPUnitVersion($majorVersion, $minorVersion);
}
}