From fa11a4ff42626cbf549afd098511fbb8f228a7d2 Mon Sep 17 00:00:00 2001 From: Semyon <7ionmail@gmail.com> Date: Fri, 22 Jul 2022 16:03:45 +0300 Subject: [PATCH] Add stub for DatePeriod --- stubs/CoreImmutableClasses.phpstub | 21 +++++++++++++++++++++ tests/CoreStubsTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/stubs/CoreImmutableClasses.phpstub b/stubs/CoreImmutableClasses.phpstub index cccb6814d79..60b807abbfe 100644 --- a/stubs/CoreImmutableClasses.phpstub +++ b/stubs/CoreImmutableClasses.phpstub @@ -16,6 +16,27 @@ class DateTimeZone public function __construct(string $timezone) {} } +/** + * @psalm-immutable + * + * @template-covariant From of string|DateTimeInterface + * @implements IteratorAggregate + */ +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&Iterator) : (Traversable&Iterator)) */ + public function getIterator(): Iterator {} +} + /** * @psalm-taint-specialize */ diff --git a/tests/CoreStubsTest.php b/tests/CoreStubsTest.php index 8bbe6435522..38a38672c40 100644 --- a/tests/CoreStubsTest.php +++ b/tests/CoreStubsTest.php @@ -33,5 +33,35 @@ public function providerValidCodeParse(): iterable 'error_levels' => [], 'php_version' => '8.0', ]; + yield 'Iterating over \DatePeriod (#5954)' => [ + 'format("Y-m-d"); + }', + 'assertions' => [ + '$period' => 'DatePeriod', + '$dt' => 'DateTimeImmutable|null' + ], + ]; + yield 'Iterating over \DatePeriod (#5954), ISO string' => [ + 'format("Y-m-d"); + }', + 'assertions' => [ + '$period' => 'DatePeriod', + '$dt' => 'DateTime|null' + ], + ]; } }