Skip to content

Commit 270326a

Browse files
committed
Bleeding edge - private constant accessed through static::
1 parent d8e8953 commit 270326a

7 files changed

+142
-3
lines changed

conf/config.level2.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ parameters:
77
checkPhpDocMissingReturn: true
88

99
conditionalTags:
10+
PHPStan\Rules\Classes\AccessPrivateConstantThroughStaticRule:
11+
phpstan.rules.rule: %featureToggles.privateStaticCall%
1012
PHPStan\Rules\PhpDoc\IncompatibleClassConstantPhpDocTypeRule:
1113
phpstan.rules.rule: %featureToggles.classConstants%
1214
PHPStan\Rules\Methods\CallPrivateMethodThroughStaticRule:
@@ -40,6 +42,8 @@ rules:
4042
- PHPStan\Rules\PhpDoc\InvalidThrowsPhpDocValueRule
4143

4244
services:
45+
-
46+
class: PHPStan\Rules\Classes\AccessPrivateConstantThroughStaticRule
4347
-
4448
class: PHPStan\Rules\Classes\MixinRule
4549
arguments:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Classes;
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\ClassConstFetch>
13+
*/
14+
class AccessPrivateConstantThroughStaticRule implements Rule
15+
{
16+
17+
public function getNodeType(): string
18+
{
19+
return Node\Expr\ClassConstFetch::class;
20+
}
21+
22+
public function processNode(Node $node, Scope $scope): array
23+
{
24+
if (!$node->name instanceof Node\Identifier) {
25+
return [];
26+
}
27+
if (!$node->class instanceof Name) {
28+
return [];
29+
}
30+
31+
$constantName = $node->name->name;
32+
$className = $node->class;
33+
if ($className->toLowerString() !== 'static') {
34+
return [];
35+
}
36+
37+
$classType = $scope->resolveTypeByName($className);
38+
if (!$classType->hasConstant($constantName)->yes()) {
39+
return [];
40+
}
41+
42+
$constant = $classType->getConstant($constantName);
43+
if (!$constant->isPrivate()) {
44+
return [];
45+
}
46+
47+
if ($scope->isInClass() && $scope->getClassReflection()->isFinal()) {
48+
return [];
49+
}
50+
51+
return [
52+
RuleErrorBuilder::message(sprintf(
53+
'Unsafe access to private constant %s::%s through static::.',
54+
$constant->getDeclaringClass()->getDisplayName(),
55+
$constantName
56+
))->build(),
57+
];
58+
}
59+
60+
}

src/Type/Php/ArrayFillFunctionReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3737
if (
3838
$startIndexType instanceof ConstantIntegerType
3939
&& $numberType instanceof ConstantIntegerType
40-
&& $numberType->getValue() <= static::MAX_SIZE_USE_CONSTANT_ARRAY
40+
&& $numberType->getValue() <= self::MAX_SIZE_USE_CONSTANT_ARRAY
4141
) {
4242
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
4343
$nextIndex = $startIndexType->getValue();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Classes;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<AccessPrivateConstantThroughStaticRule>
10+
*/
11+
class AccessPrivateConstantThroughStaticRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new AccessPrivateConstantThroughStaticRule();
17+
}
18+
19+
public function testRule(): void
20+
{
21+
$this->analyse([__DIR__ . '/data/access-private-constant-static.php'], [
22+
[
23+
'Unsafe access to private constant AccessPrivateConstantThroughStatic\Foo::FOO through static::.',
24+
12,
25+
],
26+
]);
27+
}
28+
29+
}

tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,17 @@ public function testClassConstantVisibility(): void
143143
'Cannot access constant FOO on int|string.',
144144
116,
145145
],
146+
[
147+
'Access to undefined constant static(ClassConstantVisibility\AccessWithStatic)::BAR.',
148+
129,
149+
],
146150
[
147151
'Class ClassConstantVisibility\Foo referenced with incorrect case: ClassConstantVisibility\FOO.',
148-
122,
152+
135,
149153
],
150154
[
151155
'Access to private constant PRIVATE_FOO of class ClassConstantVisibility\Foo.',
152-
122,
156+
135,
153157
],
154158
]);
155159
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace AccessPrivateConstantThroughStatic;
4+
5+
class Foo
6+
{
7+
8+
private const FOO = 1;
9+
10+
public function doFoo()
11+
{
12+
static::FOO;
13+
static::BAR; // reported by a different rule
14+
}
15+
16+
}
17+
18+
final class Bar
19+
{
20+
21+
private const FOO = 1;
22+
23+
public function doFoo()
24+
{
25+
static::FOO;
26+
static::BAR; // reported by a different rule
27+
}
28+
29+
}

tests/PHPStan/Rules/Classes/data/class-constant-visibility.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ public function doIpsum(WithFooConstant $foo)
118118

119119
}
120120

121+
class AccessWithStatic
122+
{
123+
124+
private const FOO = 1;
125+
126+
public function doFoo()
127+
{
128+
static::FOO; // reported by a different rule
129+
static::BAR;
130+
}
131+
132+
}
133+
121134
function () {
122135
FOO::PRIVATE_FOO;
123136
};

0 commit comments

Comments
 (0)