Skip to content

Commit 7337f68

Browse files
committed
Check every union member for restricted usage class names
The restricted usage rules (method, property, static, class constant, callable, string cast) missed the class of a union member in several cases: - Static access on Foo|false, Foo|string, Foo|int|null was lost at level 7+, because the whole union was converted at once. - Instance access on callable|Foo, iterable<int>|Foo, Foo|object{a: int} and TObject|Foo found no class name, because the intersection with object left a member without a class. - class-string<Foo>|false static access was never checked. - RestrictedStaticPropertyUsageRule looked for an instance property instead of a static one when picking the type to check, so $orFalse::$prop was missed. Class names are now collected per union member. Follow-up to #7006 (phpstan/phpstan#15681)
1 parent 217459a commit 7337f68

19 files changed

Lines changed: 360 additions & 30 deletions

‎src/Rules/RestrictedUsage/RestrictedClassConstantUsageRule.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPStan\Rules\RuleLevelHelper;
1616
use PHPStan\Type\ErrorType;
1717
use PHPStan\Type\Type;
18-
use PHPStan\Type\TypeCombinator;
1918
use function strtolower;
2019

2120
/**
@@ -73,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array
7372
return [];
7473
}
7574

76-
$referencedClasses = TypeCombinator::removeNull($classTypeResult->getType())->getObjectTypeOrClassStringObjectType()->getObjectClassNames();
75+
$referencedClasses = RestrictedUsageObjectClassNames::getFromClassExpression($classTypeResult->getType());
7776
}
7877

7978
$errors = [];

‎src/Rules/RestrictedUsage/RestrictedMethodCallableUsageRule.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use PHPStan\Rules\Rule;
1414
use PHPStan\Rules\RuleErrorBuilder;
15-
use PHPStan\Type\ObjectWithoutClassType;
16-
use PHPStan\Type\TypeCombinator;
1715

1816
/**
1917
* @implements Rule<MethodCallableNode>
@@ -54,9 +52,7 @@ public function processNode(Node $node, Scope $scope): array
5452

5553
$methodName = $node->getName()->name;
5654
$methodCalledOnType = $scope->getType($node->getVar());
57-
// the intersection only removes what is not an object, such as the null of a nullable type
58-
$objectType = $methodCalledOnType->isObject()->yes() ? $methodCalledOnType : TypeCombinator::intersect($methodCalledOnType, new ObjectWithoutClassType());
59-
$referencedClasses = $objectType->getObjectClassNames();
55+
$referencedClasses = RestrictedUsageObjectClassNames::get($methodCalledOnType);
6056

6157
$errors = [];
6258

‎src/Rules/RestrictedUsage/RestrictedMethodUsageRule.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use PHPStan\Rules\Rule;
1414
use PHPStan\Rules\RuleErrorBuilder;
15-
use PHPStan\Type\ObjectWithoutClassType;
16-
use PHPStan\Type\TypeCombinator;
1715

1816
/**
1917
* @implements Rule<MethodCall>
@@ -54,9 +52,7 @@ public function processNode(Node $node, Scope $scope): array
5452

5553
$methodName = $node->name->name;
5654
$methodCalledOnType = $scope->getType($node->var);
57-
// the intersection only removes what is not an object, such as the null of a nullable type
58-
$objectType = $methodCalledOnType->isObject()->yes() ? $methodCalledOnType : TypeCombinator::intersect($methodCalledOnType, new ObjectWithoutClassType());
59-
$referencedClasses = $objectType->getObjectClassNames();
55+
$referencedClasses = RestrictedUsageObjectClassNames::get($methodCalledOnType);
6056

6157
$errors = [];
6258

‎src/Rules/RestrictedUsage/RestrictedPropertyUsageRule.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Rules\Rule;
1313
use PHPStan\Rules\RuleErrorBuilder;
14-
use PHPStan\Type\ObjectWithoutClassType;
15-
use PHPStan\Type\TypeCombinator;
1614

1715
/**
1816
* @implements Rule<Node\Expr\PropertyFetch>
@@ -53,9 +51,7 @@ public function processNode(Node $node, Scope $scope): array
5351

5452
$propertyName = $node->name->name;
5553
$propertyCalledOnType = $scope->getType($node->var);
56-
// the intersection only removes what is not an object, such as the null of a nullable type
57-
$objectType = $propertyCalledOnType->isObject()->yes() ? $propertyCalledOnType : TypeCombinator::intersect($propertyCalledOnType, new ObjectWithoutClassType());
58-
$referencedClasses = $objectType->getObjectClassNames();
54+
$referencedClasses = RestrictedUsageObjectClassNames::get($propertyCalledOnType);
5955

6056
$errors = [];
6157

‎src/Rules/RestrictedUsage/RestrictedStaticMethodCallableUsageRule.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use PHPStan\Rules\RuleLevelHelper;
1717
use PHPStan\Type\ErrorType;
1818
use PHPStan\Type\Type;
19-
use PHPStan\Type\TypeCombinator;
2019

2120
/**
2221
* @implements Rule<StaticMethodCallableNode>
@@ -73,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array
7372
return [];
7473
}
7574

76-
$referencedClasses = TypeCombinator::removeNull($classTypeResult->getType())->getObjectTypeOrClassStringObjectType()->getObjectClassNames();
75+
$referencedClasses = RestrictedUsageObjectClassNames::getFromClassExpression($classTypeResult->getType());
7776
}
7877

7978
$errors = [];

‎src/Rules/RestrictedUsage/RestrictedStaticMethodUsageRule.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPStan\Rules\RuleLevelHelper;
1616
use PHPStan\Type\ErrorType;
1717
use PHPStan\Type\Type;
18-
use PHPStan\Type\TypeCombinator;
1918

2019
/**
2120
* @implements Rule<Node\Expr\StaticCall>
@@ -72,7 +71,7 @@ public function processNode(Node $node, Scope $scope): array
7271
return [];
7372
}
7473

75-
$referencedClasses = TypeCombinator::removeNull($classTypeResult->getType())->getObjectTypeOrClassStringObjectType()->getObjectClassNames();
74+
$referencedClasses = RestrictedUsageObjectClassNames::getFromClassExpression($classTypeResult->getType());
7675
}
7776

7877
$errors = [];

‎src/Rules/RestrictedUsage/RestrictedStaticPropertyUsageRule.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPStan\Rules\RuleLevelHelper;
1616
use PHPStan\Type\ErrorType;
1717
use PHPStan\Type\Type;
18-
use PHPStan\Type\TypeCombinator;
1918

2019
/**
2120
* @implements Rule<Node\Expr\StaticPropertyFetch>
@@ -65,14 +64,14 @@ public function processNode(Node $node, Scope $scope): array
6564
$scope,
6665
$node->class,
6766
'', // We don't care about the error message
68-
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasInstanceProperty($propertyName)->yes(),
67+
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasStaticProperty($propertyName)->yes(),
6968
);
7069

7170
if ($classTypeResult->getType() instanceof ErrorType) {
7271
return [];
7372
}
7473

75-
$referencedClasses = TypeCombinator::removeNull($classTypeResult->getType())->getObjectTypeOrClassStringObjectType()->getObjectClassNames();
74+
$referencedClasses = RestrictedUsageObjectClassNames::getFromClassExpression($classTypeResult->getType());
7675
}
7776

7877
$errors = [];
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RestrictedUsage;
4+
5+
use PHPStan\Type\Type;
6+
use PHPStan\Type\UnionType;
7+
use function array_values;
8+
9+
final class RestrictedUsageObjectClassNames
10+
{
11+
12+
/**
13+
* Class names of the members of the type, so that the object part of a union
14+
* is still checked when it is combined with null, false, string, ...
15+
* Members without a class name contribute nothing.
16+
*
17+
* Iterates the members directly instead of intersecting with `object`,
18+
* as this runs for every method call and property fetch.
19+
*
20+
* @return list<string>
21+
*/
22+
public static function get(Type $type): array
23+
{
24+
return self::collect($type, false);
25+
}
26+
27+
/**
28+
* Same as get(), for the class expression of a static access (`$expr::foo()`),
29+
* where a member can also be a class-string.
30+
*
31+
* @return list<string>
32+
*/
33+
public static function getFromClassExpression(Type $type): array
34+
{
35+
return self::collect($type, true);
36+
}
37+
38+
/**
39+
* @return list<string>
40+
*/
41+
private static function collect(Type $type, bool $acceptClassStrings): array
42+
{
43+
$members = $type instanceof UnionType ? $type->getTypes() : [$type];
44+
45+
$classNames = [];
46+
foreach ($members as $member) {
47+
if ($acceptClassStrings) {
48+
$member = $member->getObjectTypeOrClassStringObjectType();
49+
}
50+
51+
foreach ($member->getObjectClassNames() as $className) {
52+
$classNames[$className] = $className;
53+
}
54+
}
55+
56+
return array_values($classNames);
57+
}
58+
59+
}

