Skip to content

Commit 2b0963b

Browse files
committed
[BCB] TypehintHelper::decideTypeFromReflection() parameter $selfClass no longer accepts string
1 parent fd35d2f commit 2b0963b

File tree

6 files changed

+15
-19
lines changed

6 files changed

+15
-19
lines changed

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,4 @@ Instead of `PHPStanTestCase::createBroker()`, call `PHPStanTestCase::createRefle
281281
* [`Type::getProperty()`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.Type.html#_getProperty) now returns [`ExtendedPropertyReflection`](https://apiref.phpstan.org/2.0.x/PHPStan.Reflection.ExtendedPropertyReflection.html)
282282
* `additionalConfigFiles` config parameter must be a list
283283
* Remove `__set_state()` on objects that should not be serialized in cache
284+
* Parameter `$selfClass` of [`TypehintHelper::decideTypeFromReflection()`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.TypehintHelper.html#_decideTypeFromReflection) no longer accepts string

src/Reflection/Php/PhpMethodReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private function getParameters(): array
232232
$this->initializerExprTypeResolver,
233233
$reflection,
234234
$this->phpDocParameterTypes[$reflection->getName()] ?? null,
235-
$this->getDeclaringClass()->getName(),
235+
$this->getDeclaringClass(),
236236
$this->phpDocParameterOutTypes[$reflection->getName()] ?? null,
237237
$this->immediatelyInvokedCallableParameters[$reflection->getName()] ?? TrinaryLogic::createMaybe(),
238238
$this->phpDocClosureThisTypeParameters[$reflection->getName()] ?? null,

src/Reflection/Php/PhpParameterReflection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection\Php;
44

55
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
6+
use PHPStan\Reflection\ClassReflection;
67
use PHPStan\Reflection\InitializerExprContext;
78
use PHPStan\Reflection\InitializerExprTypeResolver;
89
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
@@ -24,7 +25,7 @@ public function __construct(
2425
private InitializerExprTypeResolver $initializerExprTypeResolver,
2526
private ReflectionParameter $reflection,
2627
private ?Type $phpDocType,
27-
private ?string $declaringClassName,
28+
private ?ClassReflection $declaringClass,
2829
private ?Type $outType,
2930
private TrinaryLogic $immediatelyInvokedCallable,
3031
private ?Type $closureThisType,
@@ -62,7 +63,7 @@ public function getType(): Type
6263
$this->type = TypehintHelper::decideTypeFromReflection(
6364
$this->reflection->getType(),
6465
$phpDocType,
65-
$this->declaringClassName,
66+
$this->declaringClass,
6667
$this->isVariadic(),
6768
);
6869
}
@@ -97,7 +98,7 @@ public function getNativeType(): Type
9798
$this->nativeType = TypehintHelper::decideTypeFromReflection(
9899
$this->reflection->getType(),
99100
null,
100-
$this->declaringClassName,
101+
$this->declaringClass,
101102
$this->isVariadic(),
102103
);
103104
}

src/Rules/Traits/ConflictingTraitConstantsRule.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Reflection\ClassReflection;
99
use PHPStan\Reflection\InitializerExprContext;
1010
use PHPStan\Reflection\InitializerExprTypeResolver;
11+
use PHPStan\Reflection\ReflectionProvider;
1112
use PHPStan\Rules\IdentifierRuleError;
1213
use PHPStan\Rules\Rule;
1314
use PHPStan\Rules\RuleErrorBuilder;
@@ -22,7 +23,10 @@
2223
final class ConflictingTraitConstantsRule implements Rule
2324
{
2425

25-
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
26+
public function __construct(
27+
private InitializerExprTypeResolver $initializerExprTypeResolver,
28+
private ReflectionProvider $reflectionProvider,
29+
)
2630
{
2731
}
2832

@@ -186,7 +190,7 @@ private function processSingleConstant(ClassReflection $classReflection, Reflect
186190
->build();
187191
}
188192
} elseif ($constantNativeType === null) {
189-
$traitNativeTypeType = TypehintHelper::decideTypeFromReflection($traitNativeType, null, $traitDeclaringClass->getName());
193+
$traitNativeTypeType = TypehintHelper::decideTypeFromReflection($traitNativeType, null, $this->reflectionProvider->getClass($traitDeclaringClass->getName()));
190194
$errors[] = RuleErrorBuilder::message(sprintf(
191195
'Constant %s::%s overriding constant %s::%s (%s) should also have native type %s.',
192196
$classReflection->getDisplayName(),
@@ -200,7 +204,7 @@ private function processSingleConstant(ClassReflection $classReflection, Reflect
200204
->identifier('classConstant.missingNativeType')
201205
->build();
202206
} else {
203-
$traitNativeTypeType = TypehintHelper::decideTypeFromReflection($traitNativeType, null, $traitDeclaringClass->getName());
207+
$traitNativeTypeType = TypehintHelper::decideTypeFromReflection($traitNativeType, null, $this->reflectionProvider->getClass($traitDeclaringClass->getName()));
204208
$constantNativeTypeType = ParserNodeTypeToPHPStanType::resolve($constantNativeType, $classReflection);
205209
if (!$traitNativeTypeType->equals($constantNativeTypeType)) {
206210
$errors[] = RuleErrorBuilder::message(sprintf(

src/Type/TypehintHelper.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionNamedType;
99
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionUnionType;
1010
use PHPStan\Reflection\ClassReflection;
11-
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
1211
use PHPStan\ShouldNotHappenException;
1312
use PHPStan\Type\Constant\ConstantArrayType;
1413
use PHPStan\Type\Generic\TemplateTypeHelper;
1514
use ReflectionType;
1615
use function array_map;
1716
use function count;
1817
use function get_class;
19-
use function is_string;
2018
use function sprintf;
2119

2220
final class TypehintHelper
@@ -26,7 +24,7 @@ final class TypehintHelper
2624
public static function decideTypeFromReflection(
2725
?ReflectionType $reflectionType,
2826
?Type $phpDocType = null,
29-
ClassReflection|string|null $selfClass = null,
27+
ClassReflection|null $selfClass = null,
3028
bool $isVariadic = false,
3129
): Type
3230
{
@@ -67,14 +65,6 @@ public static function decideTypeFromReflection(
6765
$typeNode = new FullyQualified($reflectionType->getName());
6866
}
6967

70-
if (is_string($selfClass)) {
71-
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
72-
if ($reflectionProvider->hasClass($selfClass)) {
73-
$selfClass = $reflectionProvider->getClass($selfClass);
74-
} else {
75-
$selfClass = null;
76-
}
77-
}
7868
$type = ParserNodeTypeToPHPStanType::resolve($typeNode, $selfClass);
7969
if ($reflectionType->allowsNull()) {
8070
$type = TypeCombinator::addNull($type);

tests/PHPStan/Rules/Traits/ConflictingTraitConstantsRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ConflictingTraitConstantsRuleTest extends RuleTestCase
1414

1515
protected function getRule(): TRule
1616
{
17-
return new ConflictingTraitConstantsRule(self::getContainer()->getByType(InitializerExprTypeResolver::class));
17+
return new ConflictingTraitConstantsRule(self::getContainer()->getByType(InitializerExprTypeResolver::class), $this->createReflectionProvider());
1818
}
1919

2020
public function testRule(): void

0 commit comments

Comments
 (0)