/* Decoded by unphp.net */ */ private const OLD_TO_NEW_METHOD = ['setExpectedException' => 'expectExceptionMessage', 'setExpectedExceptionRegExp' => 'expectExceptionMessageRegExp']; public function __construct(AssertCallFactory $assertCallFactory, TestsNodeAnalyzer $testsNodeAnalyzer) { $this->assertCallFactory = $assertCallFactory; $this->testsNodeAnalyzer = $testsNodeAnalyzer; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.', [new CodeSample(<<<'CODE_SAMPLE' use PHPUnit\Framework\TestCase; class SomeTest extends TestCase { public function test() { $this->setExpectedException(SomeException::class, "Message", "CODE"); } } CODE_SAMPLE , <<<'CODE_SAMPLE' use PHPUnit\Framework\TestCase; class SomeTest extends TestCase { public function test() { $this->setExpectedException(SomeException::class); $this->expectExceptionMessage('Message'); $this->expectExceptionCode('CODE'); } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [StmtsAwareInterface::class]; } /** * @param StmtsAwareInterface $node */ public function refactor(Node $node) : ?Node { if ($node->stmts === null || $node->stmts === []) { return null; } if (!$this->testsNodeAnalyzer->isInTestClass($node)) { return null; } $hasChanged = \false; $oldMethodNames = \array_keys(self::OLD_TO_NEW_METHOD); foreach ($node->stmts as $key => $stmt) { if (!$stmt instanceof Expression) { continue; } if (!$stmt->expr instanceof StaticCall && !$stmt->expr instanceof MethodCall) { continue; } $call = $stmt->expr; if (!$this->testsNodeAnalyzer->isPHPUnitMethodCallNames($call, $oldMethodNames)) { continue; } // add exception code method call if (isset($call->args[2])) { $extraCall = $this->assertCallFactory->createCallWithName($call, 'expectExceptionCode'); $extraCall->args[] = $call->args[2]; \array_splice($node->stmts, $key + 1, 0, [new Expression($extraCall)]); unset($call->args[2]); } if (isset($call->args[1])) { $extraCall = $this->createFirstArgExtraMethodCall($call); \array_splice($node->stmts, $key + 1, 0, [new Expression($extraCall)]); unset($call->args[1]); } $hasChanged = \true; $call->name = new Identifier('expectException'); } if ($hasChanged) { return $node; } return null; } /** * @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $call * @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall */ private function createFirstArgExtraMethodCall($call) { /** @var Identifier $identifierNode */ $identifierNode = $call->name; $oldMethodName = $identifierNode->name; $extraCall = $this->assertCallFactory->createCallWithName($call, self::OLD_TO_NEW_METHOD[$oldMethodName]); $extraCall->args[] = $call->args[1]; return $extraCall; } } ?>