Skip to content

Commit deb0911

Browse files
authored
Detect accessing static property as non static
1 parent 08465ec commit deb0911

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/Rules/Properties/AccessPropertiesCheck.php

+10
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
160160
}
161161

162162
$propertyReflection = $type->getProperty($name, $scope);
163+
if ($propertyReflection->isStatic()) {
164+
return [
165+
RuleErrorBuilder::message(sprintf(
166+
'Non-static access to static property %s::$%s.',
167+
$propertyReflection->getDeclaringClass()->getDisplayName(),
168+
$name,
169+
))->identifier('staticProperty.nonStaticAccess')->build(),
170+
];
171+
}
172+
163173
if ($write) {
164174
if ($scope->canWriteProperty($propertyReflection)) {
165175
return [];

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ public function testAccessPropertiesOnThisOnly(): void
344344
);
345345
}
346346

347+
public function testBug12692(): void
348+
{
349+
$this->checkThisOnly = false;
350+
$this->checkUnionTypes = false;
351+
$this->checkDynamicProperties = false;
352+
$this->analyse([__DIR__ . '/data/bug-12692.php'], [[
353+
'Non-static access to static property Bug12692\Foo::$static.',
354+
14,
355+
]]);
356+
}
357+
347358
public function testAccessPropertiesAfterIsNullInBooleanOr(): void
348359
{
349360
$this->checkThisOnly = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug12692;
6+
7+
class Foo
8+
{
9+
10+
public static $static;
11+
12+
public function foo()
13+
{
14+
$this->static;
15+
}
16+
17+
}

0 commit comments

Comments
 (0)