-
-
Notifications
You must be signed in to change notification settings - Fork 739
Decouple PHPStan Type to function resolver logic #2742
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
Merged
Merged
Changes from all commits
Commits
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
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
32 changes: 32 additions & 0 deletions
32
src/PHPStan/Reflection/TypeToCallReflectionResolver/ClosureTypeToCallReflectionResolver.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,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Core\PHPStan\Reflection\TypeToCallReflectionResolver; | ||
|
|
||
| use PHPStan\Reflection\ClassMemberAccessAnswerer; | ||
| use PHPStan\Reflection\Native\NativeFunctionReflection; | ||
| use PHPStan\TrinaryLogic; | ||
| use PHPStan\Type\ClosureType; | ||
| use PHPStan\Type\Type; | ||
|
|
||
| final class ClosureTypeToCallReflectionResolver implements TypeToCallReflectionResolverInterface | ||
| { | ||
| public function supports(Type $type): bool | ||
| { | ||
| return $type instanceof ClosureType; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClosureType $type | ||
| */ | ||
| public function resolve(Type $type, ClassMemberAccessAnswerer $classMemberAccessAnswerer) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be return type, in interface and it's implementations |
||
| { | ||
| return new NativeFunctionReflection( | ||
| '{closure}', | ||
| $type->getCallableParametersAcceptors($classMemberAccessAnswerer), | ||
| null, | ||
| TrinaryLogic::createMaybe() | ||
| ); | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
...tan/Reflection/TypeToCallReflectionResolver/ConstantArrayTypeToCallReflectionResolver.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,111 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Core\PHPStan\Reflection\TypeToCallReflectionResolver; | ||
|
|
||
| use PHPStan\Reflection\ClassMemberAccessAnswerer; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Type\Constant\ConstantArrayType; | ||
| use PHPStan\Type\Constant\ConstantArrayTypeAndMethod; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPStan\Type\Constant\ConstantStringType; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\ObjectWithoutClassType; | ||
| use PHPStan\Type\Type; | ||
|
|
||
| /** | ||
| * @see https://github.com/phpstan/phpstan-src/blob/b1fd47bda2a7a7d25091197b125c0adf82af6757/src/Type/Constant/ConstantArrayType.php#L188 | ||
| */ | ||
| final class ConstantArrayTypeToCallReflectionResolver implements TypeToCallReflectionResolverInterface | ||
| { | ||
| /** | ||
| * @var ReflectionProvider | ||
| */ | ||
| private $reflectionProvider; | ||
|
|
||
| public function __construct(ReflectionProvider $reflectionProvider) | ||
| { | ||
| $this->reflectionProvider = $reflectionProvider; | ||
| } | ||
|
|
||
| public function supports(Type $type): bool | ||
| { | ||
| return $type instanceof ConstantArrayType; | ||
| } | ||
|
|
||
| /** | ||
| * @param ConstantArrayType $type | ||
| */ | ||
| public function resolve(Type $type, ClassMemberAccessAnswerer $classMemberAccessAnswerer) | ||
| { | ||
| $typeAndMethodName = $this->findTypeAndMethodName($type); | ||
| if ($typeAndMethodName === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($typeAndMethodName->isUnknown() || ! $typeAndMethodName->getCertainty()->yes()) { | ||
| return null; | ||
| } | ||
|
|
||
| $method = $typeAndMethodName | ||
| ->getType() | ||
| ->getMethod($typeAndMethodName->getMethod(), $classMemberAccessAnswerer); | ||
|
|
||
| if (! $classMemberAccessAnswerer->canCallMethod($method)) { | ||
| return null; | ||
| } | ||
|
|
||
| return $method; | ||
| } | ||
|
|
||
| /** | ||
| * @see https://github.com/phpstan/phpstan-src/blob/b1fd47bda2a7a7d25091197b125c0adf82af6757/src/Type/Constant/ConstantArrayType.php#L209 | ||
| */ | ||
| private function findTypeAndMethodName(ConstantArrayType $constantArrayType): ?ConstantArrayTypeAndMethod | ||
| { | ||
| if (! $this->areKeyTypesValid($constantArrayType)) { | ||
| return null; | ||
| } | ||
|
|
||
| [$classOrObject, $method] = $constantArrayType->getValueTypes(); | ||
|
|
||
| if (! $method instanceof ConstantStringType) { | ||
| return ConstantArrayTypeAndMethod::createUnknown(); | ||
| } | ||
|
|
||
| if ($classOrObject instanceof ConstantStringType) { | ||
| if (! $this->reflectionProvider->hasClass($classOrObject->getValue())) { | ||
| return ConstantArrayTypeAndMethod::createUnknown(); | ||
| } | ||
|
|
||
| $type = new ObjectType($this->reflectionProvider->getClass($classOrObject->getValue())->getName()); | ||
| } elseif ((new ObjectWithoutClassType())->isSuperTypeOf($classOrObject)->yes()) { | ||
| $type = $classOrObject; | ||
| } else { | ||
| return ConstantArrayTypeAndMethod::createUnknown(); | ||
| } | ||
|
|
||
| $has = $type->hasMethod($method->getValue()); | ||
| if (! $has->no()) { | ||
| return ConstantArrayTypeAndMethod::createConcrete($type, $method->getValue(), $has); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function areKeyTypesValid(ConstantArrayType $constantArrayType): bool | ||
| { | ||
| $keyTypes = $constantArrayType->getKeyTypes(); | ||
|
|
||
| if (count($keyTypes) !== 2) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($keyTypes[0]->isSuperTypeOf(new ConstantIntegerType(0))->no()) { | ||
| return false; | ||
| } | ||
|
|
||
| return ! $keyTypes[1]->isSuperTypeOf(new ConstantIntegerType(1))->no(); | ||
| } | ||
| } |
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.
There should be return type, in interface and it's implementations