|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Set return type of \WP_CLI\Utils\parse_url(). |
| 5 | + * |
| 6 | + * Based on ParseUrlFunctionDynamicReturnTypeExtension in PHPStan itself |
| 7 | + * and WpParseUrlFunctionDynamicReturnTypeExtension in the PHPStan WordPress extension. |
| 8 | + * |
| 9 | + * phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace WP_CLI\Tests\PHPStan; |
| 15 | + |
| 16 | +use PhpParser\Node\Expr\FuncCall; |
| 17 | +use PHPStan\Analyser\Scope; |
| 18 | +use PHPStan\Reflection\FunctionReflection; |
| 19 | +use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
| 20 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 21 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 22 | +use PHPStan\Type\Constant\ConstantStringType; |
| 23 | +use PHPStan\Type\IntegerRangeType; |
| 24 | +use PHPStan\Type\NullType; |
| 25 | +use PHPStan\Type\StringType; |
| 26 | +use PHPStan\Type\Type; |
| 27 | +use PHPStan\Type\TypeCombinator; |
| 28 | + |
| 29 | +use function count; |
| 30 | +use function parse_url; |
| 31 | + |
| 32 | +use const PHP_URL_FRAGMENT; |
| 33 | +use const PHP_URL_HOST; |
| 34 | +use const PHP_URL_PASS; |
| 35 | +use const PHP_URL_PATH; |
| 36 | +use const PHP_URL_PORT; |
| 37 | +use const PHP_URL_QUERY; |
| 38 | +use const PHP_URL_SCHEME; |
| 39 | +use const PHP_URL_USER; |
| 40 | + |
| 41 | +final class ParseUrlFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension { |
| 42 | + |
| 43 | + /** @var array<int, \PHPStan\Type\Type>|null */ |
| 44 | + private $componentTypesPairedConstants = null; |
| 45 | + |
| 46 | + /** @var array<string, \PHPStan\Type\Type>|null */ |
| 47 | + private $componentTypesPairedStrings = null; |
| 48 | + |
| 49 | + /** @var \PHPStan\Type\Type|null */ |
| 50 | + private $allComponentsTogetherType = null; |
| 51 | + |
| 52 | + public function isFunctionSupported( FunctionReflection $functionReflection ): bool { |
| 53 | + return $functionReflection->getName() === 'WP_CLI\Utils\parse_url'; |
| 54 | + } |
| 55 | + |
| 56 | + public function getTypeFromFunctionCall( FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope ): ?Type { |
| 57 | + if ( count( $functionCall->getArgs() ) < 1 ) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + $this->cacheReturnTypes(); |
| 62 | + |
| 63 | + $componentType = new ConstantIntegerType( -1 ); |
| 64 | + |
| 65 | + if ( count( $functionCall->getArgs() ) > 1 ) { |
| 66 | + $componentType = $scope->getType( $functionCall->getArgs()[1]->value ); |
| 67 | + |
| 68 | + if ( ! $componentType->isConstantValue()->yes() ) { |
| 69 | + return $this->createAllComponentsReturnType(); |
| 70 | + } |
| 71 | + |
| 72 | + $componentType = $componentType->toInteger(); |
| 73 | + |
| 74 | + if ( ! $componentType instanceof ConstantIntegerType ) { |
| 75 | + return $this->createAllComponentsReturnType(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $urlType = $scope->getType( $functionCall->getArgs()[0]->value ); |
| 80 | + if ( count( $urlType->getConstantStrings() ) > 0 ) { |
| 81 | + $types = []; |
| 82 | + foreach ( $urlType->getConstantStrings() as $constantString ) { |
| 83 | + try { |
| 84 | + // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 85 | + $result = @parse_url( $constantString->getValue(), $componentType->getValue() ); |
| 86 | + } catch ( \Error $e ) { |
| 87 | + $types[] = new ConstantBooleanType( false ); |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + $types[] = $scope->getTypeFromValue( $result ); |
| 92 | + } |
| 93 | + |
| 94 | + return TypeCombinator::union( ...$types ); |
| 95 | + } |
| 96 | + |
| 97 | + if ( $componentType->getValue() === -1 ) { |
| 98 | + return TypeCombinator::union( $this->createComponentsArray(), new ConstantBooleanType( false ) ); |
| 99 | + } |
| 100 | + |
| 101 | + return $this->componentTypesPairedConstants[ $componentType->getValue() ] ?? new ConstantBooleanType( false ); |
| 102 | + } |
| 103 | + |
| 104 | + private function createAllComponentsReturnType(): Type { |
| 105 | + if ( null === $this->allComponentsTogetherType ) { |
| 106 | + $returnTypes = [ |
| 107 | + new ConstantBooleanType( false ), |
| 108 | + new NullType(), |
| 109 | + IntegerRangeType::fromInterval( 0, 65535 ), |
| 110 | + new StringType(), |
| 111 | + $this->createComponentsArray(), |
| 112 | + ]; |
| 113 | + |
| 114 | + $this->allComponentsTogetherType = TypeCombinator::union( ...$returnTypes ); |
| 115 | + } |
| 116 | + |
| 117 | + return $this->allComponentsTogetherType; |
| 118 | + } |
| 119 | + |
| 120 | + private function createComponentsArray(): Type { |
| 121 | + $builder = ConstantArrayTypeBuilder::createEmpty(); |
| 122 | + |
| 123 | + if ( null === $this->componentTypesPairedStrings ) { |
| 124 | + throw new \PHPStan\ShouldNotHappenException(); |
| 125 | + } |
| 126 | + |
| 127 | + foreach ( $this->componentTypesPairedStrings as $componentName => $componentValueType ) { |
| 128 | + $builder->setOffsetValueType( new ConstantStringType( $componentName ), $componentValueType, true ); |
| 129 | + } |
| 130 | + |
| 131 | + return $builder->getArray(); |
| 132 | + } |
| 133 | + |
| 134 | + private function cacheReturnTypes(): void { |
| 135 | + if ( null !== $this->componentTypesPairedConstants ) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + $stringType = new StringType(); |
| 140 | + $port = IntegerRangeType::fromInterval( 0, 65535 ); |
| 141 | + $falseType = new ConstantBooleanType( false ); |
| 142 | + $nullType = new NullType(); |
| 143 | + |
| 144 | + $stringOrFalseOrNull = TypeCombinator::union( $stringType, $falseType, $nullType ); |
| 145 | + $portOrFalseOrNull = TypeCombinator::union( $port, $falseType, $nullType ); |
| 146 | + |
| 147 | + $this->componentTypesPairedConstants = [ |
| 148 | + PHP_URL_SCHEME => $stringOrFalseOrNull, |
| 149 | + PHP_URL_HOST => $stringOrFalseOrNull, |
| 150 | + PHP_URL_PORT => $portOrFalseOrNull, |
| 151 | + PHP_URL_USER => $stringOrFalseOrNull, |
| 152 | + PHP_URL_PASS => $stringOrFalseOrNull, |
| 153 | + PHP_URL_PATH => $stringOrFalseOrNull, |
| 154 | + PHP_URL_QUERY => $stringOrFalseOrNull, |
| 155 | + PHP_URL_FRAGMENT => $stringOrFalseOrNull, |
| 156 | + ]; |
| 157 | + |
| 158 | + $this->componentTypesPairedStrings = [ |
| 159 | + 'scheme' => $stringType, |
| 160 | + 'host' => $stringType, |
| 161 | + 'port' => $port, |
| 162 | + 'user' => $stringType, |
| 163 | + 'pass' => $stringType, |
| 164 | + 'path' => $stringType, |
| 165 | + 'query' => $stringType, |
| 166 | + 'fragment' => $stringType, |
| 167 | + ]; |
| 168 | + } |
| 169 | +} |
0 commit comments