Skip to content

Commit

Permalink
Make DatePeriod implement Traversable oh PHP 7, rename constructor pa…
Browse files Browse the repository at this point in the history
…rams
  • Loading branch information
fluffycondor committed Jul 25, 2022
1 parent 9d32534 commit 462ce71
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
19 changes: 8 additions & 11 deletions stubs/CoreImmutableClasses.phpstub
Expand Up @@ -19,22 +19,19 @@ class DateTimeZone
/**
* @psalm-immutable
*
* @template-covariant From of string|DateTimeInterface
* @implements IteratorAggregate<int, DateTimeInterface>
* @template-covariant Start of string|DateTimeInterface
* @implements Traversable<int, DateTimeInterface>
*/
class DatePeriod implements IteratorAggregate
class DatePeriod implements Traversable
{
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
* @param Start $start
* @param (Start is string ? 0|self::EXCLUDE_START_DATE : DateInterval) $interval
* @param (Start is string ? never : DateTimeInterface|positive-int) $end
* @param (Start 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 {}
public function __construct($start, $interval = 0, $end = 1, $options = 0) {}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions stubs/Php80.phpstub
Expand Up @@ -82,3 +82,24 @@ class ReflectionUnionType extends ReflectionType {
}

class UnhandledMatchError extends Error {}

/**
* @psalm-immutable
*
* @template-covariant Start of string|DateTimeInterface
* @implements IteratorAggregate<int, DateTimeInterface>
*/
class DatePeriod implements IteratorAggregate
{
const EXCLUDE_START_DATE = 1;
/**
* @param Start $start
* @param (Start is string ? 0|self::EXCLUDE_START_DATE : DateInterval) $interval
* @param (Start is string ? never : DateTimeInterface|positive-int) $end
* @param (Start is string ? never : 0|self::EXCLUDE_START_DATE) $options
*/
public function __construct($start, $interval = 0, $end = 1, $options = 0) {}

/** @psalm-return (Start is string ? (Traversable<int, DateTime>&Iterator) : (Traversable<int, Start>&Iterator)) */
public function getIterator(): Iterator {}
}
43 changes: 42 additions & 1 deletion tests/CoreStubsTest.php
Expand Up @@ -33,7 +33,26 @@ public function providerValidCodeParse(): iterable
'error_levels' => [],
'php_version' => '8.0',
];
yield 'Iterating over \DatePeriod (#5954)' => [
yield 'Iterating over \DatePeriod (#5954) PHP7 Traversable' => [
'<?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' => 'DateTimeInterface|null'
],
'error_levels' => [],
'php_version' => '7.3',
];
yield 'Iterating over \DatePeriod (#5954) PHP8 IteratorAggregate' => [
'<?php
$period = new DatePeriod(
Expand All @@ -49,6 +68,8 @@ public function providerValidCodeParse(): iterable
'$period' => 'DatePeriod<DateTimeImmutable>',
'$dt' => 'DateTimeImmutable|null'
],
'error_levels' => [],
'php_version' => '8.0',
];
yield 'Iterating over \DatePeriod (#5954), ISO string' => [
'<?php
Expand All @@ -62,6 +83,26 @@ public function providerValidCodeParse(): iterable
'$period' => 'DatePeriod<string>',
'$dt' => 'DateTime|null'
],
'error_levels' => [],
'php_version' => '8.0',
];
yield 'DatePeriod implements only Traversable on PHP 7' => [
'<?php
$period = new DatePeriod("R4/2012-07-01T00:00:00Z/P7D");
if ($period instanceof IteratorAggregate) {}',
'assertions' => [],
'error_levels' => [],
'php_version' => '7.3',
];
yield 'DatePeriod implements IteratorAggregate on PHP 8' => [
'<?php
$period = new DatePeriod("R4/2012-07-01T00:00:00Z/P7D");
if ($period instanceof IteratorAggregate) {}',
'assertions' => [],
'error_levels' => ['RedundantCondition'],
'php_version' => '8.0',
];
}
}

0 comments on commit 462ce71

Please sign in to comment.