Skip to content

Commit d8e8953

Browse files
committed
Bleeding edge - private property accessed through static::
1 parent bad2607 commit d8e8953

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

conf/config.level2.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ conditionalTags:
1111
phpstan.rules.rule: %featureToggles.classConstants%
1212
PHPStan\Rules\Methods\CallPrivateMethodThroughStaticRule:
1313
phpstan.rules.rule: %featureToggles.privateStaticCall%
14+
PHPStan\Rules\Properties\AccessPrivatePropertyThroughStaticRule:
15+
phpstan.rules.rule: %featureToggles.privateStaticCall%
1416

1517
rules:
1618
- PHPStan\Rules\Cast\EchoRule
@@ -60,6 +62,8 @@ services:
6062
class: PHPStan\Rules\Methods\CallPrivateMethodThroughStaticRule
6163
-
6264
class: PHPStan\Rules\PhpDoc\IncompatibleClassConstantPhpDocTypeRule
65+
-
66+
class: PHPStan\Rules\Properties\AccessPrivatePropertyThroughStaticRule
6367
-
6468
class: PHPStan\Rules\Generics\InterfaceAncestorsRule
6569
arguments:
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Properties;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Name;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
10+
11+
/**
12+
* @implements Rule<Node\Expr\StaticPropertyFetch>
13+
*/
14+
class AccessPrivatePropertyThroughStaticRule implements Rule
15+
{
16+
17+
public function getNodeType(): string
18+
{
19+
return Node\Expr\StaticPropertyFetch::class;
20+
}
21+
22+
public function processNode(Node $node, Scope $scope): array
23+
{
24+
if (!$node->name instanceof Node\VarLikeIdentifier) {
25+
return [];
26+
}
27+
if (!$node->class instanceof Name) {
28+
return [];
29+
}
30+
31+
$propertyName = $node->name->name;
32+
$className = $node->class;
33+
if ($className->toLowerString() !== 'static') {
34+
return [];
35+
}
36+
37+
$classType = $scope->resolveTypeByName($className);
38+
if (!$classType->hasProperty($propertyName)->yes()) {
39+
return [];
40+
}
41+
42+
$property = $classType->getProperty($propertyName, $scope);
43+
if (!$property->isPrivate()) {
44+
return [];
45+
}
46+
if (!$property->isStatic()) {
47+
return [];
48+
}
49+
50+
if ($scope->isInClass() && $scope->getClassReflection()->isFinal()) {
51+
return [];
52+
}
53+
54+
return [
55+
RuleErrorBuilder::message(sprintf(
56+
'Unsafe access to private property %s::$%s through static::.',
57+
$property->getDeclaringClass()->getDisplayName(),
58+
$propertyName
59+
))->build(),
60+
];
61+
}
62+
63+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Properties;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/** @extends RuleTestCase<AccessPrivatePropertyThroughStaticRule> */
9+
class AccessPrivatePropertyThroughStaticRuleTest extends RuleTestCase
10+
{
11+
12+
protected function getRule(): Rule
13+
{
14+
return new AccessPrivatePropertyThroughStaticRule();
15+
}
16+
17+
public function testRule(): void
18+
{
19+
$this->analyse([__DIR__ . '/data/access-private-property-static.php'], [
20+
[
21+
'Unsafe access to private property AccessPrivatePropertyThroughStatic\Foo::$foo through static::.',
22+
13,
23+
],
24+
]);
25+
}
26+
27+
}

tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ public function testAccessStaticProperties(): void
174174
209,
175175
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
176176
],
177+
[
178+
'Static access to instance property AccessWithStatic::$bar.',
179+
223,
180+
],
181+
[
182+
'Access to an undefined static property static(AccessWithStatic)::$nonexistent.',
183+
224,
184+
],
177185
]);
178186
}
179187

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace AccessPrivatePropertyThroughStatic;
4+
5+
class Foo
6+
{
7+
8+
private static $foo;
9+
private $bar;
10+
11+
public function doBar()
12+
{
13+
static::$foo;
14+
static::$bar; // reported by different rule
15+
static::$nonexistent; // reported by different rule
16+
}
17+
18+
}
19+
20+
final class Bar
21+
{
22+
23+
private static $foo;
24+
private $bar;
25+
26+
public function doBar()
27+
{
28+
static::$foo;
29+
static::$bar;
30+
static::$nonexistent;
31+
}
32+
33+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,18 @@ public function doBar(TraitWithStaticProperty $a): void
210210
}
211211

212212
}
213+
214+
class AccessWithStatic
215+
{
216+
217+
private static $foo;
218+
private $bar;
219+
220+
public function doBar()
221+
{
222+
static::$foo; // reported by different rule
223+
static::$bar;
224+
static::$nonexistent;
225+
}
226+
227+
}

0 commit comments

Comments
 (0)