Skip to content

Commit d2b04a7

Browse files
Fix
1 parent 904c51c commit d2b04a7

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/Type/Php/DateTimeModifyReturnTypeExtension.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Php\PhpVersion;
1010
use PHPStan\Reflection\MethodReflection;
11-
use PHPStan\Reflection\ParametersAcceptorSelector;
1211
use PHPStan\Type\Constant\ConstantBooleanType;
1312
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1413
use PHPStan\Type\NeverType;
1514
use PHPStan\Type\ObjectType;
1615
use PHPStan\Type\Type;
1716
use PHPStan\Type\TypeCombinator;
17+
use PHPStan\Type\TypeTraverser;
18+
use PHPStan\Type\UnionType;
1819
use Throwable;
1920
use function count;
2021

@@ -86,13 +87,19 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
8687
if ($dateTimeInterfaceType->isSuperTypeOf($callerType)->yes()) {
8788
return $callerType;
8889
}
89-
foreach ($callerType->getObjectClassNames() as $className) {
90-
if (!$dateTimeInterfaceType->isSuperTypeOf(new ObjectType($className))->yes()) {
91-
$callerType = TypeCombinator::remove($callerType, new ObjectType($className));
92-
}
93-
}
9490

95-
return $callerType;
91+
return TypeTraverser::map(
92+
$callerType,
93+
static function (Type $type, callable $traverse) use ($dateTimeInterfaceType): Type {
94+
if ($type instanceof UnionType) {
95+
return $traverse($type);
96+
}
97+
if ($dateTimeInterfaceType->isSuperTypeOf($type)->yes()) {
98+
return $type;
99+
}
100+
return new NeverType();
101+
}
102+
);
96103
}
97104

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

0 commit comments

Comments
 (0)