Skip to content

Commit 4529113

Browse files
committed
ClassNameUsageLocationCreateIdentifierDynamicReturnTypeExtension - limit possible identifiers based on $location->value type
1 parent 3c3757d commit 4529113

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/Type/PHPStan/ClassNameUsageLocationCreateIdentifierDynamicReturnTypeExtension.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Type\PHPStan;
44

55
use PhpParser\Node\Expr\MethodCall;
6+
use PhpParser\Node\Expr\PropertyFetch;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Reflection\MethodReflection;
89
use PHPStan\Rules\ClassNameUsageLocation;
@@ -41,7 +42,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4142

4243
$reflection = new ReflectionClass(ClassNameUsageLocation::class);
4344
$identifiers = [];
45+
$locationValueType = $scope->getType(new PropertyFetch($methodCall->var, 'value'));
4446
foreach ($reflection->getConstants() as $constant) {
47+
if (!$locationValueType->isSuperTypeOf($scope->getTypeFromValue($constant))->yes()) {
48+
continue;
49+
}
4550
$location = ClassNameUsageLocation::from($constant);
4651
foreach ($secondPartValues as $secondPart) {
4752
$identifiers[] = $location->createIdentifier($secondPart->getValue());

tests/PHPStan/Analyser/nsrt/class-name-usage-location.php

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77

88
function (ClassNameUsageLocation $location): void {
99
assertType("'assert.test'|'attribute.test'|'catch.test'|'class.extendsTest'|'class.implementsTest'|'classConstant.test'|'enum.implementsTest'|'generics.testBound'|'generics.testDefault'|'instanceof.test'|'interface.extendsTest'|'methodTag.test'|'mixin.test'|'new.test'|'parameter.test'|'property.test'|'propertyTag.test'|'requireExtends.test'|'requireImplements.test'|'return.test'|'selfOut.test'|'staticMethod.test'|'staticProperty.test'|'traitUse.test'|'typeAlias.test'|'varTag.test'", $location->createIdentifier('test'));
10+
11+
if ($location->value === ClassNameUsageLocation::INSTANTIATION || $location->value === ClassNameUsageLocation::PROPERTY_TYPE) {
12+
assertType("'new.test'|'property.test'", $location->createIdentifier('test'));
13+
}
1014
};

0 commit comments

Comments
 (0)