diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 8be043daf2..a0c29e5f0e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -151,18 +151,17 @@ use function array_values; use function count; use function explode; -use function function_exists; use function get_class; use function implode; use function in_array; use function is_array; use function is_bool; -use function is_numeric; use function is_string; use function ltrim; use function md5; +use function restore_error_handler; +use function set_error_handler; use function sprintf; -use function str_increment; use function str_starts_with; use function strlen; use function strtolower; @@ -1737,33 +1736,24 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu $varScalars = $varType->getConstantScalarValues(); if (count($varScalars) > 0) { - $newTypes = []; + $newValues = []; + // silence string incrementing deprecation warnings + set_error_handler(static fn (): bool => true); foreach ($varScalars as $varValue) { - // until PHP 8.5 it was valid to increment/decrement an empty string. - // see https://github.com/php/php-src/issues/19597 if ($node instanceof Expr\PreInc) { - if ($varValue === '') { - $varValue = '1'; - } elseif ( - is_string($varValue) - && !is_numeric($varValue) - && function_exists('str_increment') - ) { - $varValue = str_increment($varValue); - } elseif (!is_bool($varValue)) { + if (!is_bool($varValue)) { ++$varValue; } } else { - if ($varValue === '') { - $varValue = -1; - } elseif (is_numeric($varValue)) { - --$varValue; - } + --$varValue; // @phpstan-ignore preDec.nonNumeric } - $newTypes[] = $this->getTypeFromValue($varValue); + $newValues[] = $varValue; } + restore_error_handler(); + + $newTypes = array_map(fn ($value): Type => $this->getTypeFromValue($value), $newValues); return TypeCombinator::union(...$newTypes); } elseif ($varType->isString()->yes()) { if ($varType->isLiteralString()->yes()) { diff --git a/tests/PHPStan/Analyser/nsrt/bug-13481.php b/tests/PHPStan/Analyser/nsrt/bug-13481.php new file mode 100644 index 0000000000..6af131f1e3 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-13481.php @@ -0,0 +1,23 @@ +