Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Type/Php/CtypeDigitFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntegerRangeType;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
if ($context->true()) {
$types[] = new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
new AccessoryDecimalIntegerStringType(),
]);
}

Expand All @@ -68,7 +68,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
IntegerRangeType::fromInterval(0, null),
new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
new AccessoryDecimalIntegerStringType(),
]),
new ConstantBooleanType(true),
]);
Expand Down
9 changes: 5 additions & 4 deletions tests/PHPStan/Analyser/nsrt/callsite-cast-narrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HelloWorld
public function sayHello($mixed, int $int, string $string, $numericString, $nonEmptyString, bool $bool): void
{
if (ctype_digit((string) $mixed)) {
assertType('int<0, max>|numeric-string|true', $mixed);
assertType('int<0, max>|decimal-int-string|true', $mixed);
} else {
assertType('mixed~(int<0, max>|numeric-string|true)', $mixed);
}
Expand Down Expand Up @@ -41,7 +41,7 @@ public function sayHello($mixed, int $int, string $string, $numericString, $nonE
assertType('int', $int);

if (ctype_digit((string) $string)) {
assertType('numeric-string', $string);
assertType('decimal-int-string', $string);
} else {
assertType('string', $string);
}
Expand All @@ -54,10 +54,11 @@ public function sayHello($mixed, int $int, string $string, $numericString, $nonE
}
assertType('string', $string);

// see https://3v4l.org/1Qrlg#veol
if (ctype_digit((string) $numericString)) {
assertType('numeric-string', $numericString);
assertType('decimal-int-string', $numericString);
} else {
assertType('*NEVER*', $numericString);
Copy link
Copy Markdown
Contributor Author

@staabm staabm Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug before PR: numeric strings exist which ctype_digit does not return true, for see https://3v4l.org/1Qrlg#veol

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fix on 2.1 then ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure its worth fixing on 2.1.x because noone reported it yet and I think for a good fix we need decimal-int-string type which only exists on 2.2.x

assertType('numeric-string', $numericString);
}
assertType('numeric-string', $numericString);

Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/ctype-digit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function foo(mixed $foo): void
assertType('mixed', $foo);

if (is_string($foo) && ctype_digit($foo)) {
assertType('numeric-string', $foo);
assertType('decimal-int-string', $foo);
} else {
assertType('mixed', $foo);
}
Expand All @@ -26,7 +26,7 @@ public function foo(mixed $foo): void
}

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

Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5158,6 +5158,30 @@ public static function dataRemove(): array
UnionType::class,
'array<string>|ArrayObject',
],
[
new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]),
new IntersectionType([
new StringType(),
new AccessoryDecimalIntegerStringType(),
]),
IntersectionType::class,
'numeric-string'
],
[
new IntersectionType([
new StringType(),
new AccessoryDecimalIntegerStringType(),
]),
new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]),
NeverType::class,
'*NEVER*=implicit'
],
[
new ConstantBooleanType(true),
new ConstantBooleanType(false),
Expand Down
Loading