Skip to content

Commit 1910be3

Browse files
committed
ctype_digit() narrows to decimal-int-string
1 parent 2f2bd5c commit 1910be3

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/Type/Php/CtypeDigitFunctionTypeSpecifyingExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\DependencyInjection\AutowiredService;
1313
use PHPStan\Reflection\FunctionReflection;
1414
use PHPStan\ShouldNotHappenException;
15+
use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
1516
use PHPStan\Type\Accessory\AccessoryNumericStringType;
1617
use PHPStan\Type\Constant\ConstantBooleanType;
1718
use PHPStan\Type\FunctionTypeSpecifyingExtension;
@@ -56,7 +57,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
5657
if ($context->true()) {
5758
$types[] = new IntersectionType([
5859
new StringType(),
59-
new AccessoryNumericStringType(),
60+
new AccessoryDecimalIntegerStringType(),
6061
]);
6162
}
6263

@@ -68,7 +69,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
6869
IntegerRangeType::fromInterval(0, null),
6970
new IntersectionType([
7071
new StringType(),
71-
new AccessoryNumericStringType(),
72+
new AccessoryDecimalIntegerStringType(),
7273
]),
7374
new ConstantBooleanType(true),
7475
]);

tests/PHPStan/Analyser/nsrt/callsite-cast-narrowing.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class HelloWorld
1313
public function sayHello($mixed, int $int, string $string, $numericString, $nonEmptyString, bool $bool): void
1414
{
1515
if (ctype_digit((string) $mixed)) {
16-
assertType('int<0, max>|numeric-string|true', $mixed);
16+
assertType('int<0, max>|decimal-int-string|true', $mixed);
1717
} else {
1818
assertType('mixed~(int<0, max>|numeric-string|true)', $mixed);
1919
}
@@ -41,7 +41,7 @@ public function sayHello($mixed, int $int, string $string, $numericString, $nonE
4141
assertType('int', $int);
4242

4343
if (ctype_digit((string) $string)) {
44-
assertType('numeric-string', $string);
44+
assertType('decimal-int-string', $string);
4545
} else {
4646
assertType('string', $string);
4747
}
@@ -54,10 +54,11 @@ public function sayHello($mixed, int $int, string $string, $numericString, $nonE
5454
}
5555
assertType('string', $string);
5656

57+
// see https://3v4l.org/1Qrlg#veol
5758
if (ctype_digit((string) $numericString)) {
5859
assertType('numeric-string', $numericString);
5960
} else {
60-
assertType('*NEVER*', $numericString);
61+
assertType('numeric-string', $numericString);
6162
}
6263
assertType('numeric-string', $numericString);
6364

tests/PHPStan/Analyser/nsrt/ctype-digit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function foo(mixed $foo): void
1414
assertType('mixed', $foo);
1515

1616
if (is_string($foo) && ctype_digit($foo)) {
17-
assertType('numeric-string', $foo);
17+
assertType('decimal-int-string', $foo);
1818
} else {
1919
assertType('mixed', $foo);
2020
}
@@ -26,7 +26,7 @@ public function foo(mixed $foo): void
2626
}
2727

2828
if (ctype_digit($foo)) {
29-
assertType('int<48, 57>|int<256, max>|numeric-string', $foo);
29+
assertType('int<48, 57>|int<256, max>|decimal-int-string', $foo);
3030
return;
3131
}
3232

0 commit comments

Comments
 (0)