Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions lib/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public static function createFromFormat($format, $time, $timezone = null): self
*/
public function diff($datetime2, $absolute = false): DateInterval
{
// in PHP <8.3 this returns false, 8.3 onwards throw an exception,
// phpstan is can't seem to comprehend both at once?
/** @var \DateInterval|false $result */
$result = parent::diff($datetime2, $absolute);
$result = parent::diff($datetime2, $absolute); // @phpstan-ignore-line
if ($result === false) {
throw DatetimeException::createFromPhpError();
}
Expand All @@ -52,27 +54,13 @@ public function diff($datetime2, $absolute = false): DateInterval
* @return DateTime Returns the DateTime object for method chaining.
* @throws DatetimeException
*/
#[\ReturnTypeWillChange]
public function modify($modify): self
{
// in PHP <8.3 this returns false, 8.3 onwards throw an exception,
// phpstan is can't seem to comprehend both at once?
/** @var DateTime|false $result */
$result = parent::modify($modify);
if ($result === false) {
throw DatetimeException::createFromPhpError();
}
return $result;
}

/**
* @param int $year
* @param int $month
* @param int $day
* @return DateTime
* @throws DatetimeException
*/
public function setDate($year, $month, $day): self
{
/** @var DateTime|false $result */
$result = parent::setDate($year, $month, $day);
$result = parent::modify($modify); // @phpstan-ignore-line
if ($result === false) {
throw DatetimeException::createFromPhpError();
}
Expand Down
21 changes: 9 additions & 12 deletions tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ public function testCreateFromFormatPreserveTimeAndTimezone(): void
$this->assertEquals($timezone, $datetime->getTimezone());
}

public function testSetDate(): void
{
$datetime = new \Safe\DateTime();
$datetime = $datetime->setDate(2017, 4, 6);
$this->assertInstanceOf(DateTime::class, $datetime);
$this->assertEquals(2017, $datetime->format('Y'));
$this->assertEquals(4, $datetime->format('n'));
$this->assertEquals(6, $datetime->format('j'));

//todo: test an error case
}

public function testModify(): void
{
$datetime = new \Safe\DateTime();
Expand All @@ -44,4 +32,13 @@ public function testModify(): void
$this->assertInstanceOf(DateTime::class, $datetime);
$this->assertEquals('7-4-2017', $datetime->format('j-n-Y'));
}

public function testModifyThrowsExceptionOnInvalidInput(): void
{
// In php 8.1/8.2 this is \Safe\DatetimeException
// In php 8.3+ this is \DateMalformedStringException
// TODO: find some way to get phpstan to check the above
$this->expectException(\Throwable::class);
(new \Safe\DateTime())->modify('invalid date');
}
}