Skip to content

Commit

Permalink
Fix is_numeric issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 17, 2020
1 parent 19482b7 commit 337fbee
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\FloatType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;

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

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

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9815,6 +9815,11 @@ public function dataArrowFunctionReturnTypeInference(): array
return $this->gatherAssertTypes(__DIR__ . '/data/arrow-function-return-type.php');
}

public function dataIsNumeric(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/is-numeric.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -9849,6 +9854,7 @@ public function dataArrowFunctionReturnTypeInference(): array
* @dataProvider dataIteratorToArray
* @dataProvider dataExtDs
* @dataProvider dataArrowFunctionReturnTypeInference
* @dataProvider dataIsNumeric
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/is-numeric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

function () {
/** @var int|string $s */
$s = doFoo();
if (!is_numeric($s)) {
\PHPStan\Analyser\assertType('string', $s);
}
};
55 changes: 55 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/check-type-function-call.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,59 @@ public function doFoo(string $str, float $float)
assert(is_numeric($isNumeric));
}

/**
* @param int|string $item
*/
public function doBar($item): void
{
if (!is_numeric($item)) {
throw new \Exception;
}

echo $item;
}

/**
* @param string|float|int $value
*/
public function doBaz($value): void
{
if (is_numeric($value)) {

}
}

public function doLorem(string $rating): ?int
{
$ratingMapping = [
'UR' => 0,
'NR' => 0,
'G' => 0,
'PG' => 8,
'PG-13' => 13,
'R' => 15,
'NC-17' => 17,
];

if (array_key_exists($rating, $ratingMapping)) {
$rating = $ratingMapping[$rating];
}

if (is_numeric($rating)) {

}
}

/**
* @param mixed[] $data
*/
function doIpsum(array $data): void
{
foreach ($data as $index => $element) {
if (is_numeric($index)) {
echo "numeric key\n";
}
}
}

}

0 comments on commit 337fbee

Please sign in to comment.