-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathAssertArrayCastedObjectToAssertSameRector.php
More file actions
173 lines (143 loc) · 4.47 KB
/
AssertArrayCastedObjectToAssertSameRector.php
File metadata and controls
173 lines (143 loc) · 4.47 KB
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
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Expression;
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\AssertArrayCastedObjectToAssertSameRectorTest
*/
final class AssertArrayCastedObjectToAssertSameRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ValueResolver $valueResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Split object casted to array to assert public properties to standalone assert calls',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestClass
{
public function test()
{
$someObject = new SomeObject();
$this->assertSame([
'name' => 'John',
'surname' => 'Doe',
], (array) $someObject));
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestClass
{
public function test()
{
$someObject = new SomeObject();
$this->assertSame('John', $someObject->name);
$this->assertSame('Doe', $someObject->surname);
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
* @return Expression[]|null
*/
public function refactor(Node $node): ?array
{
if (! $node->expr instanceof MethodCall) {
return null;
}
$methodCall = $node->expr;
if ($methodCall->isFirstClassCallable()) {
return null;
}
if (! $this->isName($methodCall->name, 'assertSame')) {
return null;
}
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$args = $methodCall->getArgs();
$firstArg = $args[0];
if (! $firstArg->value instanceof Node\Expr\Array_) {
return null;
}
// we need assert to non empty arary
$array = $firstArg->value;
if ($array->items === []) {
return null;
}
$arrayType = $this->getType($array);
if (! $arrayType instanceof ConstantArrayType) {
return null;
}
$secondArg = $args[1];
if (! $secondArg->value instanceof Array_) {
return null;
}
$castArray = $secondArg->value;
$castedExpr = $castArray->expr;
$castedExprType = $this->getType($castedExpr);
if (! $castedExprType instanceof ObjectType) {
return null;
}
$assertedArrayValues = $this->valueResolver->getValue($array);
if (! $this->hasAllKeysString($assertedArrayValues)) {
return null;
}
$standaloneExpressions = [];
foreach ($assertedArrayValues as $propertyName => $expectedValue) {
$args = $this->nodeFactory->createArgs([
$expectedValue,
new PropertyFetch($castedExpr, new Identifier($propertyName)),
]);
$assertSameMethodCall = new MethodCall(new Variable('this'), 'assertSame', $args);
$standaloneExpressions[] = new Expression($assertSameMethodCall);
}
return $standaloneExpressions;
}
/**
* @param array<mixed, mixed> $assertedArrayValues
*/
private function hasAllKeysString(array $assertedArrayValues): bool
{
// all keys must be string
foreach (array_keys($assertedArrayValues) as $key) {
if (! is_string($key)) {
return false;
}
}
return true;
}
}