1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-23 06:11:25 +01:00

Merge pull request #8312 from fluffycondor/dateperiod-stub

Add stub for DatePeriod
This commit is contained in:
AndrolGenhald 2022-07-25 09:27:40 -05:00 committed by GitHub
commit 0b482ac020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 2 deletions

View File

@ -65,8 +65,6 @@ use function array_keys;
use function array_merge; use function array_merge;
use function array_search; use function array_search;
use function array_values; use function array_values;
use function assert;
use function class_exists;
use function count; use function count;
use function end; use function end;
use function in_array; use function in_array;

View File

@ -16,6 +16,24 @@ class DateTimeZone
public function __construct(string $timezone) {} 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 * @psalm-taint-specialize
*/ */

View File

@ -82,3 +82,24 @@ class ReflectionUnionType extends ReflectionType {
} }
class UnhandledMatchError extends Error {} 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 {}
}

View File

@ -33,5 +33,76 @@ class CoreStubsTest extends TestCase
'error_levels' => [], 'error_levels' => [],
'php_version' => '8.0', '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',
];
} }
} }