-
Notifications
You must be signed in to change notification settings - Fork 572
Narrow ReflectionClass::getConstant() and ReflectionClass::getConstants() return types based on generic parameter
#5534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
phpstan-bot
wants to merge
8
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-gi9ip5y
+484
−0
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
44ef0cd
Narrow `ReflectionClass::getConstant()` and `ReflectionClass::getCons…
VincentLanglet d7dfaa6
Make getConstants() array keys optional when filter is non-constant
phpstan-bot 32710c4
Compute precise return types for getConstants() with multiple constan…
phpstan-bot 6f0afd8
Simplify extension to use synthetic ClassConstFetch via Scope::getType()
phpstan-bot 3ffb67d
Fall back to default return type for ReflectionClass<covariant T> wit…
phpstan-bot 4473157
Add tests for direct instantiation of ReflectionClass with non-final …
phpstan-bot 18e223a
Simplify
VincentLanglet b4ebae8
Fix
VincentLanglet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
219 changes: 219 additions & 0 deletions
219
src/Type/Php/ReflectionClassGetConstantsDynamicReturnTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\ClassConstFetch; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Name\FullyQualified; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\ClassReflection; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Type\Constant\ConstantArrayTypeBuilder; | ||
| use PHPStan\Type\Constant\ConstantBooleanType; | ||
| use PHPStan\Type\Constant\ConstantStringType; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use ReflectionClass; | ||
| use function count; | ||
|
|
||
| #[AutowiredService] | ||
| final class ReflectionClassGetConstantsDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
| { | ||
|
|
||
| public function getClass(): string | ||
| { | ||
| return ReflectionClass::class; | ||
| } | ||
|
|
||
| public function isMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return $methodReflection->getName() === 'getConstant' | ||
| || $methodReflection->getName() === 'getConstants'; | ||
| } | ||
|
|
||
| public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
| { | ||
| $calledOnType = $scope->getType($methodCall->var); | ||
| $reflectionType = $calledOnType->getTemplateType(ReflectionClass::class, 'T'); | ||
|
|
||
| $classReflections = $reflectionType->getObjectClassReflections(); | ||
| if (count($classReflections) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!$this->isInvariantOrFinalClass($calledOnType, $classReflections)) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($methodReflection->getName() === 'getConstant') { | ||
| return $this->resolveGetConstant($methodCall, $scope, $classReflections); | ||
| } | ||
|
|
||
| $filterType = count($methodCall->getArgs()) >= 1 | ||
| ? $scope->getType($methodCall->getArgs()[0]->value) | ||
| : null; | ||
|
|
||
| return $this->resolveGetConstants($scope, $classReflections, $filterType); | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-list<ClassReflection> $classReflections | ||
| */ | ||
| private function isInvariantOrFinalClass(Type $calledOnType, array $classReflections): bool | ||
| { | ||
| $hasNonFinalClass = false; | ||
| foreach ($classReflections as $classReflection) { | ||
| if (!$classReflection->isFinal() && !$classReflection->isEnum()) { | ||
| $hasNonFinalClass = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!$hasNonFinalClass) { | ||
| return true; | ||
| } | ||
|
|
||
| foreach ($calledOnType->getObjectClassReflections() as $reflectionClassReflection) { | ||
| if ($reflectionClassReflection->getName() !== 'ReflectionClass') { | ||
| return false; | ||
| } | ||
| $variance = $reflectionClassReflection->getCallSiteVarianceMap()->getVariance('T'); | ||
| if ($variance !== null && $variance->covariant()) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-list<ClassReflection> $classReflections | ||
| */ | ||
| private function resolveGetConstant(MethodCall $methodCall, Scope $scope, array $classReflections): ?Type | ||
| { | ||
| if (count($methodCall->getArgs()) < 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $nameType = $scope->getType($methodCall->getArgs()[0]->value); | ||
| $constantNames = $nameType->getConstantStrings(); | ||
|
|
||
| if (count($constantNames) > 0) { | ||
| $types = []; | ||
| foreach ($classReflections as $classReflection) { | ||
| foreach ($constantNames as $constantName) { | ||
| $name = $constantName->getValue(); | ||
| if ($name === '') { | ||
| continue; | ||
| } | ||
| if ($classReflection->hasConstant($name)) { | ||
| $types[] = $this->getConstantType($scope, $classReflection, $name); | ||
| } else { | ||
| $types[] = new ConstantBooleanType(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (count($types) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$types); | ||
| } | ||
|
|
||
| $allConstantTypes = []; | ||
| foreach ($classReflections as $classReflection) { | ||
| foreach ($this->getConstantNames($classReflection) as $name) { | ||
| $allConstantTypes[] = $this->getConstantType($scope, $classReflection, $name); | ||
| } | ||
| } | ||
|
|
||
| if (count($allConstantTypes) === 0) { | ||
| return new ConstantBooleanType(false); | ||
| } | ||
|
|
||
| $allConstantTypes[] = new ConstantBooleanType(false); | ||
|
|
||
| return TypeCombinator::union(...$allConstantTypes); | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-string $name | ||
| */ | ||
| private function getConstantType(Scope $scope, ClassReflection $classReflection, string $name): Type | ||
| { | ||
| return $scope->getType(new ClassConstFetch( | ||
| new FullyQualified($classReflection->getName()), | ||
| new Identifier($name), | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<non-empty-string> | ||
| */ | ||
| private function getConstantNames(ClassReflection $classReflection, ?int $filter = null): array | ||
| { | ||
| $names = []; | ||
| foreach ($classReflection->getNativeReflection()->getReflectionConstants() as $reflectionConstant) { | ||
| if ($filter !== null && ($reflectionConstant->getModifiers() & $filter) === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| $name = $reflectionConstant->getName(); | ||
| if ($name === '') { | ||
| continue; | ||
| } | ||
|
|
||
| $names[] = $name; | ||
| } | ||
|
|
||
| return $names; | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-list<ClassReflection> $classReflections | ||
| */ | ||
| private function resolveGetConstants(Scope $scope, array $classReflections, ?Type $filterType): Type | ||
| { | ||
| if ($filterType === null) { | ||
| return $this->buildConstantsArray($scope, $classReflections, null, false); | ||
| } | ||
|
|
||
| $filterScalars = $filterType->getConstantScalarValues(); | ||
| if (count($filterScalars) === 0) { | ||
| return $this->buildConstantsArray($scope, $classReflections, null, true); | ||
| } | ||
|
|
||
| $types = []; | ||
| foreach ($filterScalars as $filter) { | ||
| $types[] = $this->buildConstantsArray($scope, $classReflections, (int) $filter, false); | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$types); | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-list<ClassReflection> $classReflections | ||
| */ | ||
| private function buildConstantsArray(Scope $scope, array $classReflections, ?int $filter, bool $optional): Type | ||
| { | ||
| $types = []; | ||
| foreach ($classReflections as $classReflection) { | ||
| $builder = ConstantArrayTypeBuilder::createEmpty(); | ||
| foreach ($this->getConstantNames($classReflection, $filter) as $name) { | ||
| $builder->setOffsetValueType( | ||
| new ConstantStringType($name), | ||
| $this->getConstantType($scope, $classReflection, $name), | ||
| $optional, | ||
| ); | ||
| } | ||
| $types[] = $builder->getArray(); | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$types); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't rely on most of these assertions when the class is not final.
All the new logic you wrote in the extension might actually be simplified to synthetic new ClassConstFetch and passing that to Scope::getType().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make phpstanalso passes with no errors. Both verification steps are green:make tests: 11,917 tests, 79,427 assertions - all passmake phpstan: 2,297 files analysed, no errorsThe commit has been pushed to the PR branch.