From 988e6998dbe1017b362eda9abd9583a5579a7009 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 11 Dec 2024 18:59:39 +0700 Subject: [PATCH] [CodeQuality] Handle crash on unary minums on OptionalParametersAfterRequiredRector (#6547) --- .../skip_first_class_callable_in_unary.php.inc | 15 +++++++++++++++ .../PHPStan/Scope/PHPStanNodeScopeResolver.php | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector/Fixture/skip_first_class_callable_in_unary.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector/Fixture/skip_first_class_callable_in_unary.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector/Fixture/skip_first_class_callable_in_unary.php.inc new file mode 100644 index 0000000000..b9fa95601a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector/Fixture/skip_first_class_callable_in_unary.php.inc @@ -0,0 +1,15 @@ +textElement(...); + } + + public function textElement() { return 1; } +} diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 5cc35621d4..f602b47e3a 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -24,10 +24,16 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\NullsafeMethodCall; +use PhpParser\Node\Expr\PostDec; +use PhpParser\Node\Expr\PostInc; +use PhpParser\Node\Expr\PreDec; +use PhpParser\Node\Expr\PreInc; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Ternary; +use PhpParser\Node\Expr\UnaryMinus; +use PhpParser\Node\Expr\UnaryPlus; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Expr\Yield_; use PhpParser\Node\Expr\YieldFrom; @@ -180,12 +186,18 @@ public function processNodes( $node instanceof Return_ || $node instanceof EnumCase || $node instanceof Cast || - $node instanceof YieldFrom + $node instanceof YieldFrom || + $node instanceof UnaryMinus || + $node instanceof UnaryPlus ) && $node->expr instanceof Expr) { $node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope); return; } + if ($node instanceof PostInc || $node instanceof PostDec || $node instanceof PreInc || $node instanceof PreDec) { + $node->var->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + if ($node instanceof Assign || $node instanceof AssignOp) { $this->processAssign($node, $mutatingScope);