1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

fix (DateTime|DateTimeImmutable)::modify() return types

vimeo#9042 caused issues to any called method on an instance
of DateTime|DateTimeImmutable after calling the modify
method.

This fixes vimeo#9171
This commit is contained in:
Ismail Turan 2023-01-24 11:32:09 +01:00 committed by Ismail Özgün Turan
parent 8e0fd88014
commit bec8ddf525
3 changed files with 57 additions and 10 deletions

View File

@ -8,7 +8,6 @@ use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface; use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type; use Psalm\Type;
use Psalm\Type\Atomic\TLiteralString; use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union; use Psalm\Type\Union;
/** /**
@ -57,11 +56,7 @@ class DateTimeModifyReturnTypeProvider implements MethodReturnTypeProviderInterf
return Type::getFalse(); return Type::getFalse();
} }
if ($has_date_time && !$has_false) { if ($has_date_time && !$has_false) {
return Type::intersectUnionTypes( return Type::parseString($event->getCalledFqClasslikeName() ?? $event->getFqClasslikeName());
Type::parseString($event->getCalledFqClasslikeName() ?? $event->getFqClasslikeName()),
new Union([new TNamedObject('static')]),
$statements_source->getCodebase(),
);
} }
return null; return null;

View File

@ -44,8 +44,8 @@ class DateTimeTest extends TestCase
$b = $dateTimeImmutable->modify(getString()); $b = $dateTimeImmutable->modify(getString());
', ',
'assertions' => [ 'assertions' => [
'$a' => 'DateTime&static', '$a' => 'DateTime',
'$b' => 'DateTimeImmutable&static', '$b' => 'DateTimeImmutable',
], ],
], ],
'modifyWithInvalidConstant' => [ 'modifyWithInvalidConstant' => [
@ -88,6 +88,18 @@ class DateTimeTest extends TestCase
'$b' => 'DateTimeImmutable|false', '$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' => [ 'modifyStaticReturn' => [
'code' => '<?php 'code' => '<?php
@ -99,7 +111,47 @@ class DateTimeTest extends TestCase
$mod = $foo->modify("+7 days"); $mod = $foo->modify("+7 days");
', ',
'assertions' => [ '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',
], ],
], ],
]; ];

View File

@ -263,7 +263,7 @@ class MethodCallTest extends TestCase
$b = (new DateTimeImmutable())->modify("+3 hours");', $b = (new DateTimeImmutable())->modify("+3 hours");',
'assertions' => [ 'assertions' => [
'$yesterday' => 'MyDate', '$yesterday' => 'MyDate',
'$b' => 'DateTimeImmutable&static', '$b' => 'DateTimeImmutable',
], ],
], ],
'magicCall' => [ 'magicCall' => [