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

Align Chronos::getTimezone() return type with DateTimeImmutable #423

Merged
merged 1 commit into from
Sep 24, 2023
Merged
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
18 changes: 9 additions & 9 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use ReturnTypeWillChange;
use RuntimeException;

/**
* An Immutable extension on the native DateTime object.
Expand Down Expand Up @@ -1009,12 +1009,16 @@
/**
* Return time zone set for this instance.
*
* @return \DateTimeZone|null
* @return \DateTimeZone
*/
#[ReturnTypeWillChange]
public function getTimezone(): ?DateTimeZone
public function getTimezone(): DateTimeZone
{
return parent::getTimezone() ?: null;
$tz = parent::getTimezone();
if ($tz === false) {
throw new RuntimeException('Time zone could not be retrieved.');

Check warning on line 1018 in src/Chronos.php

View check run for this annotation

Codecov / codecov/patch

src/Chronos.php#L1018

Added line #L1018 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible to trigger this and just reflects the internal timelib functions. Maybe if you serialized DateTimeImmutable with a time zone that was later removed? I'm not sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, I wouldn't worry about getting coverage on this case.

}

return $tz;
}

/**
Expand Down Expand Up @@ -2628,13 +2632,9 @@
return $this->offset === 0;

case $name === 'timezone' || $name === 'tz':
assert($this->getTimezone() !== null, 'Timezone is not set');

return $this->getTimezone();

case $name === 'timezoneName' || $name === 'tzName':
assert($this->getTimezone() !== null, 'Timezone is not set');

return $this->getTimezone()->getName();

default:
Expand Down