Skip to content

Commit 98ee715

Browse files
Seldaekondrejmirtes
authored andcommitted
Retain int specificity when $baseTimestamp is passed in as it appears unable to cause a false return
1 parent 83aa215 commit 98ee715

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/Type/Php/StrtotimeFunctionReturnTypeExtension.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\Constant\ConstantStringType;
1111
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1212
use PHPStan\Type\IntegerRangeType;
13+
use PHPStan\Type\IntegerType;
1314
use PHPStan\Type\MixedType;
1415
use PHPStan\Type\Type;
1516
use PHPStan\Type\TypeUtils;
@@ -31,7 +32,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3132
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
3233
{
3334
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
34-
if (count($functionCall->getArgs()) !== 1) { // strtotime() & 2nd param baseTimestamp are both unsupported use cases
35+
if (count($functionCall->getArgs()) === 0) {
3536
return $defaultReturnType;
3637
}
3738
$argType = $scope->getType($functionCall->getArgs()[0]->value);
@@ -49,9 +50,17 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4950
return new ConstantBooleanType(false);
5051
}
5152

52-
$results = array_map('intval', $results);
53+
// 2nd param $baseTimestamp is too non-deterministic so simply return int
54+
if (count($functionCall->getArgs()) > 1) {
55+
return new IntegerType();
56+
}
57+
58+
// if it is positive we can narrow down to positive-int as long as time flows forward
59+
if (min(array_map('intval', $results)) > 0) {
60+
return IntegerRangeType::createAllGreaterThan(0);
61+
}
5362

54-
return IntegerRangeType::createAllGreaterThan(min($results));
63+
return new IntegerType();
5564
}
5665

5766
}

tests/PHPStan/Analyser/data/strtotime-return-type-extensions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use function PHPStan\Testing\assertType;
66

7-
$strtotimeNow = strtotime('2022-03-03 12:00:00 UTC');
8-
assertType('int<1646308801, max>', $strtotimeNow);
7+
$strtotimeNow = strtotime('now');
8+
assertType('int<1, max>', $strtotimeNow);
99

1010
$strtotimeInvalid = strtotime('4 qm');
1111
assertType('false', $strtotimeInvalid);
@@ -20,10 +20,10 @@
2020
assertType('int|false', $strtotimeCrash);
2121

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

2525
$strtotimePositiveInt = strtotime('1990-01-01 12:00:00 UTC');
26-
assertType('int<631195201, max>', $strtotimePositiveInt);
26+
assertType('int<1, max>', $strtotimePositiveInt);
2727

2828
$strtotimeNegativeInt = strtotime('1969-12-31 12:00:00 UTC');
29-
assertType('int<-43199, max>', $strtotimeNegativeInt);
29+
assertType('int', $strtotimeNegativeInt);

0 commit comments

Comments
 (0)