|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WP_CLI\Tests\PHPStan; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr\StaticCall; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Type\Type; |
| 12 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 13 | +use PHPStan\Type\NullType; |
| 14 | +use PHPStan\Type\StringType; |
| 15 | +use PHPStan\Type\IntegerType; |
| 16 | +use PHPStan\Type\MixedType; |
| 17 | +use PHPStan\Type\ArrayType; |
| 18 | +use PHPStan\Type\ObjectWithoutClassType; |
| 19 | +use PHPStan\Type\Constant\ConstantStringType; |
| 20 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 21 | +use PHPStan\Type\TypeCombinator; |
| 22 | +use PHPStan\Type\ObjectShapeType; |
| 23 | +use PHPStan\Type\NeverType; |
| 24 | + |
| 25 | +class WPCliRuncommandDynamicReturnTypeExtension 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() === 'runcommand'; |
| 33 | + } |
| 34 | + |
| 35 | + public function getTypeFromStaticMethodCall( |
| 36 | + MethodReflection $methodReflection, |
| 37 | + StaticCall $methodCall, |
| 38 | + Scope $scope |
| 39 | + ): Type { |
| 40 | + $args = $methodCall->getArgs(); |
| 41 | + |
| 42 | + /** @var ConstantBooleanType|ConstantStringType $returnOption */ |
| 43 | + $returnOption = new ConstantBooleanType( true ); |
| 44 | + /** @var ConstantBooleanType|ConstantStringType $parseOption */ |
| 45 | + $parseOption = new ConstantBooleanType( false ); |
| 46 | + /** @var ConstantBooleanType $exitOnErrorOption */ |
| 47 | + $exitOnErrorOption = new ConstantBooleanType( true ); |
| 48 | + |
| 49 | + $optionsAreStaticallyKnown = true; |
| 50 | + |
| 51 | + if ( isset( $args[1] ) && $args[1] instanceof Arg ) { |
| 52 | + $optionsNode = $args[1]->value; |
| 53 | + $optionsType = $scope->getType( $optionsNode ); |
| 54 | + |
| 55 | + if ( $optionsType->isConstantArray()->yes() ) { |
| 56 | + $constantArrayTypes = $optionsType->getConstantArrays(); |
| 57 | + if ( count( $constantArrayTypes ) === 1 ) { |
| 58 | + $constantArrayType = $constantArrayTypes[0]; |
| 59 | + $keyTypes = $constantArrayType->getKeyTypes(); |
| 60 | + $valueTypes = $constantArrayType->getValueTypes(); |
| 61 | + |
| 62 | + foreach ( $keyTypes as $i => $keyType ) { |
| 63 | + $keyConstantStrings = $keyType->getConstantStrings(); |
| 64 | + if ( count( $keyConstantStrings ) !== 1 ) { |
| 65 | + $optionsAreStaticallyKnown = false; |
| 66 | + break; |
| 67 | + } |
| 68 | + $keyName = $keyConstantStrings[0]->getValue(); |
| 69 | + $currentOptionValueType = $valueTypes[ $i ]; |
| 70 | + |
| 71 | + switch ( $keyName ) { |
| 72 | + case 'return': |
| 73 | + $valueConstantStrings = $currentOptionValueType->getConstantStrings(); |
| 74 | + if ( count( $valueConstantStrings ) === 1 && $currentOptionValueType->isScalar()->yes() ) { |
| 75 | + $returnOption = $valueConstantStrings[0]; |
| 76 | + } elseif ( $currentOptionValueType->isTrue()->yes() ) { |
| 77 | + $returnOption = new ConstantBooleanType( true ); |
| 78 | + } elseif ( $currentOptionValueType->isFalse()->yes() ) { |
| 79 | + $returnOption = new ConstantBooleanType( false ); |
| 80 | + } else { |
| 81 | + $optionsAreStaticallyKnown = false; |
| 82 | + } |
| 83 | + break; |
| 84 | + case 'parse': |
| 85 | + $valueConstantStrings = $currentOptionValueType->getConstantStrings(); |
| 86 | + $isExactlyJsonString = ( count( $valueConstantStrings ) === 1 && $valueConstantStrings[0]->getValue() === 'json' && $currentOptionValueType->isScalar()->yes() ); |
| 87 | + |
| 88 | + if ( $isExactlyJsonString ) { |
| 89 | + $parseOption = $valueConstantStrings[0]; |
| 90 | + } elseif ( $$currentOptionValueType->isFalse()->yes() ) { |
| 91 | + $parseOption = new ConstantBooleanType( false ); |
| 92 | + } else { |
| 93 | + // Not a single, clear constant we handle for a "known" path |
| 94 | + $parseOption = new ConstantBooleanType( false ); // Default effect |
| 95 | + $optionsAreStaticallyKnown = false; |
| 96 | + } |
| 97 | + break; |
| 98 | + case 'exit_error': |
| 99 | + if ( $currentOptionValueType->isTrue()->yes() ) { |
| 100 | + $exitOnErrorOption = new ConstantBooleanType( true ); |
| 101 | + } elseif ( $currentOptionValueType->isFalse()->yes() ) { |
| 102 | + $exitOnErrorOption = new ConstantBooleanType( false ); |
| 103 | + } else { |
| 104 | + $optionsAreStaticallyKnown = false; |
| 105 | + } |
| 106 | + break; |
| 107 | + } |
| 108 | + if ( ! $optionsAreStaticallyKnown ) { |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + } else { |
| 113 | + $optionsAreStaticallyKnown = false; |
| 114 | + } |
| 115 | + } else { |
| 116 | + $optionsAreStaticallyKnown = false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if ( ! $optionsAreStaticallyKnown ) { |
| 121 | + return TypeCombinator::union( $this->getFallbackUnionTypeWithoutNever(), new NeverType() ); |
| 122 | + } |
| 123 | + |
| 124 | + $normalReturnType = $this->determineNormalReturnType( $returnOption, $parseOption ); |
| 125 | + |
| 126 | + if ( $exitOnErrorOption->getValue() === true ) { |
| 127 | + if ( $normalReturnType instanceof NeverType ) { |
| 128 | + return $normalReturnType; |
| 129 | + } |
| 130 | + return TypeCombinator::union( $normalReturnType, new NeverType() ); |
| 131 | + } |
| 132 | + |
| 133 | + return $normalReturnType; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param ConstantBooleanType|ConstantStringType $returnOptionValue |
| 138 | + * @param ConstantBooleanType|ConstantStringType $parseOptionValue |
| 139 | + */ |
| 140 | + private function determineNormalReturnType( Type $returnOptionValue, Type $parseOptionValue ): Type { |
| 141 | + $returnConstantStrings = $returnOptionValue->getConstantStrings(); |
| 142 | + $return_val = count( $returnConstantStrings ) === 1 ? $returnConstantStrings[0]->getValue() : null; |
| 143 | + |
| 144 | + $parseConstantStrings = $parseOptionValue->getConstantStrings(); |
| 145 | + $parseIsJson = count( $parseConstantStrings ) === 1 && $parseConstantStrings[0]->getValue() === 'json'; |
| 146 | + |
| 147 | + if ( 'all' === $return_val ) { |
| 148 | + return $this->createAllObjectType(); |
| 149 | + } |
| 150 | + if ( 'return_code' === $return_val ) { |
| 151 | + return new IntegerType(); |
| 152 | + } |
| 153 | + if ( 'stderr' === $return_val ) { |
| 154 | + return new StringType(); |
| 155 | + } |
| 156 | + if ( $returnOptionValue->isTrue()->yes() || 'stdout' === $return_val ) { |
| 157 | + if ( $parseIsJson ) { |
| 158 | + return TypeCombinator::union( |
| 159 | + new ArrayType( new MixedType(), new MixedType() ), |
| 160 | + new NullType() |
| 161 | + ); |
| 162 | + } |
| 163 | + return new StringType(); |
| 164 | + } |
| 165 | + if ( $returnOptionValue->isFalse()->yes() ) { |
| 166 | + return new NullType(); |
| 167 | + } |
| 168 | + |
| 169 | + return new MixedType( true ); |
| 170 | + } |
| 171 | + |
| 172 | + private function createAllObjectType(): Type { |
| 173 | + $propertyTypes = [ |
| 174 | + 'stdout' => new StringType(), |
| 175 | + 'stderr' => new StringType(), |
| 176 | + 'return_code' => new IntegerType(), |
| 177 | + ]; |
| 178 | + $optionalProperties = []; |
| 179 | + return new ObjectShapeType( $propertyTypes, $optionalProperties ); |
| 180 | + } |
| 181 | + |
| 182 | + private function getFallbackUnionTypeWithoutNever(): Type { |
| 183 | + return TypeCombinator::union( |
| 184 | + new StringType(), |
| 185 | + new IntegerType(), |
| 186 | + $this->createAllObjectType(), |
| 187 | + new ArrayType( new MixedType(), new MixedType() ), |
| 188 | + new ObjectWithoutClassType(), |
| 189 | + new NullType() |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
0 commit comments