Skip to content

Commit 6082812

Browse files
authored
[CodeQuality] Add all instanceof asserts for multiple nullable instances in a single run (#721)
* [CodeQuality] Add all instanceof asserts for multiple nullable instances in a single run Previously AddInstanceofAssertForNullableInstanceRector added one assert per run when several nullable variables were used within a single statement, or skipped whole methods containing control-flow statements. Now all needed asserts are added in one pass. * Collect all nullable variables per statement and skip control-flow bail Add the missing rule + collector logic so all instanceof asserts are added in a single run: match every nullable variable used within one statement, and stop bailing out of the collector on control-flow statements. * Limit scope to multi-nullable-per-statement; keep control-flow methods skipped Revert the collector control-flow change and restore the skip_with_if_else fixture. Keep a single fixture for the actual fix: multiple nullable variables used within one statement.
1 parent 188445b commit 6082812

2 files changed

Lines changed: 102 additions & 23 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
9+
10+
final class MultipleNullableInSingleStatement extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$first = $someObject->getSomeNestedObject();
16+
$second = $someObject->getSomeNestedObject();
17+
18+
$this->assertSame(1, $first->getNumber() + $second->getNumber());
19+
}
20+
21+
private function getSomeObject(): ?SomeClassUsedInTests
22+
{
23+
if (mt_rand(0, 1)) {
24+
return new SomeClassUsedInTests();
25+
}
26+
27+
return null;
28+
}
29+
}
30+
31+
?>
32+
-----
33+
<?php
34+
35+
declare(strict_types=1);
36+
37+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
38+
39+
use PHPUnit\Framework\TestCase;
40+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
41+
42+
final class MultipleNullableInSingleStatement extends TestCase
43+
{
44+
public function test(): void
45+
{
46+
$someObject = $this->getSomeObject();
47+
$this->assertInstanceOf(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests::class, $someObject);
48+
$first = $someObject->getSomeNestedObject();
49+
$second = $someObject->getSomeNestedObject();
50+
$this->assertInstanceOf(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeNestedObject::class, $first);
51+
$this->assertInstanceOf(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeNestedObject::class, $second);
52+
53+
$this->assertSame(1, $first->getNumber() + $second->getNumber());
54+
}
55+
56+
private function getSomeObject(): ?SomeClassUsedInTests
57+
{
58+
if (mt_rand(0, 1)) {
59+
return new SomeClassUsedInTests();
60+
}
61+
62+
return null;
63+
}
64+
}
65+
66+
?>

‎rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Stmt;
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PhpParser\Node\Stmt\Foreach_;
13-
use PhpParser\NodeVisitor;
1413
use Rector\PHPUnit\CodeQuality\NodeAnalyser\NullableObjectAssignCollector;
1514
use Rector\PHPUnit\CodeQuality\NodeFactory\AssertMethodCallFactory;
1615
use Rector\PHPUnit\CodeQuality\TypeAnalyzer\SimpleTypeAnalyzer;
@@ -117,28 +116,27 @@ public function refactor(Node $node): ?Node
117116

118117
$next = 0;
119118
foreach ($node->stmts as $key => $stmt) {
120-
// has callable on nullable variable of already collected name?
121-
$matchedNullableVariableNameToType = $this->matchedNullableVariableNameToType(
119+
// has callable on nullable variables of already collected names?
120+
// a single statement can use multiple nullable variables at once
121+
$matchedNullableVariableNameToTypes = $this->matchNullableVariableNameToTypes(
122122
$stmt,
123123
$variableNameToTypeCollection
124124
);
125-
if (! $matchedNullableVariableNameToType instanceof VariableNameToType) {
126-
continue;
127-
}
128125

129-
// adding type here + popping the variable name out
130-
$assertInstanceOfExpression = $this->assertMethodCallFactory->createAssertInstanceOf(
131-
$matchedNullableVariableNameToType
132-
);
133-
array_splice($node->stmts, $key + $next, 0, [$assertInstanceOfExpression]);
126+
foreach ($matchedNullableVariableNameToTypes as $matchedNullableVariableNameToType) {
127+
// adding type here + popping the variable name out
128+
$assertInstanceOfExpression = $this->assertMethodCallFactory->createAssertInstanceOf(
129+
$matchedNullableVariableNameToType
130+
);
131+
array_splice($node->stmts, $key + $next, 0, [$assertInstanceOfExpression]);
134132

135-
// remove variable name from nullable ones
136-
$hasChanged = true;
133+
$hasChanged = true;
137134

138-
// from now on, the variable is not nullable, remove to avoid double asserts
139-
$variableNameToTypeCollection->remove($matchedNullableVariableNameToType);
135+
// from now on, the variable is not nullable, remove to avoid double asserts
136+
$variableNameToTypeCollection->remove($matchedNullableVariableNameToType);
140137

141-
++$next;
138+
++$next;
139+
}
142140
}
143141

144142
if (! $hasChanged) {
@@ -148,15 +146,20 @@ public function refactor(Node $node): ?Node
148146
return $node;
149147
}
150148

151-
private function matchedNullableVariableNameToType(
149+
/**
150+
* @return VariableNameToType[]
151+
*/
152+
private function matchNullableVariableNameToTypes(
152153
Stmt $stmt,
153154
VariableNameToTypeCollection $variableNameToTypeCollection
154-
): ?VariableNameToType {
155-
$matchedNullableVariableNameToType = null;
155+
): array {
156+
$matchedNullableVariableNameToTypes = [];
157+
$matchedVariableNames = [];
156158

157159
$this->traverseNodesWithCallable($stmt, function (Node $node) use (
158160
$variableNameToTypeCollection,
159-
&$matchedNullableVariableNameToType
161+
&$matchedNullableVariableNameToTypes,
162+
&$matchedVariableNames
160163
): ?int {
161164
if (! $node instanceof MethodCall) {
162165
return null;
@@ -176,12 +179,22 @@ private function matchedNullableVariableNameToType(
176179
return null;
177180
}
178181

182+
// avoid duplicated assert for the same variable used multiple times in one statement
183+
if (in_array($variableName, $matchedVariableNames, true)) {
184+
return null;
185+
}
186+
179187
$matchedNullableVariableNameToType = $variableNameToTypeCollection->matchByVariableName($variableName);
188+
if (! $matchedNullableVariableNameToType instanceof VariableNameToType) {
189+
return null;
190+
}
191+
192+
$matchedVariableNames[] = $variableName;
193+
$matchedNullableVariableNameToTypes[] = $matchedNullableVariableNameToType;
180194

181-
// is the variable we're interested in?
182-
return NodeVisitor::STOP_TRAVERSAL;
195+
return null;
183196
});
184197

185-
return $matchedNullableVariableNameToType;
198+
return $matchedNullableVariableNameToTypes;
186199
}
187200
}

0 commit comments

Comments
 (0)