|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace mglaman\PHPStanDrupal\Type\EntityQuery; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\Query\QueryInterface; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Analyser\SpecifiedTypes; |
| 9 | +use PHPStan\Analyser\TypeSpecifier; |
| 10 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 11 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 14 | +use PHPStan\Type\MethodTypeSpecifyingExtension; |
| 15 | + |
| 16 | +final class AccessCheckTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 17 | +{ |
| 18 | + private TypeSpecifier $typeSpecifier; |
| 19 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier) : void |
| 20 | + { |
| 21 | + $this->typeSpecifier = $typeSpecifier; |
| 22 | + } |
| 23 | + |
| 24 | + public function getClass(): string |
| 25 | + { |
| 26 | + return QueryInterface::class; |
| 27 | + } |
| 28 | + |
| 29 | + public function isMethodSupported( |
| 30 | + MethodReflection $methodReflection, |
| 31 | + MethodCall $node, |
| 32 | + TypeSpecifierContext $context |
| 33 | + ): bool { |
| 34 | + return $methodReflection->getName() === 'accessCheck'; |
| 35 | + } |
| 36 | + |
| 37 | + public function specifyTypes( |
| 38 | + MethodReflection $methodReflection, |
| 39 | + MethodCall $node, |
| 40 | + Scope $scope, |
| 41 | + TypeSpecifierContext $context |
| 42 | + ): SpecifiedTypes { |
| 43 | + $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 44 | + $expr = $node->var; |
| 45 | + if (!$returnType instanceof EntityQueryType) { |
| 46 | + return $this->typeSpecifier->create($expr, $returnType, TypeSpecifierContext::createTruthy()); |
| 47 | + } |
| 48 | + return $this->typeSpecifier->create($expr, $returnType->withAccessCheck(), TypeSpecifierContext::createTruthy()); |
| 49 | + } |
| 50 | +} |
0 commit comments