Skip to content

Commit

Permalink
Retain int specificity when $baseTimestamp is passed in as it appears…
Browse files Browse the repository at this point in the history
… unable to cause a false return
  • Loading branch information
Seldaek authored and ondrejmirtes committed Mar 3, 2022
1 parent 1cb8890 commit ca5e927
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Type/Php/StrtotimeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
Expand All @@ -31,7 +32,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
if (count($functionCall->getArgs()) !== 1) { // strtotime() & 2nd param baseTimestamp are both unsupported use cases
if (count($functionCall->getArgs()) === 0) {
return $defaultReturnType;
}
$argType = $scope->getType($functionCall->getArgs()[0]->value);
Expand All @@ -48,10 +49,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if ($results[0] === false) {
return new ConstantBooleanType(false);
}
// 2nd param $baseTimestamp is too non-deterministic so simply return int
if (count($functionCall->getArgs()) > 1) {
return new IntegerType();
}

$results = array_map('intval', $results);

return IntegerRangeType::createAllGreaterThan(min($results));
return IntegerRangeType::createAllGreaterThan((int) min($results));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
assertType('int|false', $strtotimeCrash);

$strtotimeWithBase = strtotime('+2 days', time());
assertType('int|false', $strtotimeWithBase);
assertType('int', $strtotimeWithBase);

$strtotimePositiveInt = strtotime('1990-01-01 12:00:00 UTC');
assertType('int<631195201, max>', $strtotimePositiveInt);
Expand Down

0 comments on commit ca5e927

Please sign in to comment.