Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stub for DatePeriod #8312

Merged
merged 3 commits into from Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
];
}
}