Skip to content

Commit

Permalink
Add stub for DatePeriod
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffycondor committed Jul 22, 2022
1 parent 510456c commit fa11a4f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions stubs/CoreImmutableClasses.phpstub
Expand Up @@ -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
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/CoreStubsTest.php
Expand Up @@ -33,5 +33,35 @@ public function providerValidCodeParse(): iterable
'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'
],
];
}
}

0 comments on commit fa11a4f

Please sign in to comment.