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
51 lines (43 loc) · 1.36 KB
/
PHPUnitVersionDetector.php
File metadata and controls
51 lines (43 loc) · 1.36 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
51
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PHPStan\Reflection\ReflectionProvider;
use PHPUnit\Framework\TestCase;
use function dirname;
use function explode;
use function file_get_contents;
use function is_file;
use function json_decode;
class PHPUnitVersionDetector
{
private ReflectionProvider $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
public function createPHPUnitVersion(): PHPUnitVersion
{
$majorVersion = null;
$minorVersion = null;
if ($this->reflectionProvider->hasClass(TestCase::class)) {
$testCase = $this->reflectionProvider->getClass(TestCase::class);
$file = $testCase->getFileName();
if ($file !== null) {
$phpUnitRoot = dirname($file, 3);
$phpUnitComposer = $phpUnitRoot . '/composer.json';
if (is_file($phpUnitComposer)) {
$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);
}
}