Skip to content

Commit 58785fa

Browse files
committed
Useful getPropertyReflection() shortcut in property hook virtual nodes
1 parent e8bceca commit 58785fa

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4792,7 +4792,19 @@ private function processPropertyHooks(
47924792
if (!$hookReflection instanceof PhpMethodFromParserNodeReflection) {
47934793
throw new ShouldNotHappenException();
47944794
}
4795-
$nodeCallback(new InPropertyHookNode($classReflection, $hookReflection, $hook), $hookScope);
4795+
4796+
if (!$classReflection->hasNativeProperty($propertyName)) {
4797+
throw new ShouldNotHappenException();
4798+
}
4799+
4800+
$propertyReflection = $classReflection->getNativeProperty($propertyName);
4801+
4802+
$nodeCallback(new InPropertyHookNode(
4803+
$classReflection,
4804+
$hookReflection,
4805+
$propertyReflection,
4806+
$hook,
4807+
), $hookScope);
47964808

47974809
if ($hook->body instanceof Expr) {
47984810
$this->processExprNode($stmt, $hook->body, $hookScope, $nodeCallback, ExpressionContext::createTopLevel());
@@ -4838,6 +4850,7 @@ private function processPropertyHooks(
48384850
array_merge($statementResult->getImpurePoints(), $methodImpurePoints),
48394851
$classReflection,
48404852
$hookReflection,
4853+
$propertyReflection,
48414854
), $hookScope);
48424855
}
48434856

src/Node/InPropertyHookNode.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\NodeAbstract;
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
9+
use PHPStan\Reflection\Php\PhpPropertyReflection;
910

1011
/**
1112
* @api
@@ -16,6 +17,7 @@ final class InPropertyHookNode extends NodeAbstract implements VirtualNode
1617
public function __construct(
1718
private ClassReflection $classReflection,
1819
private PhpMethodFromParserNodeReflection $hookReflection,
20+
private PhpPropertyReflection $propertyReflection,
1921
private Node\PropertyHook $originalNode,
2022
)
2123
{
@@ -32,6 +34,11 @@ public function getHookReflection(): PhpMethodFromParserNodeReflection
3234
return $this->hookReflection;
3335
}
3436

37+
public function getPropertyReflection(): PhpPropertyReflection
38+
{
39+
return $this->propertyReflection;
40+
}
41+
3542
public function getOriginalNode(): Node\PropertyHook
3643
{
3744
return $this->originalNode;

src/Node/PropertyHookReturnStatementsNode.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Analyser\StatementResult;
99
use PHPStan\Reflection\ClassReflection;
1010
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
11+
use PHPStan\Reflection\Php\PhpPropertyReflection;
1112

1213
/**
1314
* @api
@@ -28,6 +29,7 @@ public function __construct(
2829
private array $impurePoints,
2930
private ClassReflection $classReflection,
3031
private PhpMethodFromParserNodeReflection $hookReflection,
32+
private PhpPropertyReflection $propertyReflection,
3133
)
3234
{
3335
parent::__construct($hook->getAttributes());
@@ -88,6 +90,11 @@ public function getHookReflection(): PhpMethodFromParserNodeReflection
8890
return $this->hookReflection;
8991
}
9092

93+
public function getPropertyReflection(): PhpPropertyReflection
94+
{
95+
return $this->propertyReflection;
96+
}
97+
9198
public function getType(): string
9299
{
93100
return 'PHPStan_Node_PropertyHookReturnStatementsNode';

src/Rules/Properties/SetNonVirtualPropertyHookAssignRule.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ public function processNode(Node $node, Scope $scope): array
3737

3838
$propertyName = $hookReflection->getHookedPropertyName();
3939
$classReflection = $node->getClassReflection();
40-
if (!$classReflection->hasNativeProperty($propertyName)) {
41-
throw new ShouldNotHappenException();
42-
}
43-
44-
$propertyReflection = $classReflection->getNativeProperty($propertyName);
40+
$propertyReflection = $node->getPropertyReflection();
4541
if ($propertyReflection->isVirtual()->yes()) {
4642
return [];
4743
}

0 commit comments

Comments
 (0)