Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DateTime::modify() and DateTimeImmutable::modify() return types #9043

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,6 +8,7 @@
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;

/**
Expand Down Expand Up @@ -56,7 +57,11 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
return Type::getFalse();
}
if ($has_date_time && !$has_false) {
return Type::parseString($event->getFqClasslikeName());
return Type::intersectUnionTypes(
Type::parseString($event->getCalledFqClasslikeName() ?? $event->getFqClasslikeName()),
new Union([new TNamedObject('static')]),
$statements_source->getCodebase(),
);
}

return null;
Expand Down
18 changes: 16 additions & 2 deletions tests/DateTimeTest.php
Expand Up @@ -44,8 +44,8 @@ function getString(): string
$b = $dateTimeImmutable->modify(getString());
',
'assertions' => [
'$a' => 'DateTime',
'$b' => 'DateTimeImmutable',
'$a' => 'DateTime&static',
'$b' => 'DateTimeImmutable&static',
],
],
'modifyWithInvalidConstant' => [
Expand Down Expand Up @@ -88,6 +88,20 @@ function getString(): string
'$b' => 'DateTimeImmutable|false',
],
],
'modifyStaticReturn' => [
'code' => '<?php

class Subclass extends DateTimeImmutable
{
}

$foo = new Subclass("2023-01-01 12:12:13");
$mod = $foo->modify("+7 days");
',
'assertions' => [
'$mod' => 'Subclass&static',
],
],
];
}
}
2 changes: 1 addition & 1 deletion tests/MethodCallTest.php
Expand Up @@ -263,7 +263,7 @@ final class MyDate extends DateTimeImmutable {}
$b = (new DateTimeImmutable())->modify("+3 hours");',
'assertions' => [
'$yesterday' => 'MyDate',
'$b' => 'DateTimeImmutable',
'$b' => 'DateTimeImmutable&static',
],
],
'magicCall' => [
Expand Down