Skip to content

Commit 580b374

Browse files
authored
[DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector on dynamic variable check (#8057)
* [DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector * Fix * Fix * final touch: move to ExprAnalyzer * clean up $scope->hasVariableType() check inside loop defined variables * move check on defined variables on last resort after other check * add fixture to proof scoped dynamic variable
1 parent 13940f3 commit 580b374

3 files changed

Lines changed: 84 additions & 21 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
4+
5+
class ScopedDynamicVariable
6+
{
7+
public function build(array $params): string
8+
{
9+
$addHelpMessage = true;
10+
11+
foreach (['addHelpMessage'] as $f) {
12+
if (isset($params[$f])) {
13+
${$f} = (bool) $params[$f];
14+
}
15+
}
16+
17+
if ($addHelpMessage) {
18+
return 'help';
19+
}
20+
21+
return 'no help';
22+
}
23+
24+
public function differentScope()
25+
{
26+
$int = 1;
27+
if ($int === 1) {
28+
return 'hi';
29+
}
30+
}
31+
}
32+
33+
?>
34+
-----
35+
<?php
36+
37+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
38+
39+
class ScopedDynamicVariable
40+
{
41+
public function build(array $params): string
42+
{
43+
$addHelpMessage = true;
44+
45+
foreach (['addHelpMessage'] as $f) {
46+
if (isset($params[$f])) {
47+
${$f} = (bool) $params[$f];
48+
}
49+
}
50+
51+
if ($addHelpMessage) {
52+
return 'help';
53+
}
54+
55+
return 'no help';
56+
}
57+
58+
public function differentScope()
59+
{
60+
$int = 1;
61+
return 'hi';
62+
}
63+
}
64+
65+
?>

‎rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,11 @@ private function shouldSkipFromVariable(Expr $expr): bool
150150
/** @var Variable[] $variables */
151151
$variables = $this->betterNodeFinder->findInstancesOf($expr, [Variable::class]);
152152

153-
$hasPlainVariable = false;
154153
foreach ($variables as $variable) {
155154
if ($this->exprAnalyzer->isNonTypedFromParam($variable)) {
156155
return true;
157156
}
158157

159-
if (! $variable->name instanceof Expr) {
160-
$hasPlainVariable = true;
161-
}
162-
163158
$type = $this->nodeTypeResolver->getNativeType($variable);
164159
if ($type instanceof IntersectionType) {
165160
foreach ($type->getTypes() as $subType) {
@@ -170,18 +165,7 @@ private function shouldSkipFromVariable(Expr $expr): bool
170165
}
171166
}
172167

173-
// a dynamic variable assignment, e.g. ${$name} = ..., is invisible to native type inference, so the
174-
// condition variable may be overwritten at runtime and is not safe to evaluate as always true
175-
return $hasPlainVariable && $this->hasDynamicVariable();
176-
}
177-
178-
private function hasDynamicVariable(): bool
179-
{
180-
return (bool) $this->betterNodeFinder->findFirst(
181-
$this->getFile()
182-
->getNewStmts(),
183-
static fn (Node $node): bool => $node instanceof Variable && $node->name instanceof Expr
184-
);
168+
return false;
185169
}
186170

187171
private function shouldSkipExpr(Expr $expr): bool

‎src/NodeAnalyzer/ExprAnalyzer.php‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use PhpParser\Node\Scalar\InterpolatedString;
5151
use PhpParser\Node\Scalar\String_;
5252
use PHPStan\Analyser\Scope;
53+
use PHPStan\Type\Constant\ConstantStringType;
5354
use PHPStan\Type\MixedType;
5455
use PHPStan\Type\ObjectWithoutClassType;
5556
use PHPStan\Type\UnionType;
@@ -168,12 +169,25 @@ public function isNonTypedFromParam(Expr $expr): bool
168169
return true;
169170
}
170171

171-
if ($nativeType instanceof UnionType) {
172-
return ! $nativeType->equals($type);
172+
if ($nativeType instanceof UnionType && ! $nativeType->equals($type)) {
173+
return true;
173174
}
174175

175-
return ! $nativeType->isSuperTypeOf($type)
176-
->yes();
176+
if (! $nativeType->isSuperTypeOf($type)
177+
->yes()) {
178+
return true;
179+
}
180+
181+
$definedVariables = $scope->getDefinedVariables();
182+
foreach ($definedVariables as $definedVariable) {
183+
$variableType = $scope->getVariableType($definedVariable);
184+
if ($variableType instanceof ConstantStringType
185+
&& in_array($variableType->getValue(), $definedVariables, true)) {
186+
return true;
187+
}
188+
}
189+
190+
return false;
177191
}
178192

179193
public function isDynamicExpr(Expr $expr): bool

0 commit comments

Comments
 (0)