Skip to content

Commit c8807bc

Browse files
pepakrizondrejmirtes
authored andcommitted
Function is_numeric leads to string|int|float type
1 parent d6c9c27 commit c8807bc

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ public function specifyTypesInCondition(Scope $scope, Expr $expr, bool $negated
133133
return $this->create($expr, new IterableIterableType(new MixedType()), $negated);
134134
case 'is_string':
135135
return $this->create($expr, new StringType(), $negated);
136+
case 'is_numeric':
137+
return $this->create($expr, new UnionType([
138+
new StringType(),
139+
new IntegerType(),
140+
new FloatType(),
141+
]), $negated);
136142
}
137143
} elseif ($expr instanceof BooleanAnd) {
138144
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $negated);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,10 @@ public function dataSpecifiedTypesUsingIsFunctions(): array
27272727
'string',
27282728
'$string',
27292729
],
2730+
[
2731+
'int',
2732+
'$intOrStdClass',
2733+
],
27302734
];
27312735
}
27322736

tests/PHPStan/Analyser/TypeSpecifierTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public function dataCondition(): array
5959
['$foo' => 'int'],
6060
['$foo' => '~int'],
6161
],
62-
62+
[
63+
$this->createFunctionCall('is_numeric'),
64+
['$foo' => 'float|int|string'],
65+
['$foo' => '~float|int|string'],
66+
],
6367
[
6468
new Expr\BinaryOp\BooleanAnd(
6569
$this->createFunctionCall('is_int'),

tests/PHPStan/Analyser/data/specifiedTypesUsingIsFunctions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
return;
5353
}
5454

55+
/** @var int|\stdClass $intOrStdClass */
56+
$intOrStdClass = doFoo();
57+
if (!is_numeric($intOrStdClass)) {
58+
return;
59+
}
60+
5561
assert(is_int($yetAnotherInteger));
5662

5763
die;

0 commit comments

Comments
 (0)