mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 01:40:49 +00:00
7e7a2f067a
476cfb00cb
disable fallback for now
114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\PHPUnit\PHPUnit100\Rector\Class_;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use PhpParser\Node\Stmt\TraitUse;
|
|
use PHPStan\Reflection\ClassReflection;
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
|
|
use Rector\Rector\AbstractRector;
|
|
use Rector\Reflection\ReflectionResolver;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @changelog https://github.com/sebastianbergmann/phpunit/issues/4142
|
|
* @changelog https://github.com/sebastianbergmann/phpunit/issues/4141
|
|
* @changelog https://github.com/sebastianbergmann/phpunit/issues/4149
|
|
*
|
|
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\AddProphecyTraitRector\AddProphecyTraitRectorTest
|
|
*/
|
|
final class AddProphecyTraitRector extends AbstractRector
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
|
|
*/
|
|
private $testsNodeAnalyzer;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Reflection\ReflectionResolver
|
|
*/
|
|
private $reflectionResolver;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
|
*/
|
|
private $betterNodeFinder;
|
|
/**
|
|
* @var string
|
|
*/
|
|
private const PROPHECY_TRAIT = 'Prophecy\\PhpUnit\\ProphecyTrait';
|
|
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer, ReflectionResolver $reflectionResolver, BetterNodeFinder $betterNodeFinder)
|
|
{
|
|
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
|
|
$this->reflectionResolver = $reflectionResolver;
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Add Prophecy trait for method using $this->prophesize()', [new CodeSample(<<<'CODE_SAMPLE'
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ExampleTest extends TestCase
|
|
{
|
|
public function testOne(): void
|
|
{
|
|
$prophecy = $this->prophesize(\AnInterface::class);
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
use PHPUnit\Framework\TestCase;
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
|
|
|
final class ExampleTest extends TestCase
|
|
{
|
|
use ProphecyTrait;
|
|
|
|
public function testOne(): void
|
|
{
|
|
$prophecy = $this->prophesize(\AnInterface::class);
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [Class_::class];
|
|
}
|
|
/**
|
|
* @param Class_ $node
|
|
*/
|
|
public function refactor(Node $node) : ?Node
|
|
{
|
|
if ($this->shouldSkipClass($node)) {
|
|
return null;
|
|
}
|
|
$traitUse = new TraitUse([new FullyQualified(self::PROPHECY_TRAIT)]);
|
|
$node->stmts = \array_merge([$traitUse], $node->stmts);
|
|
return $node;
|
|
}
|
|
private function shouldSkipClass(Class_ $class) : bool
|
|
{
|
|
$hasProphesizeMethodCall = (bool) $this->betterNodeFinder->findFirst($class, function (Node $node) : bool {
|
|
return $this->testsNodeAnalyzer->isAssertMethodCallName($node, 'prophesize');
|
|
});
|
|
if (!$hasProphesizeMethodCall) {
|
|
return \true;
|
|
}
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
return \false;
|
|
}
|
|
return $classReflection->hasTraitUse(self::PROPHECY_TRAIT);
|
|
}
|
|
}
|