-
Notifications
You must be signed in to change notification settings - Fork 74
/
AssertOptionSelectedRector.php
70 lines (60 loc) · 2.08 KB
/
AssertOptionSelectedRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal9\Rector\Deprecation;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class AssertOptionSelectedRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated AssertLegacyTrait::assertOptionSelected() calls', [
new CodeSample(
<<<'CODE_BEFORE'
$this->assertOptionSelected('options', 2);
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertTrue($this->assertSession()->optionExists('options', 2)->hasAttribute('selected'));
CODE_AFTER
),
]);
}
public function getNodeTypes(): array
{
return [
Node\Expr\MethodCall::class,
];
}
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);
if ($this->getName($node->name) !== 'assertOptionSelected') {
return null;
}
$message = null;
if (count($node->args) === 3) {
$message = $node->args[2];
}
$assertSessionNode = $this->nodeFactory->createLocalMethodCall('assertSession');
$optionExistsNode = $this->nodeFactory->createMethodCall($assertSessionNode, 'optionExists', [
$node->args[0],
$node->args[1],
]);
$hasAttributeNode = $this->nodeFactory->createMethodCall(
$optionExistsNode,
'hasAttribute',
$this->nodeFactory->createArgs(['selected'])
);
if ($message === null) {
return $this->nodeFactory->createLocalMethodCall('assertTrue', [
$this->nodeFactory->createArg($hasAttributeNode),
]);
}
return $this->nodeFactory->createLocalMethodCall('assertTrue', [
$this->nodeFactory->createArg($hasAttributeNode),
$this->nodeFactory->createArg($message),
]);
}
}