Skip to content

Commit 1514045

Browse files
gnutixclaude
andcommitted
Check static property access through constant class-name strings and class-string unions
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 184d34e commit 1514045

3 files changed

Lines changed: 180 additions & 2 deletions

File tree

‎src/Rules/Properties/AccessStaticPropertiesCheck.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\DependencyInjection\AutowiredParameter;
1717
use PHPStan\DependencyInjection\AutowiredService;
1818
use PHPStan\Internal\SprintfHelper;
19+
use PHPStan\Node\Expr\TypeExpr;
1920
use PHPStan\Php\PhpVersion;
2021
use PHPStan\Reflection\ReflectionProvider;
2122
use PHPStan\Rules\ClassNameCheck;
@@ -175,9 +176,24 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
175176
$classType = $scope->resolveTypeByName($node->class);
176177
}
177178
} else {
179+
$exprToCheck = NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->class);
180+
$exprType = $scope->getType($exprToCheck);
181+
if ($exprType->isClassString()->yes()) {
182+
// A constant class-name string or a class-string type describes the
183+
// object it names, so route the check through that object type. This
184+
// makes constant-string / class-string unions behave exactly like
185+
// object unions with regard to rule levels (member filtering when
186+
// checkUnionTypes is off, whole-union check when it is on).
187+
$objectType = $exprType->getClassStringObjectType();
188+
if (count($objectType->getObjectClassNames()) === 0) {
189+
// plain class-string resolves to ObjectWithoutClassType, keep silent
190+
return [];
191+
}
192+
$exprToCheck = new TypeExpr($objectType);
193+
}
178194
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
179195
$scope,
180-
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->class),
196+
$exprToCheck,
181197
sprintf('Access to static property $%s on an unknown class %%s.', SprintfHelper::escapeFormatString($name)),
182198
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasStaticProperty($name)->yes(),
183199
);

‎tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
class AccessStaticPropertiesRuleTest extends RuleTestCase
2222
{
2323

24+
private bool $checkUnionTypes = true;
25+
2426
protected function getRule(): Rule
2527
{
2628
$reflectionProvider = self::createReflectionProvider();
2729
$ruleLevelHelper = new RuleLevelHelper(
2830
$reflectionProvider,
2931
checkNullables: true,
3032
checkThisOnly: false,
31-
checkUnionTypes: true,
33+
checkUnionTypes: $this->checkUnionTypes,
3234
checkExplicitMixed: false,
3335
checkImplicitMixed: false,
3436
checkBenevolentUnionTypes: false,
@@ -396,4 +398,55 @@ public function testBug15002(): void
396398
$this->analyse([__DIR__ . '/../Methods/data/bug-15002.php'], []);
397399
}
398400

401+
#[RequiresPhp('>= 8.0.0')]
402+
public function testStaticPropertyAccessOnStringUnions(): void
403+
{
404+
$this->analyse([__DIR__ . '/data/static-property-string-unions.php'], [
405+
[
406+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
407+
32,
408+
],
409+
[
410+
'Access to an undefined static property StaticPropertyStringUnions\Baz|StaticPropertyStringUnions\Foo::$create.',
411+
38,
412+
],
413+
[
414+
'Access to an undefined static property StaticPropertyStringUnions\Baz|StaticPropertyStringUnions\Foo::$create.',
415+
50,
416+
],
417+
[
418+
'Access to an undefined static property StaticPropertyStringUnions\Baz|StaticPropertyStringUnions\Foo::$create.',
419+
58,
420+
],
421+
[
422+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
423+
66,
424+
],
425+
[
426+
'Static access to instance property StaticPropertyStringUnions\Foo::$doBar.',
427+
80,
428+
],
429+
]);
430+
}
431+
432+
#[RequiresPhp('>= 8.0.0')]
433+
public function testStaticPropertyAccessOnStringUnionsWithoutCheckUnionTypes(): void
434+
{
435+
$this->checkUnionTypes = false;
436+
$this->analyse([__DIR__ . '/data/static-property-string-unions.php'], [
437+
[
438+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
439+
32,
440+
],
441+
[
442+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
443+
66,
444+
],
445+
[
446+
'Static access to instance property StaticPropertyStringUnions\Foo::$doBar.',
447+
80,
448+
],
449+
]);
450+
}
451+
399452
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php declare(strict_types = 1); // lint >= 8.0
2+
3+
namespace StaticPropertyStringUnions;
4+
5+
class Foo
6+
{
7+
8+
public static $create;
9+
10+
public $doBar;
11+
12+
}
13+
14+
class Bar
15+
{
16+
17+
public static $create;
18+
19+
}
20+
21+
class Baz
22+
{
23+
24+
}
25+
26+
class Runner
27+
{
28+
29+
public function singleConstantString(): void
30+
{
31+
$class = Baz::class;
32+
echo $class::$create;
33+
}
34+
35+
public function unionOfConstantStrings(bool $flag): void
36+
{
37+
$class = $flag ? Foo::class : Baz::class;
38+
echo $class::$create;
39+
}
40+
41+
public function unionOfConstantStringsAllValid(bool $flag): void
42+
{
43+
$class = $flag ? Foo::class : Bar::class;
44+
echo $class::$create;
45+
}
46+
47+
public function getClassUnion(Foo|Baz $object): void
48+
{
49+
$class = get_class($object);
50+
echo $class::$create;
51+
}
52+
53+
/**
54+
* @param class-string<Foo>|class-string<Baz> $class
55+
*/
56+
public function genericClassStringUnion(string $class): void
57+
{
58+
echo $class::$create;
59+
}
60+
61+
/**
62+
* @param class-string<Baz> $class
63+
*/
64+
public function singleGenericClassString(string $class): void
65+
{
66+
echo $class::$create;
67+
}
68+
69+
/**
70+
* @param class-string $class
71+
*/
72+
public function plainClassString(string $class): void
73+
{
74+
echo $class::$create;
75+
}
76+
77+
public function instancePropertyThroughConstantString(): void
78+
{
79+
$class = Foo::class;
80+
echo $class::$doBar;
81+
}
82+
83+
/**
84+
* @param class-string<Foo> $class
85+
*/
86+
public function propertyExistsNarrowing(string $class): void
87+
{
88+
if (property_exists($class, 'doesNotExistStatically')) {
89+
echo $class::$doesNotExistStatically;
90+
}
91+
}
92+
93+
/**
94+
* @param class-string<Foo>|class-string<Bar> $class
95+
*/
96+
public function genericClassStringUnionAllValid(string $class): void
97+
{
98+
echo $class::$create;
99+
}
100+
101+
/**
102+
* @param class-string<Foo> $class
103+
*/
104+
public function singleGenericClassStringValid(string $class): void
105+
{
106+
echo $class::$create;
107+
}
108+
109+
}

0 commit comments

Comments
 (0)