Skip to content

Commit c4c7b8c

Browse files
committed
Utilize PropertyReflectionFinder
1 parent aa238e2 commit c4c7b8c

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/Rules/Variables/UnsetRule.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\IdentifierRuleError;
8+
use PHPStan\Rules\Properties\PropertyReflectionFinder;
89
use PHPStan\Rules\Rule;
910
use PHPStan\Rules\RuleErrorBuilder;
1011
use PHPStan\Type\VerbosityLevel;
@@ -17,6 +18,12 @@
1718
final class UnsetRule implements Rule
1819
{
1920

21+
public function __construct(
22+
private PropertyReflectionFinder $propertyReflectionFinder,
23+
)
24+
{
25+
}
26+
2027
public function getNodeType(): string
2128
{
2229
return Node\Stmt\Unset_::class;
@@ -73,26 +80,30 @@ private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError
7380
$node instanceof Node\Expr\PropertyFetch
7481
&& $node->name instanceof Node\Identifier
7582
) {
76-
$type = $scope->getType($node->var);
77-
foreach ($type->getObjectClassReflections() as $classReflection) {
78-
if (!$classReflection->hasNativeProperty($node->name->name)) {
79-
continue;
80-
}
81-
82-
$propertyReflection = $classReflection->getNativeProperty($node->name->name);
83-
if ($propertyReflection->isReadOnly() || $propertyReflection->isReadOnlyByPhpDoc()) {
84-
return RuleErrorBuilder::message(
85-
sprintf(
86-
'Cannot unset %s property %s of %s.',
87-
$propertyReflection->isReadOnly() ? 'readonly' : '@readonly',
88-
$node->name->name,
89-
$type->describe(VerbosityLevel::value()),
90-
),
91-
)
92-
->line($node->getStartLine())
93-
->identifier($propertyReflection->isReadOnly() ? 'unset.readOnlyProperty' : 'unset.readOnlyPropertyByPhpDoc')
94-
->build();
95-
}
83+
$foundPropertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($node, $scope);
84+
if ($foundPropertyReflection === null) {
85+
return null;
86+
}
87+
88+
$propertyReflection = $foundPropertyReflection->getNativeReflection();
89+
if ($propertyReflection === null) {
90+
return null;
91+
}
92+
93+
if ($propertyReflection->isReadOnly() || $propertyReflection->isReadOnlyByPhpDoc()) {
94+
$type = $scope->getType($node->var);
95+
96+
return RuleErrorBuilder::message(
97+
sprintf(
98+
'Cannot unset %s property %s of %s.',
99+
$propertyReflection->isReadOnly() ? 'readonly' : '@readonly',
100+
$node->name->name,
101+
$type->describe(VerbosityLevel::value()),
102+
),
103+
)
104+
->line($node->getStartLine())
105+
->identifier($propertyReflection->isReadOnly() ? 'unset.readOnlyProperty' : 'unset.readOnlyPropertyByPhpDoc')
106+
->build();
96107
}
97108
}
98109

tests/PHPStan/Rules/Variables/UnsetRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Variables;
44

5+
use PHPStan\Rules\Properties\PropertyReflectionFinder;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78

@@ -13,7 +14,7 @@ class UnsetRuleTest extends RuleTestCase
1314

1415
protected function getRule(): Rule
1516
{
16-
return new UnsetRule();
17+
return new UnsetRule(self::getContainer()->getByType(PropertyReflectionFinder::class));
1718
}
1819

1920
public function testUnsetRule(): void

0 commit comments

Comments
 (0)