-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataProviderDeclarationRule - report non-static dataProvider only wit…
…h PHPUnit 10+
- Loading branch information
1 parent
b9827cf
commit cd9c693
Showing
4 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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 DataProviderHelperFactory | ||
{ | ||
|
||
/** @var ReflectionProvider */ | ||
private $reflectionProvider; | ||
|
||
public function __construct(ReflectionProvider $reflectionProvider) | ||
{ | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
public function create(): DataProviderHelper | ||
{ | ||
$phpUnit10OrNewer = false; | ||
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) { | ||
$majorVersion = (int) explode('.', $version)[0]; | ||
if ($majorVersion >= 10) { | ||
$phpUnit10OrNewer = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return new DataProviderHelper($this->reflectionProvider, $phpUnit10OrNewer); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters