Skip to content

Commit d23e01f

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 411daa4 commit d23e01f

3 files changed

Lines changed: 167 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;
@@ -163,9 +164,24 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
163164
$classType = $scope->resolveTypeByName($node->class);
164165
}
165166
} else {
167+
$exprToCheck = NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->class);
168+
$exprType = $scope->getType($exprToCheck);
169+
if ($exprType->isClassString()->yes() && !$exprType->hasStaticProperty($name)->yes()) {
170+
// A constant class-name string or a class-string type describes the
171+
// object it names, so route the check through that object type. This
172+
// makes constant-string / class-string unions behave exactly like
173+
// object unions with regard to rule levels (member filtering when
174+
// checkUnionTypes is off, whole-union check when it is on).
175+
$objectType = $exprType->getClassStringObjectType();
176+
if (count($objectType->getObjectClassNames()) === 0) {
177+
// plain class-string resolves to ObjectWithoutClassType, keep silent
178+
return [];
179+
}
180+
$exprToCheck = new TypeExpr($objectType);
181+
}
166182
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
167183
$scope,
168-
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->class),
184+
$exprToCheck,
169185
sprintf('Access to static property $%s on an unknown class %%s.', SprintfHelper::escapeFormatString($name)),
170186
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasStaticProperty($name)->yes(),
171187
);

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleLevelHelper;
1111
use PHPStan\Testing\RuleTestCase;
12+
use PHPUnit\Framework\Attributes\RequiresPhp;
1213
use const PHP_VERSION_ID;
1314

1415
/**
@@ -17,6 +18,8 @@
1718
class AccessStaticPropertiesRuleTest extends RuleTestCase
1819
{
1920

21+
private bool $checkUnionTypes = true;
22+
2023
protected function getRule(): Rule
2124
{
2225
$reflectionProvider = self::createReflectionProvider();
@@ -27,7 +30,7 @@ protected function getRule(): Rule
2730
$reflectionProvider,
2831
checkNullables: true,
2932
checkThisOnly: false,
30-
checkUnionTypes: true,
33+
checkUnionTypes: $this->checkUnionTypes,
3134
checkExplicitMixed: false,
3235
checkImplicitMixed: false,
3336
checkBenevolentUnionTypes: false,
@@ -358,4 +361,55 @@ public function testBug2861(): void
358361
$this->analyse([__DIR__ . '/data/bug-2861.php'], []);
359362
}
360363

364+
#[RequiresPhp('>= 8.0.0')]
365+
public function testStaticPropertyAccessOnStringUnions(): void
366+
{
367+
$this->analyse([__DIR__ . '/data/static-property-string-unions.php'], [
368+
[
369+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
370+
32,
371+
],
372+
[
373+
'Access to an undefined static property StaticPropertyStringUnions\Baz|StaticPropertyStringUnions\Foo::$create.',
374+
38,
375+
],
376+
[
377+
'Access to an undefined static property StaticPropertyStringUnions\Baz|StaticPropertyStringUnions\Foo::$create.',
378+
50,
379+
],
380+
[
381+
'Access to an undefined static property StaticPropertyStringUnions\Baz|StaticPropertyStringUnions\Foo::$create.',
382+
58,
383+
],
384+
[
385+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
386+
66,
387+
],
388+
[
389+
'Static access to instance property StaticPropertyStringUnions\Foo::$doBar.',
390+
80,
391+
],
392+
]);
393+
}
394+
395+
#[RequiresPhp('>= 8.0.0')]
396+
public function testStaticPropertyAccessOnStringUnionsWithoutCheckUnionTypes(): void
397+
{
398+
$this->checkUnionTypes = false;
399+
$this->analyse([__DIR__ . '/data/static-property-string-unions.php'], [
400+
[
401+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
402+
32,
403+
],
404+
[
405+
'Access to an undefined static property StaticPropertyStringUnions\Baz::$create.',
406+
66,
407+
],
408+
[
409+
'Static access to instance property StaticPropertyStringUnions\Foo::$doBar.',
410+
80,
411+
],
412+
]);
413+
}
414+
361415
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace StaticPropertyStringUnions;
6+
7+
class Foo
8+
{
9+
10+
public static $create;
11+
12+
public $doBar;
13+
14+
}
15+
16+
class Bar
17+
{
18+
19+
public static $create;
20+
21+
}
22+
23+
class Baz
24+
{
25+
26+
}
27+
28+
class Runner
29+
{
30+
31+
public function singleConstantString(): void
32+
{
33+
$class = Baz::class;
34+
echo $class::$create;
35+
}
36+
37+
public function unionOfConstantStrings(bool $flag): void
38+
{
39+
$class = $flag ? Foo::class : Baz::class;
40+
echo $class::$create;
41+
}
42+
43+
public function unionOfConstantStringsAllValid(bool $flag): void
44+
{
45+
$class = $flag ? Foo::class : Bar::class;
46+
echo $class::$create;
47+
}
48+
49+
public function getClassUnion(Foo|Baz $object): void
50+
{
51+
$class = get_class($object);
52+
echo $class::$create;
53+
}
54+
55+
/**
56+
* @param class-string<Foo>|class-string<Baz> $class
57+
*/
58+
public function genericClassStringUnion(string $class): void
59+
{
60+
echo $class::$create;
61+
}
62+
63+
/**
64+
* @param class-string<Baz> $class
65+
*/
66+
public function singleGenericClassString(string $class): void
67+
{
68+
echo $class::$create;
69+
}
70+
71+
/**
72+
* @param class-string $class
73+
*/
74+
public function plainClassString(string $class): void
75+
{
76+
echo $class::$create;
77+
}
78+
79+
public function instancePropertyThroughConstantString(): void
80+
{
81+
$class = Foo::class;
82+
echo $class::$doBar;
83+
}
84+
85+
/**
86+
* @param class-string<Foo> $class
87+
*/
88+
public function propertyExistsNarrowing(string $class): void
89+
{
90+
if (property_exists($class, 'doesNotExistStatically')) {
91+
echo $class::$doesNotExistStatically;
92+
}
93+
}
94+
95+
}

0 commit comments

Comments
 (0)