Skip to content

Commit 0505980

Browse files
committed
More precise microtime() return type
1 parent b50ac58 commit 0505980

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Type/Php/MicrotimeFunctionReturnTypeExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
910
use PHPStan\Type\BenevolentUnionType;
1011
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1112
use PHPStan\Type\FloatType;
13+
use PHPStan\Type\IntersectionType;
1214
use PHPStan\Type\MixedType;
1315
use PHPStan\Type\StringType;
1416
use PHPStan\Type\Type;
@@ -26,8 +28,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
2628

2729
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
2830
{
31+
$stringType = new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]);
32+
2933
if (count($functionCall->getArgs()) < 1) {
30-
return new StringType();
34+
return $stringType;
3135
}
3236

3337
$argType = $scope->getType($functionCall->getArgs()[0]->value);
@@ -38,14 +42,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3842
return new FloatType();
3943
}
4044
if ($compareTypes === $isFalseType) {
41-
return new StringType();
45+
return $stringType;
4246
}
4347

4448
if ($argType instanceof MixedType) {
45-
return new BenevolentUnionType([new StringType(), new FloatType()]);
49+
return new BenevolentUnionType([$stringType, new FloatType()]);
4650
}
4751

48-
return new UnionType([new StringType(), new FloatType()]);
52+
return new UnionType([$stringType, new FloatType()]);
4953
}
5054

5155
}

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5193,23 +5193,23 @@ public static function dataFunctions(): array
51935193
{
51945194
return [
51955195
[
5196-
'string',
5196+
'non-falsy-string',
51975197
'$microtimeStringWithoutArg',
51985198
],
51995199
[
5200-
'string',
5200+
'non-falsy-string',
52015201
'$microtimeString',
52025202
],
52035203
[
52045204
'float',
52055205
'$microtimeFloat',
52065206
],
52075207
[
5208-
'float|string',
5208+
'float|non-falsy-string',
52095209
'$microtimeDefault',
52105210
],
52115211
[
5212-
'(float|string)',
5212+
'(float|non-falsy-string)',
52135213
'$microtimeBenevolent',
52145214
],
52155215
[

0 commit comments

Comments
 (0)