1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #6181 from Jack97/date-time-interface-type-reconciliation

This commit is contained in:
Bruce Weirdan 2021-07-26 16:28:10 +03:00 committed by GitHub
commit 358f83d66b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View File

@ -208,6 +208,21 @@ class NegatedAssertionReconciler extends Reconciler
return $existing_var_type;
}
if (!$is_equality
&& ($assertion === 'DateTime' || $assertion === 'DateTimeImmutable')
&& isset($existing_var_atomic_types['DateTimeInterface'])
) {
$existing_var_type->removeType('DateTimeInterface');
if ($assertion === 'DateTime') {
$existing_var_type->addType(new TNamedObject('DateTimeImmutable'));
} else {
$existing_var_type->addType(new TNamedObject('DateTime'));
}
return $existing_var_type;
}
if (strtolower($assertion) === 'traversable'
&& isset($existing_var_atomic_types['iterable'])
) {

View File

@ -110,6 +110,8 @@ class ReconcilerTest extends \Psalm\Tests\TestCase
'notSomeClassWithSomeClassPipeBool' => ['bool', '!SomeClass', 'SomeClass|bool'],
'notSomeClassWithSomeClassPipeNull' => ['null', '!SomeClass', 'SomeClass|null'],
'notSomeClassWithAPipeB' => ['B', '!A', 'A|B'],
'notDateTimeWithDateTimeInterface' => ['DateTimeImmutable', '!DateTime', 'DateTimeInterface'],
'notDateTimeImmutableWithDateTimeInterface' => ['DateTime', '!DateTimeImmutable', 'DateTimeInterface'],
'myObjectWithSomeClassPipeBool' => ['SomeClass', 'SomeClass', 'SomeClass|bool'],
'myObjectWithAPipeB' => ['A', 'A', 'A|B'],

View File

@ -1073,6 +1073,36 @@ class TypeTest extends \Psalm\Tests\TestCase
*/
function consume($input): void{}'
],
'notDateTimeWithDateTimeInterface' => [
'<?php
function foo(DateTimeInterface $dateTime): DateTimeInterface {
$dateInterval = new DateInterval("P1D");
if ($dateTime instanceof DateTime) {
$dateTime->add($dateInterval);
return $dateTime;
} else {
return $dateTime->add($dateInterval);
}
}
',
],
'notDateTimeImmutableWithDateTimeInterface' => [
'<?php
function foo(DateTimeInterface $dateTime): DateTimeInterface {
$dateInterval = new DateInterval("P1D");
if ($dateTime instanceof DateTimeImmutable) {
return $dateTime->add($dateInterval);
} else {
$dateTime->add($dateInterval);
return $dateTime;
}
}
',
],
];
}