1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Add stub for DatePeriod

This commit is contained in:
Semyon 2022-07-22 16:03:45 +03:00
parent 640d3b646c
commit 9d3253482d
2 changed files with 51 additions and 0 deletions

View File

@ -16,6 +16,27 @@ class DateTimeZone
public function __construct(string $timezone) {}
}
/**
* @psalm-immutable
*
* @template-covariant From of string|DateTimeInterface
* @implements IteratorAggregate<int, DateTimeInterface>
*/
class DatePeriod implements IteratorAggregate
{
const EXCLUDE_START_DATE = 1;
/**
* @param From $from
* @param (From is string ? 0|self::EXCLUDE_START_DATE : DateInterval) $interval_or_options
* @param (From is string ? never : DateTimeInterface|positive-int) $end_or_recurrences
* @param (From is string ? never : 0|self::EXCLUDE_START_DATE) $options
*/
public function __construct($from, $interval_or_options = 0, $end_or_recurrences = 1, $options = 0) {}
/** @psalm-return (From is string ? (Traversable<int, DateTime>&Iterator) : (Traversable<int, From>&Iterator)) */
public function getIterator(): Iterator {}
}
/**
* @psalm-taint-specialize
*/

View File

@ -33,5 +33,35 @@ class CoreStubsTest extends TestCase
'error_levels' => [],
'php_version' => '8.0',
];
yield 'Iterating over \DatePeriod (#5954)' => [
'<?php
$period = new DatePeriod(
new DateTimeImmutable("now"),
DateInterval::createFromDateString("1 day"),
new DateTime("+1 week")
);
$dt = null;
foreach ($period as $dt) {
echo $dt->format("Y-m-d");
}',
'assertions' => [
'$period' => 'DatePeriod<DateTimeImmutable>',
'$dt' => 'DateTimeImmutable|null'
],
];
yield 'Iterating over \DatePeriod (#5954), ISO string' => [
'<?php
$period = new DatePeriod("R4/2012-07-01T00:00:00Z/P7D");
$dt = null;
foreach ($period as $dt) {
echo $dt->format("Y-m-d");
}',
'assertions' => [
'$period' => 'DatePeriod<string>',
'$dt' => 'DateTime|null'
],
];
}
}