Skip to content
Merged
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
15 changes: 10 additions & 5 deletions src/Type/Php/CtypeDigitFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
Expand Down Expand Up @@ -56,20 +57,24 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
if ($context->true()) {
$types[] = new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
new AccessoryDecimalIntegerStringType(),
]);
}

$unionType = TypeCombinator::union(...$types);
$specifiedTypes = $this->typeSpecifier->create($exprArg, $unionType, $context, $scope);

if ($exprArg instanceof Cast\String_) {
$accessories = [
new StringType(),
new AccessoryNumericStringType(),
];
if ($context->true()) {
$accessories[] = new AccessoryDecimalIntegerStringType();
}
$castedType = new UnionType([
IntegerRangeType::fromInterval(0, null),
new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]),
new IntersectionType($accessories),
new ConstantBooleanType(true),
]);
$specifiedTypes = $specifiedTypes->unionWith(
Expand Down
12 changes: 10 additions & 2 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,6 +54,7 @@ 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);
} else {
Expand All @@ -67,6 +68,13 @@ public function sayHello($mixed, int $int, string $string, $numericString, $nonE
assertType('false', $bool);
}
assertType('bool', $bool);

if (ctype_digit((string) $bool) === true) {
assertType('true', $bool);
} else {
assertType('false', $bool);
}
assertType('bool', $bool);
}

}
14 changes: 12 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,10 +26,20 @@ 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;
}

assertType('mixed~(int<48, 57>|int<256, max>)', $foo); // not all numeric strings are covered by ctype_digit
}

public function doString(string $string): void
{
if (ctype_digit($string) === true) {
assertType('decimal-int-string', $string);
} else {
assertType('string', $string);
}

}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5713,6 +5713,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