|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\PrettyPrinter\Standard; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Analyser\SpecifiedTypes; |
| 10 | +use PHPStan\Analyser\TypeSpecifier; |
| 11 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 14 | +use PHPStan\Symfony\ServiceMap; |
| 15 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 16 | +use PHPStan\Type\ObjectType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\VerbosityLevel; |
| 19 | + |
| 20 | +final class Helper |
| 21 | +{ |
| 22 | + |
| 23 | + public static function getGetTypeFromMethodCall( |
| 24 | + MethodReflection $methodReflection, |
| 25 | + MethodCall $methodCall, |
| 26 | + Scope $scope, |
| 27 | + ServiceMap $serviceMap |
| 28 | + ): Type |
| 29 | + { |
| 30 | + $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 31 | + if (!isset($methodCall->args[0])) { |
| 32 | + return $returnType; |
| 33 | + } |
| 34 | + |
| 35 | + $serviceId = ServiceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope); |
| 36 | + if ($serviceId !== null) { |
| 37 | + $service = $serviceMap->getService($serviceId); |
| 38 | + if ($service !== null && !$service->isSynthetic()) { |
| 39 | + return new ObjectType($service->getClass() ?? $serviceId); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return $returnType; |
| 44 | + } |
| 45 | + |
| 46 | + public static function getHasTypeFromMethodCall( |
| 47 | + MethodReflection $methodReflection, |
| 48 | + MethodCall $methodCall, |
| 49 | + Scope $scope, |
| 50 | + ServiceMap $serviceMap |
| 51 | + ): Type |
| 52 | + { |
| 53 | + $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 54 | + if (!isset($methodCall->args[0])) { |
| 55 | + return $returnType; |
| 56 | + } |
| 57 | + |
| 58 | + $serviceId = ServiceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope); |
| 59 | + if ($serviceId !== null) { |
| 60 | + $service = $serviceMap->getService($serviceId); |
| 61 | + return new ConstantBooleanType($service !== null && $service->isPublic()); |
| 62 | + } |
| 63 | + |
| 64 | + return $returnType; |
| 65 | + } |
| 66 | + |
| 67 | + public static function specifyTypes( |
| 68 | + MethodReflection $methodReflection, |
| 69 | + MethodCall $node, |
| 70 | + Scope $scope, |
| 71 | + TypeSpecifierContext $context, |
| 72 | + TypeSpecifier $typeSpecifier, |
| 73 | + Standard $printer |
| 74 | + ): SpecifiedTypes |
| 75 | + { |
| 76 | + if (!isset($node->args[0])) { |
| 77 | + return new SpecifiedTypes(); |
| 78 | + } |
| 79 | + $argType = $scope->getType($node->args[0]->value); |
| 80 | + return $typeSpecifier->create( |
| 81 | + self::createMarkerNode($node->var, $argType, $printer), |
| 82 | + $argType, |
| 83 | + $context |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public static function createMarkerNode(Expr $expr, Type $type, Standard $printer): Expr |
| 88 | + { |
| 89 | + return new Expr\Variable(md5(sprintf( |
| 90 | + '%s::%s', |
| 91 | + $printer->prettyPrintExpr($expr), |
| 92 | + $type->describe(VerbosityLevel::value()) |
| 93 | + ))); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments