diff --git a/conf/config.neon b/conf/config.neon index 205827901e..90a4b03ec5 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1774,6 +1774,11 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension + - + class: PHPStan\Type\Php\StrrevFunctionReturnTypeExtension + tags: + - phpstan.broker.dynamicFunctionReturnTypeExtension + - class: PHPStan\Type\Php\SubstrDynamicReturnTypeExtension tags: diff --git a/src/Type/Php/StrrevFunctionReturnTypeExtension.php b/src/Type/Php/StrrevFunctionReturnTypeExtension.php new file mode 100644 index 0000000000..3d3ec6b4e8 --- /dev/null +++ b/src/Type/Php/StrrevFunctionReturnTypeExtension.php @@ -0,0 +1,73 @@ +getName() === 'strrev'; + } + + public function getTypeFromFunctionCall( + FunctionReflection $functionReflection, + FuncCall $functionCall, + Scope $scope, + ): Type + { + $args = $functionCall->getArgs(); + if (count($args) < 1) { + return new StringType(); + } + + $inputType = $scope->getType($args[0]->value); + $constantStrings = $inputType->getConstantStrings(); + if (count($constantStrings) > 0) { + $resultTypes = []; + foreach ($constantStrings as $constantString) { + $resultTypes[] = new ConstantStringType(strrev($constantString->getValue())); + } + + return TypeCombinator::union(...$resultTypes); + } + + $accessoryTypes = []; + if ($inputType->isNonFalsyString()->yes()) { + $accessoryTypes[] = new AccessoryNonFalsyStringType(); + } elseif ($inputType->isNonEmptyString()->yes()) { + $accessoryTypes[] = new AccessoryNonEmptyStringType(); + } + if ($inputType->isLowercaseString()->yes()) { + $accessoryTypes[] = new AccessoryLowercaseStringType(); + } + if ($inputType->isUppercaseString()->yes()) { + $accessoryTypes[] = new AccessoryUppercaseStringType(); + } + + if (count($accessoryTypes) > 0) { + $accessoryTypes[] = new StringType(); + + return new IntersectionType($accessoryTypes); + } + + return new StringType(); + } + +} diff --git a/tests/PHPStan/Analyser/nsrt/strrev.php b/tests/PHPStan/Analyser/nsrt/strrev.php new file mode 100644 index 0000000000..2029c56df8 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/strrev.php @@ -0,0 +1,37 @@ +