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

fix: TimeTrait::createFromTimestamp() method signature for PHP 8.4 #9134

Closed
Closed
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
12 changes: 0 additions & 12 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7555,12 +7555,6 @@
'count' => 1,
'path' => __DIR__ . '/system/I18n/Time.php',
];
$ignoreErrors[] = [
// identifier: method.childReturnType
'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\Time\\) of method CodeIgniter\\\\I18n\\\\Time\\:\\:setTimezone\\(\\) should be covariant with return type \\(static\\(DateTimeImmutable\\)\\) of method DateTimeImmutable\\:\\:setTimezone\\(\\)$#',
'count' => 1,
'path' => __DIR__ . '/system/I18n/Time.php',
];
$ignoreErrors[] = [
// identifier: ternary.shortNotAllowed
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
Expand All @@ -7579,12 +7573,6 @@
'count' => 1,
'path' => __DIR__ . '/system/I18n/TimeLegacy.php',
];
$ignoreErrors[] = [
// identifier: method.childReturnType
'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\TimeLegacy\\) of method CodeIgniter\\\\I18n\\\\TimeLegacy\\:\\:setTimezone\\(\\) should be covariant with return type \\(static\\(DateTime\\)\\) of method DateTime\\:\\:setTimezone\\(\\)$#',
'count' => 1,
'path' => __DIR__ . '/system/I18n/TimeLegacy.php',
];
$ignoreErrors[] = [
// identifier: ternary.shortNotAllowed
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
Expand Down
2 changes: 2 additions & 0 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* @property-read string $weekOfYear
* @property-read string $year
*
* @phpstan-consistent-constructor
*
* @see \CodeIgniter\I18n\TimeTest
*/
class Time extends DateTimeImmutable implements Stringable
Expand Down
2 changes: 2 additions & 0 deletions system/I18n/TimeLegacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* @property string $weekOfYear read-only
* @property string $year read-only
*
* @phpstan-consistent-constructor
*
* @deprecated Use Time instead.
* @see \CodeIgniter\I18n\TimeLegacyTest
*/
Expand Down
14 changes: 6 additions & 8 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,11 @@ public static function createFromFormat($format, $datetime, $timezone = null)
*
* @param DateTimeZone|string|null $timezone
*
* @return self
*
* @throws Exception
*/
public static function createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null)
public static function createFromTimestamp(float|int $timestamp, $timezone = null, ?string $locale = null): static
{
$time = new self(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale);
$time = new static(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale);

$timezone ??= date_default_timezone_get();

Expand All @@ -277,7 +275,7 @@ public static function createFromTimestamp(int $timestamp, $timezone = null, ?st
/**
* Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -286,7 +284,7 @@ public static function createFromInstance(DateTimeInterface $dateTime, ?string $
$date = $dateTime->format('Y-m-d H:i:s');
$timezone = $dateTime->getTimezone();

return new self($date, $timezone, $locale);
return new static($date, $timezone, $locale);
}

/**
Expand Down Expand Up @@ -674,7 +672,7 @@ protected function setValue(string $name, $value)
*
* @param DateTimeZone|string $timezone
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -683,7 +681,7 @@ public function setTimezone($timezone)
{
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);

return self::createFromInstance($this->toDateTime()->setTimezone($timezone), $this->locale);
return static::createFromInstance($this->toDateTime()->setTimezone($timezone), $this->locale);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions user_guide_src/source/changelogs/v4.5.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ Release Date: Unreleased
BREAKING
********

.. _v455-method-signature-changes:

Method Signature Changes
========================

- **Time:** For PHP 8.4 compatibility only, the first parameter type of the
``createFromTimestamp()`` method has been changed from ``int`` to ``int|float``,
and the return type ``static`` has been added. But **note that the**
``createFromTimestamp()`` **method still cannot handle a float value, and the
behavior is not yet changed**. That is, the behavior is not the exactly same as
PHP 8.4's ``DateTimeImmutable::createFromTimestamp()`` method.

***************
Message Changes
***************
Expand Down
7 changes: 7 additions & 0 deletions user_guide_src/source/installation/upgrade_455.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ Mandatory File Changes
Breaking Changes
****************

Method Signature Changes
========================

Some method signature changes have been made. Classes that extend them should
update their APIs to reflect the changes. See :ref:`ChangeLog <v455-method-signature-changes>`
for details.

*********************
Breaking Enhancements
*********************
Expand Down
Loading