Skip to content

Commit

Permalink
Test DatePeriod constructor with named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 13, 2022
1 parent 4dc86c4 commit 5945367
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,47 @@ public function testBug6370(): void
]);
}

public function testBug5553(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-5553.php'], []);
}

public function testBug7048(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-7048.php'], [
[
'Unknown parameter $recurrences in call to DatePeriod constructor.',
21,
],
[
'Missing parameter $end (DateTimeInterface|int) in call to DatePeriod constructor.',
18,
],
[
'Unknown parameter $isostr in call to DatePeriod constructor.',
25,
],
[
'Missing parameter $start (string) in call to DatePeriod constructor.',
24,
],
[
'Parameter #3 $end of class DatePeriod constructor expects DateTimeInterface|int, string given.',
41,
],
[
'Parameter $end of class DatePeriod constructor expects DateTimeInterface|int, string given.',
49,
],
]);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-5553.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // lint >= 8.0

namespace Bug5553;

use DatePeriod;
use DateTime;

class Foo
{

public function weekNumberToWorkRange(int $week, int $year): DatePeriod
{
$dto = new DateTime();
$dto->setISODate($year, $week);
$dto->setTime(8, 0, 0, 0);

$start = clone $dto;
$dto->modify('+4 days');
$end = clone $dto;
$end->setTime(18, 0, 0, 0);

return new DatePeriod(
start: $start,
interval: new \DateInterval('P1D'),
end: $end
);
}

}
52 changes: 52 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-7048.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug7048;

class HelloWorld
{
public function sayHello(): \DatePeriod
{
return new \DatePeriod(
start: new \DateTime('now'),
interval: new \DateInterval('P1D'),
end: new \DateTime('now + 3 days'),
);
}

public function doFoo(): void
{
new \DatePeriod(
start: new \DateTime('now'),
interval: new \DateInterval('P1D'),
recurrences: 5,
);

new \DatePeriod(
isostr: 'R4/2012-07-01T00:00:00Z/P7D'
);
}

public function allValid(): void
{
$start = new \DateTime('2012-07-01');
$interval = new \DateInterval('P7D');
$end = new \DateTime('2012-07-31');
$recurrences = 4;
$iso = 'R4/2012-07-01T00:00:00Z/P7D';


$period = new \DatePeriod($start, $interval, $recurrences);
$period = new \DatePeriod($start, $interval, $end);
$period = new \DatePeriod($iso);
$period = new \DatePeriod($start, $interval, "foo");
}

public function invalid(): void
{
new \DatePeriod(
start: new \DateTime('now'),
interval: new \DateInterval('P1D'),
end: "foo",
);
}
}

0 comments on commit 5945367

Please sign in to comment.