Skip to content

Commit bdd26a9

Browse files
committed
Updated Rector to commit 580b374ea3638fd50cf9b98b84445cd9fe53768e
rectorphp/rector-src@580b374 [DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector on dynamic variable check (#8057)
1 parent 7526bea commit bdd26a9

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,10 @@ private function shouldSkipFromVariable(Expr $expr): bool
137137
{
138138
/** @var Variable[] $variables */
139139
$variables = $this->betterNodeFinder->findInstancesOf($expr, [Variable::class]);
140-
$hasPlainVariable = \false;
141140
foreach ($variables as $variable) {
142141
if ($this->exprAnalyzer->isNonTypedFromParam($variable)) {
143142
return \true;
144143
}
145-
if (!$variable->name instanceof Expr) {
146-
$hasPlainVariable = \true;
147-
}
148144
$type = $this->nodeTypeResolver->getNativeType($variable);
149145
if ($type instanceof IntersectionType) {
150146
foreach ($type->getTypes() as $subType) {
@@ -154,13 +150,7 @@ private function shouldSkipFromVariable(Expr $expr): bool
154150
}
155151
}
156152
}
157-
// a dynamic variable assignment, e.g. ${$name} = ..., is invisible to native type inference, so the
158-
// condition variable may be overwritten at runtime and is not safe to evaluate as always true
159-
return $hasPlainVariable && $this->hasDynamicVariable();
160-
}
161-
private function hasDynamicVariable(): bool
162-
{
163-
return (bool) $this->betterNodeFinder->findFirst($this->getFile()->getNewStmts(), static fn(Node $node): bool => $node instanceof Variable && $node->name instanceof Expr);
153+
return \false;
164154
}
165155
private function shouldSkipExpr(Expr $expr): bool
166156
{

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '2.5.0';
22+
public const PACKAGE_VERSION = '580b374ea3638fd50cf9b98b84445cd9fe53768e';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-06-20 18:37:50';
27+
public const RELEASE_DATE = '2026-06-21 00:26:52';
2828
/**
2929
* @var int
3030
*/

src/NodeAnalyzer/ExprAnalyzer.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use PhpParser\Node\Scalar\InterpolatedString;
5050
use PhpParser\Node\Scalar\String_;
5151
use PHPStan\Analyser\Scope;
52+
use PHPStan\Type\Constant\ConstantStringType;
5253
use PHPStan\Type\MixedType;
5354
use PHPStan\Type\ObjectWithoutClassType;
5455
use PHPStan\Type\UnionType;
@@ -119,10 +120,20 @@ public function isNonTypedFromParam(Expr $expr): bool
119120
if (!$scope->hasVariableType((string) $this->nodeNameResolver->getName($expr))->yes()) {
120121
return \true;
121122
}
122-
if ($nativeType instanceof UnionType) {
123-
return !$nativeType->equals($type);
123+
if ($nativeType instanceof UnionType && !$nativeType->equals($type)) {
124+
return \true;
125+
}
126+
if (!$nativeType->isSuperTypeOf($type)->yes()) {
127+
return \true;
124128
}
125-
return !$nativeType->isSuperTypeOf($type)->yes();
129+
$definedVariables = $scope->getDefinedVariables();
130+
foreach ($definedVariables as $definedVariable) {
131+
$variableType = $scope->getVariableType($definedVariable);
132+
if ($variableType instanceof ConstantStringType && in_array($variableType->getValue(), $definedVariables, \true)) {
133+
return \true;
134+
}
135+
}
136+
return \false;
126137
}
127138
public function isDynamicExpr(Expr $expr): bool
128139
{

0 commit comments

Comments
 (0)