From 337fbeebe070513215fd2fef6c2ea536413c4255 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 17 Mar 2020 13:12:03 +0100 Subject: [PATCH] Fix is_numeric issues --- ...NumericFunctionTypeSpecifyingExtension.php | 3 +- .../Analyser/NodeScopeResolverTest.php | 6 ++ tests/PHPStan/Analyser/data/is-numeric.php | 9 +++ .../data/check-type-function-call.php | 55 +++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Analyser/data/is-numeric.php diff --git a/src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php b/src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php index 0c4a0f177a..0b200caffe 100644 --- a/src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php +++ b/src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php @@ -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; @@ -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([], []); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 5efa42bbb4..54c283703f 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -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 @@ -9849,6 +9854,7 @@ public function dataArrowFunctionReturnTypeInference(): array * @dataProvider dataIteratorToArray * @dataProvider dataExtDs * @dataProvider dataArrowFunctionReturnTypeInference + * @dataProvider dataIsNumeric * @param ConstantStringType $expectedType * @param Type $actualType */ diff --git a/tests/PHPStan/Analyser/data/is-numeric.php b/tests/PHPStan/Analyser/data/is-numeric.php new file mode 100644 index 0000000000..d5d84d2d37 --- /dev/null +++ b/tests/PHPStan/Analyser/data/is-numeric.php @@ -0,0 +1,9 @@ + 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"; + } + } + } + }