forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssertComparisonToSpecificMethodRector.php
162 lines (139 loc) · 5.44 KB
/
AssertComparisonToSpecificMethodRector.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ConstantScalarType;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\ValueObject\BinaryOpWithAssertMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector\AssertComparisonToSpecificMethodRectorTest
*/
final class AssertComparisonToSpecificMethodRector extends AbstractRector
{
/**
* @var BinaryOpWithAssertMethod[]
*/
private array $binaryOpWithAssertMethods = [];
public function __construct(
private readonly IdentifierManipulator $identifierManipulator,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
$this->binaryOpWithAssertMethods = [
new BinaryOpWithAssertMethod(Identical::class, 'assertSame', 'assertNotSame'),
new BinaryOpWithAssertMethod(NotIdentical::class, 'assertNotSame', 'assertSame'),
new BinaryOpWithAssertMethod(Equal::class, 'assertEquals', 'assertNotEquals'),
new BinaryOpWithAssertMethod(NotEqual::class, 'assertNotEquals', 'assertEquals'),
new BinaryOpWithAssertMethod(Greater::class, 'assertGreaterThan', 'assertLessThan'),
new BinaryOpWithAssertMethod(Smaller::class, 'assertLessThan', 'assertGreaterThan'),
new BinaryOpWithAssertMethod(
GreaterOrEqual::class,
'assertGreaterThanOrEqual',
'assertLessThanOrEqual'
),
new BinaryOpWithAssertMethod(
SmallerOrEqual::class,
'assertLessThanOrEqual',
'assertGreaterThanOrEqual'
),
];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turns comparison operations to their method name alternatives in PHPUnit TestCase',
[
new CodeSample(
'$this->assertTrue($foo === $bar, "message");',
'$this->assertSame($bar, $foo, "message");'
),
new CodeSample(
'$this->assertFalse($foo >= $bar, "message");',
'$this->assertLessThanOrEqual($bar, $foo, "message");'
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class];
}
/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertTrue', 'assertFalse'])) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof BinaryOp) {
return null;
}
return $this->processCallWithBinaryOp($node, $firstArgumentValue);
}
private function processCallWithBinaryOp(MethodCall|StaticCall $node, BinaryOp $binaryOp): ?Node
{
$binaryOpClass = $binaryOp::class;
foreach ($this->binaryOpWithAssertMethods as $binaryOpWithAssertMethod) {
if ($binaryOpClass !== $binaryOpWithAssertMethod->getBinaryOpClass()) {
continue;
}
$this->identifierManipulator->renameNodeWithMap($node, [
'assertTrue' => $binaryOpWithAssertMethod->getAssetMethodName(),
'assertFalse' => $binaryOpWithAssertMethod->getNotAssertMethodName(),
]);
$this->changeArgumentsOrder($node);
return $node;
}
return null;
}
private function changeArgumentsOrder(MethodCall|StaticCall $node): void
{
$oldArguments = $node->getArgs();
/** @var BinaryOp $expression */
$expression = $oldArguments[0]->value;
if ($this->isConstantValue($expression->left)) {
$firstArgument = new Arg($expression->left);
$secondArgument = new Arg($expression->right);
} else {
$firstArgument = new Arg($expression->right);
$secondArgument = new Arg($expression->left);
}
unset($oldArguments[0]);
$newArgs = [$firstArgument, $secondArgument];
$node->args = array_merge($newArgs, $oldArguments);
}
private function isConstantValue(Expr $expr): bool
{
$staticType = $this->nodeTypeResolver->getType($expr);
if ($staticType instanceof ConstantScalarType) {
return true;
}
return $staticType instanceof ConstantArrayType;
}
}