|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WP_CLI\Tests\PHPStan; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr\StaticCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\MethodReflection; |
| 10 | +use PHPStan\Type\ArrayType; |
| 11 | +use PHPStan\Type\BooleanType; |
| 12 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 13 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 14 | +use PHPStan\Type\Constant\ConstantStringType; |
| 15 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 16 | +use PHPStan\Type\IntegerType; |
| 17 | +use PHPStan\Type\NullType; |
| 18 | +use PHPStan\Type\StringType; |
| 19 | +use PHPStan\Type\Type; |
| 20 | +use PHPStan\Type\TypeCombinator; |
| 21 | + |
| 22 | +use function count; |
| 23 | +use function array_values; |
| 24 | + |
| 25 | +final class WPCliGetConfigDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension { |
| 26 | + |
| 27 | + public function getClass(): string { |
| 28 | + return 'WP_CLI'; |
| 29 | + } |
| 30 | + |
| 31 | + public function isStaticMethodSupported( MethodReflection $methodReflection ): bool { |
| 32 | + return $methodReflection->getName() === 'get_config'; |
| 33 | + } |
| 34 | + |
| 35 | + public function getTypeFromStaticMethodCall( |
| 36 | + MethodReflection $methodReflection, |
| 37 | + StaticCall $methodCall, |
| 38 | + Scope $scope |
| 39 | + ): Type { |
| 40 | + $args = $methodCall->getArgs(); |
| 41 | + |
| 42 | + if ( count( $args ) === 0 ) { |
| 43 | + return $this->getGlobalConfigArrayType(); |
| 44 | + } |
| 45 | + |
| 46 | + $keyType = $scope->getType( $args[0]->value ); |
| 47 | + |
| 48 | + if ( $keyType->isNull()->yes() ) { |
| 49 | + return $this->getGlobalConfigArrayType(); |
| 50 | + } |
| 51 | + |
| 52 | + $constantStrings = $keyType->getConstantStrings(); |
| 53 | + if ( count( $constantStrings ) > 0 ) { |
| 54 | + $types = []; |
| 55 | + $configMap = $this->getConfigMap(); |
| 56 | + |
| 57 | + foreach ( $constantStrings as $constantString ) { |
| 58 | + $key = $constantString->getValue(); |
| 59 | + if ( isset( $configMap[ $key ] ) ) { |
| 60 | + $types[] = $configMap[ $key ]; |
| 61 | + } else { |
| 62 | + $types[] = new NullType(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if ( count( $types ) > 0 ) { |
| 67 | + $returnType = TypeCombinator::union( ...$types ); |
| 68 | + if ( $keyType->isNull()->maybe() ) { |
| 69 | + $returnType = TypeCombinator::union( $returnType, $this->getGlobalConfigArrayType() ); |
| 70 | + } |
| 71 | + return $returnType; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Fallback for non-constant string or unknown types |
| 76 | + $fallback = TypeCombinator::addNull( $this->getFallbackValueType() ); |
| 77 | + if ( $keyType->isNull()->maybe() ) { |
| 78 | + return TypeCombinator::union( $fallback, $this->getGlobalConfigArrayType() ); |
| 79 | + } |
| 80 | + |
| 81 | + return $fallback; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return array<string, Type> |
| 86 | + */ |
| 87 | + private function getConfigMap(): array { |
| 88 | + $stringType = new StringType(); |
| 89 | + $stringOrNull = TypeCombinator::addNull( $stringType ); |
| 90 | + $stringList = new ArrayType( new IntegerType(), $stringType ); |
| 91 | + $boolType = new BooleanType(); |
| 92 | + $trueOrStringList = TypeCombinator::union( new ConstantBooleanType( true ), $stringList ); |
| 93 | + $stringOrTrue = TypeCombinator::union( $stringType, new ConstantBooleanType( true ) ); |
| 94 | + $stringOrFalse = TypeCombinator::union( $stringType, new ConstantBooleanType( false ) ); |
| 95 | + |
| 96 | + return [ |
| 97 | + 'path' => $stringOrNull, |
| 98 | + 'ssh' => $stringOrNull, |
| 99 | + 'ssh-args' => $stringList, |
| 100 | + 'http' => $stringOrNull, |
| 101 | + 'url' => $stringOrNull, |
| 102 | + 'user' => $stringOrNull, |
| 103 | + 'skip-plugins' => $trueOrStringList, |
| 104 | + 'skip-themes' => $trueOrStringList, |
| 105 | + 'skip-packages' => $boolType, |
| 106 | + 'require' => $stringList, |
| 107 | + 'exec' => $stringList, |
| 108 | + 'context' => $stringType, |
| 109 | + 'debug' => $stringOrTrue, |
| 110 | + 'prompt' => $stringOrFalse, |
| 111 | + 'quiet' => $boolType, |
| 112 | + 'apache_modules' => $stringList, |
| 113 | + 'assume-https' => $boolType, |
| 114 | + 'color' => TypeCombinator::union( $stringType, $boolType ), |
| 115 | + 'disabled_commands' => $stringList, |
| 116 | + 'locale' => $stringType, |
| 117 | + 'allow-root' => $boolType, |
| 118 | + 'alias' => $stringType, |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + private function getGlobalConfigArrayType(): Type { |
| 123 | + $keyTypes = []; |
| 124 | + $valueTypes = []; |
| 125 | + |
| 126 | + foreach ( $this->getConfigMap() as $key => $type ) { |
| 127 | + $keyTypes[] = new ConstantStringType( $key ); |
| 128 | + $valueTypes[] = $type; |
| 129 | + } |
| 130 | + |
| 131 | + return new ConstantArrayType( $keyTypes, $valueTypes ); |
| 132 | + } |
| 133 | + |
| 134 | + private function getFallbackValueType(): Type { |
| 135 | + $types = array_values( $this->getConfigMap() ); |
| 136 | + return TypeCombinator::union( ...$types ); |
| 137 | + } |
| 138 | +} |
0 commit comments