Skip to content

Commit

Permalink
Merge pull request #9043 from pilif/fix-9042
Browse files Browse the repository at this point in the history
fixes #9042
  • Loading branch information
weirdan committed Jan 3, 2023
2 parents c6cfa86 + bab1756 commit 7029e76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
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

0 comments on commit 7029e76

Please sign in to comment.