mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-13 06:32:22 +00:00
4d01db5c10
[DX] Improve direct return of Stmt arrays in Rector rules, remove NodesToAddCollector from AbstractRector (#2623)
91 lines
3.4 KiB
PHP
91 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\PHPUnit\Rector\MethodCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
use PhpParser\Node\Identifier;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
|
|
use Rector\PHPUnit\NodeFactory\AssertCallFactory;
|
|
use Rector\PostRector\Collector\NodesToAddCollector;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\PHPUnit\Tests\Rector\MethodCall\DelegateExceptionArgumentsRector\DelegateExceptionArgumentsRectorTest
|
|
*/
|
|
final class DelegateExceptionArgumentsRector extends AbstractRector
|
|
{
|
|
/**
|
|
* @var array<string, string>
|
|
*/
|
|
private const OLD_TO_NEW_METHOD = ['setExpectedException' => 'expectExceptionMessage', 'setExpectedExceptionRegExp' => 'expectExceptionMessageRegExp'];
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PHPUnit\NodeFactory\AssertCallFactory
|
|
*/
|
|
private $assertCallFactory;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
|
|
*/
|
|
private $testsNodeAnalyzer;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PostRector\Collector\NodesToAddCollector
|
|
*/
|
|
private $nodesToAddCollector;
|
|
public function __construct(AssertCallFactory $assertCallFactory, TestsNodeAnalyzer $testsNodeAnalyzer, NodesToAddCollector $nodesToAddCollector)
|
|
{
|
|
$this->assertCallFactory = $assertCallFactory;
|
|
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
|
|
$this->nodesToAddCollector = $nodesToAddCollector;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.', [new CodeSample('$this->setExpectedException(Exception::class, "Message", "CODE");', <<<'CODE_SAMPLE'
|
|
$this->setExpectedException(Exception::class);
|
|
$this->expectExceptionMessage('Message');
|
|
$this->expectExceptionCode('CODE');
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [MethodCall::class, StaticCall::class];
|
|
}
|
|
/**
|
|
* @param MethodCall|StaticCall $node
|
|
*/
|
|
public function refactor(Node $node) : ?Node
|
|
{
|
|
$oldMethodNames = \array_keys(self::OLD_TO_NEW_METHOD);
|
|
if (!$this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) {
|
|
return null;
|
|
}
|
|
if (isset($node->args[1])) {
|
|
/** @var Identifier $identifierNode */
|
|
$identifierNode = $node->name;
|
|
$oldMethodName = $identifierNode->name;
|
|
$call = $this->assertCallFactory->createCallWithName($node, self::OLD_TO_NEW_METHOD[$oldMethodName]);
|
|
$call->args[] = $node->args[1];
|
|
$this->nodesToAddCollector->addNodeAfterNode($call, $node);
|
|
unset($node->args[1]);
|
|
// add exception code method call
|
|
if (isset($node->args[2])) {
|
|
$call = $this->assertCallFactory->createCallWithName($node, 'expectExceptionCode');
|
|
$call->args[] = $node->args[2];
|
|
$this->nodesToAddCollector->addNodeAfterNode($call, $node);
|
|
unset($node->args[2]);
|
|
}
|
|
}
|
|
$node->name = new Identifier('expectException');
|
|
return $node;
|
|
}
|
|
}
|