|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace yii2\extensions\phpstan\type; |
| 6 | + |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\{MethodReflection, ParametersAcceptorSelector, ReflectionProvider}; |
| 11 | +use PHPStan\Type\{DynamicMethodReturnTypeExtension, MixedType, ObjectType, Type}; |
| 12 | +use yii\di\ServiceLocator; |
| 13 | +use yii2\extensions\phpstan\ServiceMap; |
| 14 | + |
| 15 | +/** |
| 16 | + * Provides dynamic return type extension for Yii Service Locator component resolution in PHPStan analysis. |
| 17 | + * |
| 18 | + * Integrates the Yii Service Locator service {@see ServiceLocator} with PHPStan dynamic method return type extension |
| 19 | + * system, enabling precise type inference for {@see ServiceLocator::get()} calls based on component ID and the |
| 20 | + * {@see ServiceMap}. |
| 21 | + * |
| 22 | + * This extension analyzes the first argument of {@see ServiceLocator::get()} to determine the most accurate return |
| 23 | + * type, returning an {@see ObjectType} for known component classes or a {@see MixedType} for unknown or dynamic ID. |
| 24 | + * |
| 25 | + * Key features: |
| 26 | + * - Accurate return type inference for {@see ServiceLocator::get()} based on component ID string. |
| 27 | + * - Compatible with PHPStan strict static analysis and autocompletion. |
| 28 | + * - Falls back to method signature return type for unsupported or invalid calls. |
| 29 | + * - Supports Yii modules, applications, and any class extending {@see ServiceLocator}. |
| 30 | + * - Uses {@see ServiceMap} to resolve component class names. |
| 31 | + * |
| 32 | + * @see DynamicMethodReturnTypeExtension for PHPStan dynamic return type extension contract. |
| 33 | + * @see ServiceMap for service and component map for Yii Application static analysis. |
| 34 | + * |
| 35 | + * @copyright Copyright (C) 2023 Terabytesoftw. |
| 36 | + * @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License. |
| 37 | + */ |
| 38 | +final class ServiceLocatorDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 39 | +{ |
| 40 | + /** |
| 41 | + * Creates a new instance of the {@see ServiceLocatorDynamicMethodReturnTypeExtension} class. |
| 42 | + * |
| 43 | + * @param ReflectionProvider $reflectionProvider Reflection provider for class and property lookups. |
| 44 | + * @param ServiceMap $serviceMap Service and component map for Yii Application static analysis. |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + private readonly ReflectionProvider $reflectionProvider, |
| 48 | + private readonly ServiceMap $serviceMap, |
| 49 | + ) {} |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the class name for which this dynamic return type extension applies. |
| 53 | + * |
| 54 | + * Specifies the fully qualified class name of the Yii ServiceLocator {@see ServiceLocator} that this extension |
| 55 | + * target for dynamic return type inference in PHPStan analysis. |
| 56 | + * |
| 57 | + * This method enables PHPStan to associate the extension with the {@see ServiceLocator} class and all its |
| 58 | + * subclasses (like Module and Application), ensuring that dynamic return type logic is applied to component |
| 59 | + * resolution calls. |
| 60 | + * |
| 61 | + * @return string Fully qualified class name of the supported ServiceLocator class. |
| 62 | + * |
| 63 | + * @phpstan-return class-string |
| 64 | + */ |
| 65 | + public function getClass(): string |
| 66 | + { |
| 67 | + return ServiceLocator::class; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Infers the return type for a {@see ServiceLocator::get()} method call based on the provided component ID |
| 72 | + * argument. |
| 73 | + * |
| 74 | + * Determines the most accurate return type for component resolution by analyzing the first argument of the |
| 75 | + * {@see ServiceLocator::get()} call. |
| 76 | + * |
| 77 | + * - If the argument is a constant string and matches a known component in the {@see ServiceMap}, returns an |
| 78 | + * {@see ObjectType} for the resolved class. |
| 79 | + * - If the argument is a class name known to the {@see ReflectionProvider}, returns an {@see ObjectType} for that |
| 80 | + * class. |
| 81 | + * - Otherwise, returns a {@see MixedType} to indicate an unknown or dynamic component type. |
| 82 | + * |
| 83 | + * Falls back to the default method signature return type for unsupported or invalid calls, ensuring compatibility |
| 84 | + * with PHPStan static analysis and IDE autocompletion. |
| 85 | + * |
| 86 | + * @param MethodReflection $methodReflection Reflection instance for the method being analyzed. |
| 87 | + * @param MethodCall $methodCall AST node for the method call expression. |
| 88 | + * @param Scope $scope PHPStan analysis scope for type resolution. |
| 89 | + * |
| 90 | + * @return Type Inferred return type for the component resolution call. |
| 91 | + */ |
| 92 | + public function getTypeFromMethodCall( |
| 93 | + MethodReflection $methodReflection, |
| 94 | + MethodCall $methodCall, |
| 95 | + Scope $scope, |
| 96 | + ): Type { |
| 97 | + if (isset($methodCall->args[0]) === false || $methodCall->args[0]::class !== Arg::class) { |
| 98 | + return ParametersAcceptorSelector::selectFromArgs( |
| 99 | + $scope, |
| 100 | + $methodCall->getArgs(), |
| 101 | + $methodReflection->getVariants(), |
| 102 | + )->getReturnType(); |
| 103 | + } |
| 104 | + |
| 105 | + $argType = $scope->getType($methodCall->args[0]->value); |
| 106 | + $constantStrings = $argType->getConstantStrings(); |
| 107 | + |
| 108 | + if (count($constantStrings) === 1) { |
| 109 | + $value = $constantStrings[0]->getValue(); |
| 110 | + |
| 111 | + $componentClass = $this->serviceMap->getComponentClassById($value); |
| 112 | + |
| 113 | + if ($componentClass !== null) { |
| 114 | + return new ObjectType($componentClass); |
| 115 | + } |
| 116 | + |
| 117 | + $serviceClass = $this->serviceMap->getServiceById($value); |
| 118 | + |
| 119 | + if ($serviceClass !== null) { |
| 120 | + return new ObjectType($serviceClass); |
| 121 | + } |
| 122 | + |
| 123 | + if ($this->reflectionProvider->hasClass($value)) { |
| 124 | + return new ObjectType($value); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return new MixedType(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Determines whether the specified method is supported for dynamic return type inference. |
| 133 | + * |
| 134 | + * Checks if the method name is {@see ServiceLocator::get}, which is the only method supported by this extension for |
| 135 | + * dynamic return type analysis. |
| 136 | + * |
| 137 | + * This enables PHPStan to apply custom type inference logic exclusively to component resolution calls on the Yii |
| 138 | + * Service Locator {@see ServiceLocator} and its subclasses (Module, Application). |
| 139 | + * |
| 140 | + * @param MethodReflection $methodReflection Reflection instance for the method being analyzed. |
| 141 | + * |
| 142 | + * @return bool `true` if the method is {@see ServiceLocator::get}; `false` otherwise. |
| 143 | + */ |
| 144 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 145 | + { |
| 146 | + return $methodReflection->getName() === 'get'; |
| 147 | + } |
| 148 | +} |
0 commit comments