Skip to content

Commit f0bff68

Browse files
lookymanondrejmirtes
authored andcommitted
Unwrap Stmt\Expression nodes
1 parent 10a6a3e commit f0bff68

File tree

3 files changed

+141
-98
lines changed

3 files changed

+141
-98
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ public function processNodes(
163163
if (!($node instanceof \PhpParser\Node)) {
164164
continue;
165165
}
166+
if ($node instanceof Node\Stmt\Expression) {
167+
$node = $node->expr;
168+
}
166169

167170
if ($scope->getInFunctionCall() !== null && $node instanceof Arg) {
168171
$functionCall = $scope->getInFunctionCall();
@@ -247,6 +250,10 @@ private function specifyProperty(Scope $scope, Expr $expr): Scope
247250

248251
private function specifyFetchedPropertyForInnerScope(Node $node, Scope $inScope, bool $inEarlyTermination, Scope &$scope): void
249252
{
253+
if ($node instanceof Node\Stmt\Expression) {
254+
$node = $node->expr;
255+
}
256+
250257
if ($inEarlyTermination === $inScope->isNegated()) {
251258
if ($node instanceof Isset_) {
252259
foreach ($node->vars as $var) {
@@ -276,6 +283,10 @@ private function specifyFetchedPropertyForInnerScope(Node $node, Scope $inScope,
276283

277284
private function lookForArrayDestructuringArray(Scope $scope, Node $node): Scope
278285
{
286+
if ($node instanceof Node\Stmt\Expression) {
287+
$node = $node->expr;
288+
}
289+
279290
if ($node instanceof Array_) {
280291
foreach ($node->items as $item) {
281292
if ($item === null) {
@@ -867,6 +878,10 @@ private function ensureNonNullability(
867878
bool $findMethods
868879
): Scope
869880
{
881+
if ($node instanceof Node\Stmt\Expression) {
882+
$node = $node->expr;
883+
}
884+
870885
$scope = $this->assignVariable($scope, $node, TrinaryLogic::createYes());
871886
$nodeToSpecify = $node;
872887
while (
@@ -950,6 +965,10 @@ private function lookForAssigns(
950965
LookForAssignsSettings $lookForAssignsSettings
951966
): Scope
952967
{
968+
if ($node instanceof Node\Stmt\Expression) {
969+
$node = $node->expr;
970+
}
971+
953972
if ($node instanceof StaticVar) {
954973
if (!is_string($node->var->name)) {
955974
throw new \PHPStan\ShouldNotHappenException();
@@ -1339,6 +1358,10 @@ private function updateScopeForVariableAssign(
13391358
LookForAssignsSettings $lookForAssignsSettings
13401359
): Scope
13411360
{
1361+
if ($node instanceof Node\Stmt\Expression) {
1362+
$node = $node->expr;
1363+
}
1364+
13421365
if ($node instanceof Assign || $node instanceof AssignRef || $node instanceof Expr\AssignOp || $node instanceof Node\Stmt\Global_) {
13431366
if ($node instanceof Assign || $node instanceof AssignRef || $node instanceof Expr\AssignOp) {
13441367
$scope = $this->lookForAssigns($scope, $node->var, TrinaryLogic::createYes(), $lookForAssignsSettings);
@@ -1438,6 +1461,10 @@ private function assignVariable(
14381461
?Type $subNodeType = null
14391462
): Scope
14401463
{
1464+
if ($var instanceof Node\Stmt\Expression) {
1465+
$var = $var->expr;
1466+
}
1467+
14411468
if ($var instanceof Variable && is_string($var->name)) {
14421469
$scope = $scope->assignVariable($var->name, $subNodeType !== null ? $subNodeType : new MixedType(), $certainty);
14431470
} elseif ($var instanceof ArrayDimFetch) {
@@ -1588,6 +1615,10 @@ private function lookForAssignsInBranches(
15881615
private function findEarlyTermination(array $statements, Scope $scope): ?\PhpParser\Node
15891616
{
15901617
foreach ($statements as $statement) {
1618+
if ($statement instanceof Node\Stmt\Expression) {
1619+
$statement = $statement->expr;
1620+
}
1621+
15911622
$statement = $this->findStatementEarlyTermination($statement, $scope);
15921623
if ($statement !== null) {
15931624
return $statement;
@@ -1599,6 +1630,10 @@ private function findEarlyTermination(array $statements, Scope $scope): ?\PhpPar
15991630

16001631
private function findStatementEarlyTermination(Node $statement, Scope $scope): ?\PhpParser\Node
16011632
{
1633+
if ($statement instanceof Node\Stmt\Expression) {
1634+
$statement = $statement->expr;
1635+
}
1636+
16021637
if (
16031638
$statement instanceof Throw_
16041639
|| $statement instanceof Return_
@@ -1737,6 +1772,10 @@ private function processTraitUse(Node\Stmt\TraitUse $node, Scope $classScope, \C
17371772
private function processNodesForTraitUse($node, string $traitName, Scope $classScope, \Closure $nodeCallback): void
17381773
{
17391774
if ($node instanceof Node) {
1775+
if ($node instanceof Node\Stmt\Expression) {
1776+
$node = $node->expr;
1777+
}
1778+
17401779
if ($node instanceof Node\Stmt\Trait_ && $traitName === (string) $node->namespacedName) {
17411780
$this->processNodes($node->stmts, $classScope->enterFirstLevelStatements(), $nodeCallback);
17421781
return;

src/Rules/Classes/RequireParentConstructCallRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ private function callsParentConstruct(Node $parserNode): bool
8080
}
8181

8282
foreach ($parserNode->stmts as $statement) {
83+
if ($statement instanceof Node\Stmt\Expression) {
84+
$statement = $statement->expr;
85+
}
86+
8387
$statement = $this->ignoreErrorSuppression($statement);
8488
if ($statement instanceof \PhpParser\Node\Expr\StaticCall) {
8589
if (

0 commit comments

Comments
 (0)