Skip to content

Commit 337fbee

Browse files
committed
Fix is_numeric issues
1 parent 19482b7 commit 337fbee

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Type\FloatType;
1313
use PHPStan\Type\FunctionTypeSpecifyingExtension;
1414
use PHPStan\Type\IntegerType;
15+
use PHPStan\Type\MixedType;
1516
use PHPStan\Type\StringType;
1617
use PHPStan\Type\UnionType;
1718

@@ -35,7 +36,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
3536
}
3637

3738
$argType = $scope->getType($node->args[0]->value);
38-
if ((new StringType())->isSuperTypeOf($argType)->yes()) {
39+
if ($context->truthy() && !(new StringType())->isSuperTypeOf($argType)->no() && !$argType instanceof MixedType) {
3940
return new SpecifiedTypes([], []);
4041
}
4142

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9815,6 +9815,11 @@ public function dataArrowFunctionReturnTypeInference(): array
98159815
return $this->gatherAssertTypes(__DIR__ . '/data/arrow-function-return-type.php');
98169816
}
98179817

9818+
public function dataIsNumeric(): array
9819+
{
9820+
return $this->gatherAssertTypes(__DIR__ . '/data/is-numeric.php');
9821+
}
9822+
98189823
/**
98199824
* @dataProvider dataBug2574
98209825
* @dataProvider dataBug2577
@@ -9849,6 +9854,7 @@ public function dataArrowFunctionReturnTypeInference(): array
98499854
* @dataProvider dataIteratorToArray
98509855
* @dataProvider dataExtDs
98519856
* @dataProvider dataArrowFunctionReturnTypeInference
9857+
* @dataProvider dataIsNumeric
98529858
* @param ConstantStringType $expectedType
98539859
* @param Type $actualType
98549860
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
function () {
4+
/** @var int|string $s */
5+
$s = doFoo();
6+
if (!is_numeric($s)) {
7+
\PHPStan\Analyser\assertType('string', $s);
8+
}
9+
};

tests/PHPStan/Rules/Comparison/data/check-type-function-call.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,59 @@ public function doFoo(string $str, float $float)
700700
assert(is_numeric($isNumeric));
701701
}
702702

703+
/**
704+
* @param int|string $item
705+
*/
706+
public function doBar($item): void
707+
{
708+
if (!is_numeric($item)) {
709+
throw new \Exception;
710+
}
711+
712+
echo $item;
713+
}
714+
715+
/**
716+
* @param string|float|int $value
717+
*/
718+
public function doBaz($value): void
719+
{
720+
if (is_numeric($value)) {
721+
722+
}
723+
}
724+
725+
public function doLorem(string $rating): ?int
726+
{
727+
$ratingMapping = [
728+
'UR' => 0,
729+
'NR' => 0,
730+
'G' => 0,
731+
'PG' => 8,
732+
'PG-13' => 13,
733+
'R' => 15,
734+
'NC-17' => 17,
735+
];
736+
737+
if (array_key_exists($rating, $ratingMapping)) {
738+
$rating = $ratingMapping[$rating];
739+
}
740+
741+
if (is_numeric($rating)) {
742+
743+
}
744+
}
745+
746+
/**
747+
* @param mixed[] $data
748+
*/
749+
function doIpsum(array $data): void
750+
{
751+
foreach ($data as $index => $element) {
752+
if (is_numeric($index)) {
753+
echo "numeric key\n";
754+
}
755+
}
756+
}
757+
703758
}

0 commit comments

Comments
 (0)