‎src/Rules/RestrictedUsage/RestrictedUsageOfDeprecatedStringCastRule.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Rules\Rule;
1313
use PHPStan\Rules\RuleErrorBuilder;
14-
use PHPStan\Type\ObjectWithoutClassType;
15-
use PHPStan\Type\TypeCombinator;
1614

1715
/**
1816
* @implements Rule<Cast\String_>
@@ -45,9 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4543
}
4644

4745
$exprType = $scope->getType($node->expr);
48-
// the intersection only removes what is not an object, such as the null of a nullable type
49-
$objectType = $exprType->isObject()->yes() ? $exprType : TypeCombinator::intersect($exprType, new ObjectWithoutClassType());
50-
$referencedClasses = $objectType->getObjectClassNames();
46+
$referencedClasses = RestrictedUsageObjectClassNames::get($exprType);
5147

5248
$errors = [];
5349

‎tests/PHPStan/Rules/RestrictedUsage/RestrictedClassConstantUsageRuleTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ public function testBug15681(): void
6666
'Cannot access FOO',
6767
123,
6868
],
69+
[
70+
'Cannot access FOO',
71+
172,
72+
],
73+
[
74+
'Cannot access FOO',
75+
177,
76+
],
77+
[
78+
'Cannot access FOO',
79+
182,
80+
],
81+
[
82+
'Cannot access FOO',
83+
193,
84+
],
85+
[
86+
'Cannot access FOO',
87+
204,
88+
],
6989
]);
7090
}
7191

0 commit comments

Comments
 (0)