|
| 1 | +<?php |
| 2 | + |
| 3 | +//phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter |
| 4 | + |
| 5 | +/** |
| 6 | + * Set return type of get_terms() and related functions. |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace SzepeViktor\PHPStan\WordPress; |
| 12 | + |
| 13 | +use PhpParser\Node\Expr\FuncCall; |
| 14 | +use PHPStan\Analyser\Scope; |
| 15 | +use PHPStan\Reflection\FunctionReflection; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use PHPStan\Type\ArrayType; |
| 18 | +use PHPStan\Type\IntegerType; |
| 19 | +use PHPStan\Type\ObjectType; |
| 20 | +use PHPStan\Type\StringType; |
| 21 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 22 | +use PHPStan\Type\Constant\ConstantStringType; |
| 23 | +use PHPStan\Type\ConstantScalarType; |
| 24 | +use PHPStan\Type\TypeCombinator; |
| 25 | + |
| 26 | +class GetTermsDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension |
| 27 | +{ |
| 28 | + private const SUPPORTED_FUNCTIONS = [ |
| 29 | + 'get_tags' => 0, |
| 30 | + 'get_terms' => 0, |
| 31 | + 'wp_get_object_terms' => 2, |
| 32 | + 'wp_get_post_categories' => 1, |
| 33 | + 'wp_get_post_tags' => 1, |
| 34 | + 'wp_get_post_terms' => 2, |
| 35 | + ]; |
| 36 | + |
| 37 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 38 | + { |
| 39 | + return array_key_exists($functionReflection->getName(), self::SUPPORTED_FUNCTIONS); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @see https://developer.wordpress.org/reference/functions/get_terms/ |
| 44 | + * @see https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ |
| 45 | + */ |
| 46 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type |
| 47 | + { |
| 48 | + $name = $functionReflection->getName(); |
| 49 | + $argsParameterPosition = self::SUPPORTED_FUNCTIONS[$name] ?? null; |
| 50 | + |
| 51 | + if ($argsParameterPosition === null) { |
| 52 | + throw new \PHPStan\ShouldNotHappenException( |
| 53 | + sprintf( |
| 54 | + 'Could not detect parameter position for function %s()', |
| 55 | + $name |
| 56 | + ) |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + // Called without arguments |
| 61 | + if (! isset($functionCall->getArgs()[$argsParameterPosition])) { |
| 62 | + return self::termsType(); |
| 63 | + } |
| 64 | + |
| 65 | + $argument = $scope->getType($functionCall->getArgs()[$argsParameterPosition]->value); |
| 66 | + |
| 67 | + if (!($argument instanceof ConstantArrayType)) { |
| 68 | + // Without constant array argument return default return type |
| 69 | + return self::defaultType(); |
| 70 | + } |
| 71 | + |
| 72 | + $args = self::getArgs($argument); |
| 73 | + |
| 74 | + if ($args === null) { |
| 75 | + return self::defaultType(); |
| 76 | + } |
| 77 | + |
| 78 | + if (isset($args['count']) && $args['count'] === true) { |
| 79 | + return self::countType(); |
| 80 | + } |
| 81 | + |
| 82 | + if (! isset($args['fields'], $args['count']) || ! is_string($args['fields'])) { |
| 83 | + return self::defaultType(); |
| 84 | + } |
| 85 | + |
| 86 | + return self::deduceTypeFromFields($args['fields']); |
| 87 | + } |
| 88 | + |
| 89 | + protected static function deduceTypeFromFields(string $fields): Type |
| 90 | + { |
| 91 | + switch ($fields) { |
| 92 | + case 'count': |
| 93 | + return self::countType(); |
| 94 | + case 'names': |
| 95 | + case 'slugs': |
| 96 | + case 'id=>name': |
| 97 | + case 'id=>slug': |
| 98 | + return self::slugsType(); |
| 99 | + case 'ids': |
| 100 | + case 'tt_ids': |
| 101 | + return self::idsType(); |
| 102 | + case 'id=>parent': |
| 103 | + return self::parentsType(); |
| 104 | + case 'all': |
| 105 | + case 'all_with_object_id': |
| 106 | + default: |
| 107 | + return self::termsType(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return array<string, mixed> |
| 113 | + */ |
| 114 | + protected static function getArgs(ConstantArrayType $argument): ?array |
| 115 | + { |
| 116 | + $args = [ |
| 117 | + 'fields' => 'all', |
| 118 | + 'count' => false, |
| 119 | + ]; |
| 120 | + |
| 121 | + foreach ($argument->getKeyTypes() as $index => $key) { |
| 122 | + if (! $key instanceof ConstantStringType) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + unset($args[$key->getValue()]); |
| 127 | + $fieldsType = $argument->getValueTypes()[$index]; |
| 128 | + if (!($fieldsType instanceof ConstantScalarType)) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + $args[$key->getValue()] = $fieldsType->getValue(); |
| 133 | + } |
| 134 | + |
| 135 | + return $args; |
| 136 | + } |
| 137 | + |
| 138 | + protected static function countType(): Type |
| 139 | + { |
| 140 | + return TypeCombinator::union( |
| 141 | + new StringType(), |
| 142 | + new ObjectType('WP_Error') |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + protected static function slugsType(): Type |
| 147 | + { |
| 148 | + return TypeCombinator::union( |
| 149 | + new ArrayType(new IntegerType(), new StringType()), |
| 150 | + new ObjectType('WP_Error') |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + protected static function idsType(): Type |
| 155 | + { |
| 156 | + return TypeCombinator::union( |
| 157 | + new ArrayType(new IntegerType(), new IntegerType()), |
| 158 | + new ObjectType('WP_Error') |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + protected static function parentsType(): Type |
| 163 | + { |
| 164 | + return TypeCombinator::union( |
| 165 | + new ArrayType(new IntegerType(), new StringType()), |
| 166 | + new ObjectType('WP_Error') |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + protected static function termsType(): Type |
| 171 | + { |
| 172 | + return TypeCombinator::union( |
| 173 | + new ArrayType(new IntegerType(), new ObjectType('WP_Term')), |
| 174 | + new ObjectType('WP_Error') |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + protected static function defaultType(): Type |
| 179 | + { |
| 180 | + return TypeCombinator::union( |
| 181 | + self::termsType(), |
| 182 | + self::idsType(), |
| 183 | + self::slugsType(), |
| 184 | + self::countType() |
| 185 | + ); |
| 186 | + } |
| 187 | +} |
0 commit comments