Skip to content

Commit 15fd706

Browse files
Fix rules that were applied even if no actual change was made (#270)
* Fix rules that were applied even if no actual change was made * Remove If_ from return typr
1 parent 480f1cf commit 15fd706

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

rules/DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ public function getNodeTypes(): array
6767
/**
6868
* @param ConstFetch|BitwiseOr|If_ $node
6969
*/
70-
public function refactor(Node $node): Expr|If_|null
70+
public function refactor(Node $node): Expr|null
7171
{
7272
if ($node instanceof If_) {
73-
return $this->refactorIf($node);
73+
$this->markConstantKnownInInnerStmts($node);
74+
return null;
7475
}
7576

7677
// skip as known
@@ -84,20 +85,18 @@ public function refactor(Node $node): Expr|If_|null
8485
]);
8586
}
8687

87-
private function refactorIf(If_ $if): ?If_
88+
private function markConstantKnownInInnerStmts(If_ $if): void
8889
{
8990
if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [
9091
JsonConstant::INVALID_UTF8_IGNORE,
9192
JsonConstant::INVALID_UTF8_SUBSTITUTE,
9293
])) {
93-
return null;
94+
return;
9495
}
9596

9697
$this->traverseNodesWithCallable($if, static function (Node $node): null {
9798
$node->setAttribute(self::PHP72_JSON_CONSTANT_IS_KNOWN, true);
9899
return null;
99100
});
100-
101-
return $if;
102101
}
103102
}

rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ public function getNodeTypes(): array
102102

103103
/**
104104
* @param ConstFetch|BitwiseOr|If_|TryCatch|Expression $node
105-
* @return null|Expr|If_|array<Expression|If_>
105+
* @return null|Expr|array<Expression|If_>
106106
*/
107-
public function refactor(Node $node): null|Expr|If_|array
107+
public function refactor(Node $node): null|Expr|array
108108
{
109109
if ($node instanceof If_) {
110-
return $this->refactorIf($node);
110+
$this->markConstantKnownInInnerStmts($node);
111+
return null;
111112
}
112113

113114
// skip as known
@@ -146,18 +147,16 @@ function (Node $subNode): ?int {
146147
return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]);
147148
}
148149

149-
private function refactorIf(If_ $if): ?If_
150+
private function markConstantKnownInInnerStmts(If_ $if): void
150151
{
151152
if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [JsonConstant::THROW_ON_ERROR])) {
152-
return null;
153+
return;
153154
}
154155

155156
$this->traverseNodesWithCallable($if, static function (Node $node): null {
156157
$node->setAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN, true);
157158
return null;
158159
});
159-
160-
return $if;
161160
}
162161

163162
private function resolveFuncCall(Expression $Expression): ?FuncCall

rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ public function getNodeTypes(): array
7777
public function refactor(Node $node): Node|null
7878
{
7979
if ($node instanceof Instanceof_) {
80-
return $this->refactorInstanceof($node);
80+
$this->markSkipInstanceof($node);
81+
return null;
8182
}
8283

8384
if ($node instanceof Ternary) {
84-
return $this->refactorTernary($node);
85+
$this->markSkipTernary($node);
86+
return null;
8587
}
8688

8789
if ($node->getAttribute(self::SKIP_NODE) === true) {
@@ -105,36 +107,34 @@ public function refactor(Node $node): Node|null
105107
);
106108
}
107109

108-
private function refactorInstanceof(Instanceof_ $instanceof): ?Instanceof_
110+
private function markSkipInstanceof(Instanceof_ $instanceof): void
109111
{
110112
if (! $this->isName($instanceof->class, 'ReflectionNamedType')) {
111-
return null;
113+
return;
112114
}
113115

114116
if (! $instanceof->expr instanceof MethodCall) {
115-
return null;
117+
return;
116118
}
117119

118120
// checked typed → safe
119121
$instanceof->expr->setAttribute(self::SKIP_NODE, true);
120-
return $instanceof;
121122
}
122123

123-
private function refactorTernary(Ternary $ternary): ?Ternary
124+
private function markSkipTernary(Ternary $ternary): void
124125
{
125126
if (! $ternary->if instanceof Expr) {
126-
return null;
127+
return;
127128
}
128129

129130
if (! $ternary->cond instanceof FuncCall) {
130-
return null;
131+
return;
131132
}
132133

133134
if (! $this->isName($ternary->cond, 'method_exists')) {
134-
return null;
135+
return;
135136
}
136137

137138
$ternary->if->setAttribute(self::SKIP_NODE, true);
138-
return $ternary;
139139
}
140140
}

0 commit comments

Comments
 (0)