|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SzepeViktor\PHPStan\WordPress; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr\FuncCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\FunctionReflection; |
| 10 | +use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 11 | +use PHPStan\Type\Accessory\AccessoryType; |
| 12 | +use PHPStan\Type\Constant\ConstantStringType; |
| 13 | +use PHPStan\Type\GeneralizePrecision; |
| 14 | +use PHPStan\Type\StringType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\TypeCombinator; |
| 17 | +use PHPStan\Type\TypeUtils; |
| 18 | +use PHPStan\Type\UnionType; |
| 19 | + |
| 20 | +final class NormalizeWhitespaceDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension |
| 21 | +{ |
| 22 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 23 | + { |
| 24 | + return $functionReflection->getName() === 'normalize_whitespace'; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @see https://developer.wordpress.org/reference/functions/normalize_whitespace/ |
| 29 | + * |
| 30 | + * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter |
| 31 | + */ |
| 32 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type |
| 33 | + { |
| 34 | + if (count($functionCall->getArgs()) < 1) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + $argType = $scope->getType($functionCall->getArgs()[0]->value); |
| 39 | + if (! $scope->isDeclareStrictTypes()) { |
| 40 | + $argType = $argType->toString(); |
| 41 | + } |
| 42 | + |
| 43 | + if (! $argType->isString()->yes()) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + $argTypes = $argType instanceof UnionType ? $argType->getTypes() : [$argType]; |
| 48 | + |
| 49 | + $types = []; |
| 50 | + foreach ($argTypes as $type) { |
| 51 | + if ($type->isConstantValue()->yes()) { |
| 52 | + $types[] = $this->getTypeFromConstantString($type->getConstantStrings()[0]); |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if ($type->isNonFalsyString()->yes()) { |
| 57 | + $types[] = $this->getTypeFromNonFalsyString($type); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if ($type->isNonEmptyString()->yes()) { |
| 62 | + $types[] = $this->getTypeFromNonEmptyString($type); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $types[] = $type; |
| 67 | + } |
| 68 | + |
| 69 | + return TypeCombinator::union(...$types); |
| 70 | + } |
| 71 | + |
| 72 | + private function getTypeFromConstantString(ConstantStringType $type): Type |
| 73 | + { |
| 74 | + $typeValue = $type->getValue(); |
| 75 | + $type = new ConstantStringType(trim($typeValue)); |
| 76 | + |
| 77 | + if ($type->isNonEmptyString()->no() || $type->isNonFalsyString()->no()) { |
| 78 | + return $type; |
| 79 | + } |
| 80 | + |
| 81 | + return $type->generalize(GeneralizePrecision::moreSpecific()); |
| 82 | + } |
| 83 | + |
| 84 | + private function getTypeFromNonFalsyString(Type $type): Type |
| 85 | + { |
| 86 | + $types = array_merge( |
| 87 | + [new StringType(), new AccessoryNonEmptyStringType()], |
| 88 | + array_filter( |
| 89 | + TypeUtils::getAccessoryTypes($type), |
| 90 | + static function (AccessoryType $accessoryType): bool { |
| 91 | + return ! $accessoryType->isNonFalsyString()->yes(); |
| 92 | + } |
| 93 | + ) |
| 94 | + ); |
| 95 | + |
| 96 | + return TypeCombinator::intersect(...$types); |
| 97 | + } |
| 98 | + |
| 99 | + private function getTypeFromNonEmptyString(Type $type): Type |
| 100 | + { |
| 101 | + $types = array_merge( |
| 102 | + [new StringType()], |
| 103 | + array_filter( |
| 104 | + TypeUtils::getAccessoryTypes($type), |
| 105 | + static function (AccessoryType $accessoryType): bool { |
| 106 | + return ! $accessoryType->isNonEmptyString()->yes(); |
| 107 | + } |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + return TypeCombinator::intersect(...$types); |
| 112 | + } |
| 113 | +} |
0 commit comments