|
| 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\Constant\ConstantArrayType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use PHPStan\Type\TypeCombinator; |
| 18 | + |
| 19 | +use function count; |
| 20 | + |
| 21 | +final class GetFlagValueFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension { |
| 22 | + |
| 23 | + public function isFunctionSupported( FunctionReflection $functionReflection ): bool { |
| 24 | + return $functionReflection->getName() === 'WP_CLI\Utils\get_flag_value'; |
| 25 | + } |
| 26 | + |
| 27 | + public function getTypeFromFunctionCall( |
| 28 | + FunctionReflection $functionReflection, |
| 29 | + FuncCall $functionCall, |
| 30 | + Scope $scope |
| 31 | + ): Type { |
| 32 | + $args = $functionCall->getArgs(); |
| 33 | + |
| 34 | + // Ensure we have at least two arguments: $assoc_args and $flag |
| 35 | + if ( count( $args ) < 2 ) { |
| 36 | + // Not enough arguments, fall back to the function's declared return type or mixed |
| 37 | + return $functionReflection->getVariants()[0]->getReturnType(); |
| 38 | + } |
| 39 | + |
| 40 | + $assocArgsType = $scope->getType( $args[0]->value ); |
| 41 | + $flagArgType = $scope->getType( $args[1]->value ); |
| 42 | + |
| 43 | + // Determine the default type |
| 44 | + $defaultType = isset( $args[2] ) ? $scope->getType( $args[2]->value ) : new \PHPStan\Type\NullType(); |
| 45 | + |
| 46 | + // We can only be precise if $flag is a constant string |
| 47 | + if ( ! $flagArgType->isConstantValue()->yes() || ( ! $flagArgType->toInteger() instanceof ConstantIntegerType && ! $flagArgType->toString() instanceof ConstantStringType ) ) { |
| 48 | + // If $flag is not a constant string, we cannot know which key to check. |
| 49 | + // The return type will be a union of the array's possible value types and the default type. |
| 50 | + if ( $assocArgsType instanceof ConstantArrayType ) { |
| 51 | + $valueTypes = []; |
| 52 | + foreach ( $assocArgsType->getValueTypes() as $valueType ) { |
| 53 | + $valueTypes[] = $valueType; |
| 54 | + } |
| 55 | + if ( count( $valueTypes ) > 0 ) { |
| 56 | + return TypeCombinator::union( ...$valueTypes ); |
| 57 | + } |
| 58 | + return $defaultType; // Array is empty or has no predictable value types |
| 59 | + } elseif ( $assocArgsType instanceof \PHPStan\Type\ArrayType ) { |
| 60 | + return TypeCombinator::union( $assocArgsType->getItemType(), $defaultType ); |
| 61 | + } |
| 62 | + // Fallback if $assocArgsType isn't a well-defined array type |
| 63 | + return new MixedType(); |
| 64 | + } |
| 65 | + |
| 66 | + $flagValue = $flagArgType->getValue(); |
| 67 | + |
| 68 | + // If $assoc_args is a constant array, we can check if the key exists |
| 69 | + if ( $assocArgsType->isConstantValue()->yes() && $assocArgsType->toArray() instanceof ConstantArrayType ) { |
| 70 | + $keyTypes = $assocArgsType->getKeyTypes(); |
| 71 | + $valueTypes = $assocArgsType->getValueTypes(); |
| 72 | + $resolvedValueType = null; |
| 73 | + |
| 74 | + foreach ( $keyTypes as $index => $keyType ) { |
| 75 | + if ( $keyType->isConstantValue()->yes() && $keyType->toString() instanceof ConstantStringType && $keyType->getValue() === $flagValue ) { |
| 76 | + $resolvedValueType = $valueTypes[ $index ]; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if ( null !== $resolvedValueType ) { |
| 82 | + // Key definitely exists, return its type |
| 83 | + return $resolvedValueType; |
| 84 | + } else { |
| 85 | + // Key definitely does not exist, return default type |
| 86 | + return $defaultType; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // If $assocArgsType is a general ArrayType, we can't be sure if the specific flag exists. |
| 91 | + // The function's logic is: isset( $assoc_args[ $flag ] ) ? $assoc_args[ $flag ] : $default; |
| 92 | + // So, it's a union of the potential value type from the array and the default type. |
| 93 | + if ( $assocArgsType->isArray()->yes() ) { |
| 94 | + // We don't know IF the key $flagValue exists. |
| 95 | + // PHPStan's ArrayType has an itemType which represents the type of values in the array. |
| 96 | + // This is the best we can do for a generic array. |
| 97 | + return TypeCombinator::union( $assocArgsType->getItemType(), $defaultType ); |
| 98 | + } |
| 99 | + |
| 100 | + // Fallback for other types of $assocArgsType or if we can't determine. |
| 101 | + // This should ideally be the union of what the array could contain for that key and the default. |
| 102 | + // For simplicity, if not a ConstantArrayType or ArrayType, return mixed or a broad union. |
| 103 | + // In a real-world scenario with more complex types, you might query $assocArgsType->getOffsetValueType(new ConstantStringType($flagValue)) |
| 104 | + // and then union with $defaultType. |
| 105 | + $offsetValueType = $assocArgsType->getOffsetValueType( new ConstantStringType( $flagValue ) ); |
| 106 | + if ( ! $offsetValueType instanceof MixedType || $offsetValueType->isExplicitMixed() ) { |
| 107 | + return TypeCombinator::union( $offsetValueType, $defaultType ); |
| 108 | + } |
| 109 | + |
| 110 | + return new MixedType(); // Default fallback |
| 111 | + } |
| 112 | +} |
0 commit comments