|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Php; |
| 4 | + |
| 5 | +use Nette\Utils\Strings; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\FunctionReflection; |
| 9 | +use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
| 10 | +use PHPStan\Type\Accessory\AccessoryLowercaseStringType; |
| 11 | +use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 12 | +use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
| 13 | +use PHPStan\Type\Accessory\AccessoryNumericStringType; |
| 14 | +use PHPStan\Type\Accessory\AccessoryUppercaseStringType; |
| 15 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 16 | +use PHPStan\Type\Constant\ConstantStringType; |
| 17 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 18 | +use PHPStan\Type\IntegerRangeType; |
| 19 | +use PHPStan\Type\IntersectionType; |
| 20 | +use PHPStan\Type\NeverType; |
| 21 | +use PHPStan\Type\StringType; |
| 22 | +use PHPStan\Type\Type; |
| 23 | +use PHPStan\Type\TypeCombinator; |
| 24 | + |
| 25 | +use function count; |
| 26 | +use function str_repeat; |
| 27 | +use function strlen; |
| 28 | + |
| 29 | +final class StrrevFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 30 | +{ |
| 31 | + |
| 32 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 33 | + { |
| 34 | + return $functionReflection->getName() === 'strrev'; |
| 35 | + } |
| 36 | + |
| 37 | + public function getTypeFromFunctionCall( |
| 38 | + FunctionReflection $functionReflection, |
| 39 | + FuncCall $functionCall, |
| 40 | + Scope $scope, |
| 41 | + ): ?Type |
| 42 | + { |
| 43 | + $args = $functionCall->getArgs(); |
| 44 | + if (count($args) < 1) { |
| 45 | + return new StringType(); |
| 46 | + } |
| 47 | + |
| 48 | + $inputType = $scope->getType($args[0]->value); |
| 49 | + $constantStrings = $inputType->getConstantStrings(); |
| 50 | + if (count($constantStrings) > 0) { |
| 51 | + $resultTypes = []; |
| 52 | + foreach ($constantStrings as $constantString) { |
| 53 | + $resultTypes[] = new ConstantStringType(strrev($constantString->getValue())); |
| 54 | + } |
| 55 | + |
| 56 | + return TypeCombinator::union(...$resultTypes); |
| 57 | + } |
| 58 | + |
| 59 | + $accessoryTypes = []; |
| 60 | + if ($inputType->isNonFalsyString()->yes()) { |
| 61 | + $accessoryTypes[] = new AccessoryNonFalsyStringType(); |
| 62 | + } elseif ($inputType->isNonEmptyString()->yes()) { |
| 63 | + $accessoryTypes[] = new AccessoryNonEmptyStringType(); |
| 64 | + } |
| 65 | + if ($inputType->isLowercaseString()->yes()) { |
| 66 | + $accessoryTypes[] = new AccessoryLowercaseStringType(); |
| 67 | + } |
| 68 | + if ($inputType->isUppercaseString()->yes()) { |
| 69 | + $accessoryTypes[] = new AccessoryUppercaseStringType(); |
| 70 | + } |
| 71 | + |
| 72 | + if (count($accessoryTypes) > 0) { |
| 73 | + $accessoryTypes[] = new StringType(); |
| 74 | + |
| 75 | + return new IntersectionType($accessoryTypes); |
| 76 | + } |
| 77 | + |
| 78 | + return new StringType(); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments