Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions src/Rules/PHPUnit/PHPUnitVersionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace PHPStan\Rules\PHPUnit;

use PHPStan\Reflection\ReflectionProvider;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionException;
use function dirname;
use function explode;
use function file_get_contents;
Expand All @@ -13,33 +14,33 @@
class PHPUnitVersionDetector
{

private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function createPHPUnitVersion(): PHPUnitVersion
{
$file = false;
$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];
}

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';
if (is_file($phpUnitComposer)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need this is_file or could we just drop it, because we already do @file_get_contents($phpUnitComposer); which would swallow the error nevertheless?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be dropped :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we go.

$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];
}
}
}
Expand Down