|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Set return type of \WP_CLI\Utils\get_flag_value(). |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace WP_CLI\Tests\PHPStan; |
| 10 | + |
| 11 | +use PhpParser\Node\Expr\FuncCall; |
| 12 | +use PHPStan\Analyser\Scope; |
| 13 | +use PHPStan\Reflection\FunctionReflection; |
| 14 | +use PHPStan\Type\Constant\ConstantStringType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\MixedType; |
| 17 | +use PHPStan\Type\NullType; |
| 18 | +use PHPStan\Type\TypeCombinator; |
| 19 | + |
| 20 | +use function count; |
| 21 | + |
| 22 | +final class GetFlagValueFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension { |
| 23 | + |
| 24 | + public function isFunctionSupported( FunctionReflection $functionReflection ): bool { |
| 25 | + return $functionReflection->getName() === 'WP_CLI\Utils\get_flag_value'; |
| 26 | + } |
| 27 | + |
| 28 | + public function getTypeFromFunctionCall( |
| 29 | + FunctionReflection $functionReflection, |
| 30 | + FuncCall $functionCall, |
| 31 | + Scope $scope |
| 32 | + ): Type { |
| 33 | + $args = $functionCall->getArgs(); |
| 34 | + |
| 35 | + if ( count( $args ) < 2 ) { |
| 36 | + // Not enough arguments, fall back to the function's declared return type. |
| 37 | + return $functionReflection->getVariants()[0]->getReturnType(); |
| 38 | + } |
| 39 | + |
| 40 | + $assocArgsType = $scope->getType( $args[0]->value ); |
| 41 | + $flagArgType = $scope->getType( $args[1]->value ); |
| 42 | + |
| 43 | + // 2. Determine the default type |
| 44 | + $defaultType = isset( $args[2] ) ? $scope->getType( $args[2]->value ) : new NullType(); |
| 45 | + |
| 46 | + $flagConstantStrings = $flagArgType->getConstantStrings(); |
| 47 | + |
| 48 | + if ( count( $flagConstantStrings ) !== 1 ) { |
| 49 | + // Flag name is dynamic or not a string. |
| 50 | + // Return type is a union of all possible values in $assoc_args + default type. |
| 51 | + return $this->getDynamicFlagFallbackType( $assocArgsType, $defaultType ); |
| 52 | + } |
| 53 | + |
| 54 | + // 4. Flag is a single constant string. |
| 55 | + $flagValue = $flagConstantStrings[0]->getValue(); |
| 56 | + |
| 57 | + // 4.a. If $assoc_args is a single ConstantArray: |
| 58 | + $assocConstantArrays = $assocArgsType->getConstantArrays(); |
| 59 | + if ( count( $assocConstantArrays ) === 1 ) { |
| 60 | + $assocArgsConstantArray = $assocConstantArrays[0]; |
| 61 | + $keyTypes = $assocArgsConstantArray->getKeyTypes(); |
| 62 | + $valueTypes = $assocArgsConstantArray->getValueTypes(); |
| 63 | + $resolvedValueType = null; |
| 64 | + |
| 65 | + foreach ( $keyTypes as $index => $keyType ) { |
| 66 | + $keyConstantStrings = $keyType->getConstantStrings(); |
| 67 | + if ( count( $keyConstantStrings ) === 1 && $keyConstantStrings[0]->getValue() === $flagValue ) { |
| 68 | + $resolvedValueType = $valueTypes[ $index ]; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if ( null !== $resolvedValueType ) { |
| 74 | + // Key definitely exists and has a resolved type. |
| 75 | + return $resolvedValueType; |
| 76 | + } else { |
| 77 | + // Key definitely does not exist in this constant array. |
| 78 | + return $defaultType; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // 4.b. $assoc_args is not a single ConstantArray (but $flagValue is known): |
| 83 | + // Use getOffsetValueType for other array-like types. |
| 84 | + $valueForKeyType = $assocArgsType->getOffsetValueType( new ConstantStringType( $flagValue ) ); |
| 85 | + |
| 86 | + // The key might exist, or its presence is unknown. |
| 87 | + // The function returns $assoc_args[$flag] if set, otherwise $default. |
| 88 | + return TypeCombinator::union( $valueForKeyType, $defaultType ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Handles the case where the flag name is not a single known constant string. |
| 93 | + * The return type is a union of all possible values in $assocArgsType and $defaultType. |
| 94 | + */ |
| 95 | + private function getDynamicFlagFallbackType( Type $assocArgsType, Type $defaultType ): Type { |
| 96 | + $possibleValueTypes = []; |
| 97 | + |
| 98 | + $assocConstantArrays = $assocArgsType->getConstantArrays(); |
| 99 | + if ( count( $assocConstantArrays ) === 1 ) { // It's one specific constant array |
| 100 | + $constantArray = $assocConstantArrays[0]; |
| 101 | + if ( count( $constantArray->getValueTypes() ) > 0 ) { |
| 102 | + $possibleValueTypes = $constantArray->getValueTypes(); |
| 103 | + } |
| 104 | + } else { |
| 105 | + $possibleValueTypes[] = new MixedType(); |
| 106 | + } |
| 107 | + |
| 108 | + if ( empty( $possibleValueTypes ) ) { |
| 109 | + return $defaultType; |
| 110 | + } |
| 111 | + |
| 112 | + return TypeCombinator::union( $defaultType, ...$possibleValueTypes ); |
| 113 | + } |
| 114 | +} |
0 commit comments