Skip to content

Commit c6ac861

Browse files
Fix
1 parent fe58d31 commit c6ac861

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/Type/Php/DateTimeModifyReturnTypeExtension.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Type\Constant\ConstantBooleanType;
1313
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1414
use PHPStan\Type\NeverType;
15+
use PHPStan\Type\ObjectType;
1516
use PHPStan\Type\Type;
1617
use PHPStan\Type\TypeCombinator;
1718
use Throwable;
@@ -41,7 +42,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4142
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4243
{
4344
$args = $methodCall->getArgs();
44-
if (count($methodCall->getArgs()) < 1) {
45+
if (count($args) < 1) {
4546
return null;
4647
}
4748

@@ -79,9 +80,19 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7980

8081
return null;
8182
} elseif ($hasDateTime) {
82-
$variant = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants());
83+
$callerType = $scope->getType($methodCall->var);
8384

84-
return TypeCombinator::remove($variant->getReturnType(), new ConstantBooleanType(false));
85+
$dateTimeInterfaceType = new ObjectType(DateTimeInterface::class);
86+
if ($dateTimeInterfaceType->isSuperTypeOf($callerType)->yes()) {
87+
return $callerType;
88+
}
89+
foreach ($callerType->getObjectClassNames() as $className) {
90+
if (!$dateTimeInterfaceType->isSuperTypeOf(new ObjectType($className))->yes()) {
91+
$callerType = TypeCombinator::remove($callerType, new ObjectType($className));
92+
}
93+
}
94+
95+
return $callerType;
8596
}
8697

8798
if ($this->phpVersion->hasDateTimeExceptions()) {

0 commit comments

Comments
 (0)