Skip to content

Commit

Permalink
AccessStaticPropertiesRule - fixed blindspot about parent::
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 28, 2024
1 parent 70572a1 commit 41837b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
11 changes: 0 additions & 11 deletions src/Rules/Properties/AccessStaticPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -108,16 +107,6 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
];
}

if ($scope->getFunctionName() === null) {
throw new ShouldNotHappenException();
}

$currentMethodReflection = $scope->getClassReflection()->getNativeMethod($scope->getFunctionName());
if (!$currentMethodReflection->isStatic()) {
// calling parent::method() from instance method
return [];
}

$classType = $scope->resolveTypeByName($node->class);
} else {
if (!$this->reflectionProvider->hasClass($class)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function testAccessStaticProperties(): void
'Static access to instance property FooAccessStaticProperties::$loremIpsum.',
26,
],
[
'Static access to instance property FooAccessStaticProperties::$loremIpsum.',
32,
],
[
'IpsumAccessStaticProperties::ipsum() accesses parent::$lorem but IpsumAccessStaticProperties does not extend any class.',
42,
Expand Down Expand Up @@ -250,6 +254,14 @@ public function testAccessStaticProperties(): void
'Access to an undefined static property AllowsDynamicProperties::$foo.',
248,
],
[
'Static access to instance property ParentClassWithInstanceProperty::$i.',
267,
],
[
'Access to an undefined static property ParentClassWithInstanceProperty::$j.',
268,
],
]);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Properties/data/access-static-properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,21 @@ public function doFoo()
}

}

class ParentClassWithInstanceProperty
{

public int $i = 0;

}

class ChildClassAccessingParentProperty extends ParentClassWithInstanceProperty
{

public function doFoo(): void
{
echo parent::$i;
echo parent::$j;
}

}

0 comments on commit 41837b4

Please sign in to comment.