Skip to content

Commit

Permalink
Merge pull request #8312 from fluffycondor/dateperiod-stub
Browse files Browse the repository at this point in the history
Add stub for DatePeriod
  • Loading branch information
AndrolGenhald committed Jul 25, 2022
2 parents 640d3b6 + b1295d6 commit 0b482ac
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -65,8 +65,6 @@
use function array_merge;
use function array_search;
use function array_values;
use function assert;
use function class_exists;
use function count;
use function end;
use function in_array;
Expand Down
18 changes: 18 additions & 0 deletions stubs/CoreImmutableClasses.phpstub
Expand Up @@ -16,6 +16,24 @@ class DateTimeZone
public function __construct(string $timezone) {}
}

/**
* @psalm-immutable
*
* @template-covariant Start of string|DateTimeInterface
* @implements Traversable<int, DateTimeInterface>
*/
class DatePeriod implements Traversable
{
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-taint-specialize
*/
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 {}
}
71 changes: 71 additions & 0 deletions tests/CoreStubsTest.php
Expand Up @@ -33,5 +33,76 @@ public function providerValidCodeParse(): iterable
'error_levels' => [],
'php_version' => '8.0',
];
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(
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'
],
'error_levels' => [],
'php_version' => '8.0',
];
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'
],
'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 0b482ac

Please sign in to comment.