Skip to content

Commit 96110a9

Browse files
authored
[CodeQuality] Handle crash on property hook on OptionalParametersAfterRequiredRector (#6575)
* [CodeQuality] Handle crash on property hook on OptionalParametersAfterRequiredRector * fix * fix * fix * fix * fix
1 parent 617d06a commit 96110a9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;
4+
5+
final class SkipOnPropertyHook
6+
{
7+
public array $cookies {
8+
get => $a > get(CookieManager::class)->all();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;
4+
5+
final class SkipOnPropertyHook2
6+
{
7+
public array $cookies {
8+
get => get(CookieManager::class)->all();
9+
}
10+
}

src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public function processNodes(
305305
}
306306

307307
if ($node instanceof Property) {
308-
$this->processProperty($node, $mutatingScope);
308+
$this->processProperty($node, $mutatingScope, $nodeCallback);
309309
return;
310310
}
311311

@@ -599,7 +599,10 @@ private function processUnreachableStatementNode(
599599
$this->processNodes([$originalStmt], $filePath, $mutatingScope);
600600
}
601601

602-
private function processProperty(Property $property, MutatingScope $mutatingScope): void
602+
/**
603+
* @param callable(Node $node, MutatingScope $scope): void $nodeCallback
604+
*/
605+
private function processProperty(Property $property, MutatingScope $mutatingScope, callable $nodeCallback): void
603606
{
604607
foreach ($property->props as $propertyProperty) {
605608
$propertyProperty->setAttribute(AttributeKey::SCOPE, $mutatingScope);
@@ -608,6 +611,18 @@ private function processProperty(Property $property, MutatingScope $mutatingScop
608611
$propertyProperty->default->setAttribute(AttributeKey::SCOPE, $mutatingScope);
609612
}
610613
}
614+
615+
foreach ($property->hooks as $hook) {
616+
if ($hook->body === null) {
617+
continue;
618+
}
619+
620+
/** @var Stmt[] $stmts */
621+
$stmts = $hook->body instanceof Expr
622+
? [new Expression($hook->body)]
623+
: [$hook->body];
624+
$this->nodeScopeResolverProcessNodes($stmts, $mutatingScope, $nodeCallback);
625+
}
611626
}
612627

613628
private function processBinaryOp(BinaryOp $binaryOp, MutatingScope $mutatingScope): void

0 commit comments

Comments
 (0)