Skip to content

Commit 35fce62

Browse files
committed
Useful getPropertyReflection() shortcut in property hook virtual nodes
1 parent ef83213 commit 35fce62

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
@@ -4794,7 +4794,19 @@ private function processPropertyHooks(
47944794
if (!$hookReflection instanceof PhpMethodFromParserNodeReflection) {
47954795
throw new ShouldNotHappenException();
47964796
}
4797-
$nodeCallback(new InPropertyHookNode($classReflection, $hookReflection, $hook), $hookScope);
4797+
4798+
if (!$classReflection->hasNativeProperty($propertyName)) {
4799+
throw new ShouldNotHappenException();
4800+
}
4801+
4802+
$propertyReflection = $classReflection->getNativeProperty($propertyName);
4803+
4804+
$nodeCallback(new InPropertyHookNode(
4805+
$classReflection,
4806+
$hookReflection,
4807+
$propertyReflection,
4808+
$hook,
4809+
), $hookScope);
47984810

47994811
if ($hook->body instanceof Expr) {
48004812
$this->processExprNode($stmt, $hook->body, $hookScope, $nodeCallback, ExpressionContext::createTopLevel());
@@ -4840,6 +4852,7 @@ private function processPropertyHooks(
48404852
array_merge($statementResult->getImpurePoints(), $methodImpurePoints),
48414853
$classReflection,
48424854
$hookReflection,
4855+
$propertyReflection,
48434856
), $hookScope);
48444857
}
48454858

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)