Skip to content

Commit 41837b4

Browse files
committed
AccessStaticPropertiesRule - fixed blindspot about parent::
1 parent 70572a1 commit 41837b4

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/Rules/Properties/AccessStaticPropertiesRule.php

-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPStan\Rules\Rule;
1616
use PHPStan\Rules\RuleErrorBuilder;
1717
use PHPStan\Rules\RuleLevelHelper;
18-
use PHPStan\ShouldNotHappenException;
1918
use PHPStan\Type\Constant\ConstantStringType;
2019
use PHPStan\Type\ErrorType;
2120
use PHPStan\Type\StringType;
@@ -108,16 +107,6 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
108107
];
109108
}
110109

111-
if ($scope->getFunctionName() === null) {
112-
throw new ShouldNotHappenException();
113-
}
114-
115-
$currentMethodReflection = $scope->getClassReflection()->getNativeMethod($scope->getFunctionName());
116-
if (!$currentMethodReflection->isStatic()) {
117-
// calling parent::method() from instance method
118-
return [];
119-
}
120-
121110
$classType = $scope->resolveTypeByName($node->class);
122111
} else {
123112
if (!$this->reflectionProvider->hasClass($class)) {

tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function testAccessStaticProperties(): void
4747
'Static access to instance property FooAccessStaticProperties::$loremIpsum.',
4848
26,
4949
],
50+
[
51+
'Static access to instance property FooAccessStaticProperties::$loremIpsum.',
52+
32,
53+
],
5054
[
5155
'IpsumAccessStaticProperties::ipsum() accesses parent::$lorem but IpsumAccessStaticProperties does not extend any class.',
5256
42,
@@ -250,6 +254,14 @@ public function testAccessStaticProperties(): void
250254
'Access to an undefined static property AllowsDynamicProperties::$foo.',
251255
248,
252256
],
257+
[
258+
'Static access to instance property ParentClassWithInstanceProperty::$i.',
259+
267,
260+
],
261+
[
262+
'Access to an undefined static property ParentClassWithInstanceProperty::$j.',
263+
268,
264+
],
253265
]);
254266
}
255267

tests/PHPStan/Rules/Properties/data/access-static-properties.php

+18
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,21 @@ public function doFoo()
251251
}
252252

253253
}
254+
255+
class ParentClassWithInstanceProperty
256+
{
257+
258+
public int $i = 0;
259+
260+
}
261+
262+
class ChildClassAccessingParentProperty extends ParentClassWithInstanceProperty
263+
{
264+
265+
public function doFoo(): void
266+
{
267+
echo parent::$i;
268+
echo parent::$j;
269+
}
270+
271+
}

0 commit comments

Comments
 (0)