Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Type/Php/MicrotimeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -26,8 +28,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$stringType = new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]);

if (count($functionCall->getArgs()) < 1) {
return new StringType();
return $stringType;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
Expand All @@ -38,14 +42,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new FloatType();
}
if ($compareTypes === $isFalseType) {
return new StringType();
return $stringType;
}

if ($argType instanceof MixedType) {
return new BenevolentUnionType([new StringType(), new FloatType()]);
return new BenevolentUnionType([$stringType, new FloatType()]);
}

return new UnionType([new StringType(), new FloatType()]);
return new UnionType([$stringType, new FloatType()]);
}

}
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5193,23 +5193,23 @@ public static function dataFunctions(): array
{
return [
[
'string',
'non-falsy-string',
'$microtimeStringWithoutArg',
],
[
'string',
'non-falsy-string',
'$microtimeString',
],
[
'float',
'$microtimeFloat',
],
[
'float|string',
'float|non-falsy-string',
'$microtimeDefault',
],
[
'(float|string)',
'(float|non-falsy-string)',
'$microtimeBenevolent',
],
[
Expand Down
Loading