Skip to content

Commit

Permalink
Fixed assert(is_numeric($x)) false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 16, 2020
1 parent 88d2607 commit 53a5171
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -82,10 +81,6 @@ public function findSpecifiedType(
if (count(TypeUtils::getConstantScalars($argType)) > 0) {
return !$argType->toNumber() instanceof ErrorType;
}

if (!(new StringType())->isSuperTypeOf($argType)->no()) {
return null;
}
} elseif ($functionName === 'defined') {
return null;
} elseif (
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Php/IsNumericFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
throw new \PHPStan\ShouldNotHappenException();
}

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

$numericTypes = [
new IntegerType(),
new FloatType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ public function testImpossibleCheckTypeFunctionCall(): void
677,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Call to function is_numeric() with \'123\' will always evaluate to true.',
692,
],
[
'Call to function is_numeric() with \'blabla\' will always evaluate to false.',
693,
],
[
'Call to function assert() with true will always evaluate to true.',
700,
],
[
'Call to function is_numeric() with 123|float will always evaluate to true.',
700,
],
]
);
}
Expand Down Expand Up @@ -298,6 +314,10 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'unknown\' will always evaluate to false.',
648,
],
[
'Call to function is_numeric() with \'blabla\' will always evaluate to false.',
693,
],
]
);
}
Expand Down
19 changes: 19 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 @@ -682,3 +682,22 @@ public function doFoo($a): bool
}

}

class AssertIsNumeric
{

public function doFoo(string $str, float $float)
{
assert(is_numeric($str));
assert(is_numeric('123'));
assert(is_numeric('blabla'));

$isNumeric = $float;
if (doFoo()) {
$isNumeric = 123;
}

assert(is_numeric($isNumeric));
}

}

0 comments on commit 53a5171

Please sign in to comment.