Skip to content

Commit 466bed0

Browse files
committed
Fix "Crash: str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters"
1 parent e9556fb commit 466bed0

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
use PHPStan\Type\VoidType;
140140
use stdClass;
141141
use Throwable;
142+
use ValueError;
142143
use function abs;
143144
use function array_filter;
144145
use function array_key_exists;
@@ -1750,7 +1751,11 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
17501751
&& !is_numeric($varValue)
17511752
&& function_exists('str_increment')
17521753
) {
1753-
$varValue = str_increment($varValue);
1754+
try {
1755+
$varValue = str_increment($varValue);
1756+
} catch (ValueError) {
1757+
return new ErrorType();
1758+
}
17541759
} elseif (!is_bool($varValue)) {
17551760
++$varValue;
17561761
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php // lint < 8.3
2+
3+
namespace bug13481BeforePhp83;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function bug13481() {
8+
$s = 'ab c1';
9+
assertType("'ab c2'", str_increment($s));
10+
11+
++$s;
12+
assertType("'ab c2'", $s);
13+
}
14+
15+
function bug13481b() {
16+
$s = '%';
17+
assertType("'%'", str_increment($s));
18+
19+
++$s;
20+
assertType("'%'", $s);
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.3
2+
3+
namespace bug13481Php83;
4+
5+
use function PHPStan\Testing\assertType;
6+
use function str_increment;
7+
8+
function bug13481() {
9+
$s = 'ab c1';
10+
assertType("*ERROR*", str_increment($s));
11+
12+
++$s;
13+
assertType("*ERROR*", $s);
14+
}
15+
16+
function bug13481b() {
17+
$s = '%';
18+
assertType("*ERROR*", str_increment($s));
19+
20+
++$s;
21+
assertType("*ERROR*", $s);
22+
}
23+

0 commit comments

Comments
 (0)