Skip to content

Commit

Permalink
Merge pull request #9172 from DaDeather/fix-datetime-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Feb 20, 2023
2 parents 8e0fd88 + bec8ddf commit 589fee7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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 @@ -57,11 +56,7 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
return Type::getFalse();
}
if ($has_date_time && !$has_false) {
return Type::intersectUnionTypes(
Type::parseString($event->getCalledFqClasslikeName() ?? $event->getFqClasslikeName()),
new Union([new TNamedObject('static')]),
$statements_source->getCodebase(),
);
return Type::parseString($event->getCalledFqClasslikeName() ?? $event->getFqClasslikeName());
}

return null;
Expand Down
58 changes: 55 additions & 3 deletions tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function getString(): string
$b = $dateTimeImmutable->modify(getString());
',
'assertions' => [
'$a' => 'DateTime&static',
'$b' => 'DateTimeImmutable&static',
'$a' => 'DateTime',
'$b' => 'DateTimeImmutable',
],
],
'modifyWithInvalidConstant' => [
Expand Down Expand Up @@ -88,6 +88,18 @@ function getString(): string
'$b' => 'DateTimeImmutable|false',
],
],
'otherMethodAfterModify' => [
'code' => '<?php
$datetime = new DateTime();
$dateTimeImmutable = new DateTimeImmutable();
$a = $datetime->modify("+1 day")->setTime(0, 0);
$b = $dateTimeImmutable->modify("+1 day")->setTime(0, 0);
',
'assertions' => [
'$a' => 'DateTime|false',
'$b' => 'DateTimeImmutable',
],
],
'modifyStaticReturn' => [
'code' => '<?php
Expand All @@ -99,7 +111,47 @@ class Subclass extends DateTimeImmutable
$mod = $foo->modify("+7 days");
',
'assertions' => [
'$mod' => 'Subclass&static',
'$mod' => 'Subclass',
],
],
'otherMethodAfterModifyStaticReturn' => [
'code' => '<?php
class Subclass extends DateTimeImmutable
{
}
$datetime = new Subclass();
$mod = $datetime->modify("+1 day")->setTime(0, 0);
',
'assertions' => [
'$mod' => 'Subclass',
],
],
'formatAfterModify' => [
'code' => '<?php
$datetime = new DateTime();
$dateTimeImmutable = new DateTimeImmutable();
$a = $datetime->modify("+1 day")->format("Y-m-d");
$b = $dateTimeImmutable->modify("+1 day")->format("Y-m-d");
',
'assertions' => [
'$a' => 'false|string',
'$b' => 'string',
],
],
'formatAfterModifyStaticReturn' => [
'code' => '<?php
class Subclass extends DateTimeImmutable
{
}
$datetime = new Subclass();
$format = $datetime->modify("+1 day")->format("Y-m-d");
',
'assertions' => [
'$format' => 'string',
],
],
];
Expand Down
2 changes: 1 addition & 1 deletion tests/MethodCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ final class MyDate extends DateTimeImmutable {}
$b = (new DateTimeImmutable())->modify("+3 hours");',
'assertions' => [
'$yesterday' => 'MyDate',
'$b' => 'DateTimeImmutable&static',
'$b' => 'DateTimeImmutable',
],
],
'magicCall' => [
Expand Down

0 comments on commit 589fee7

Please sign in to comment.