diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 56c213803a8b..7fa8438f2b31 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -7495,18 +7495,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/I18n/Time.php', ]; -$ignoreErrors[] = [ - // identifier: method.childReturnType - 'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\Time\\) of method CodeIgniter\\\\I18n\\\\Time\\:\\:setTimestamp\\(\\) should be covariant with return type \\(static\\(DateTimeImmutable\\)\\) of method DateTimeImmutable\\:\\:setTimestamp\\(\\)$#', - '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\\.$#', @@ -7519,18 +7507,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/I18n/TimeLegacy.php', ]; -$ignoreErrors[] = [ - // identifier: method.childReturnType - 'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\TimeLegacy\\) of method CodeIgniter\\\\I18n\\\\TimeLegacy\\:\\:setTimestamp\\(\\) should be covariant with return type \\(static\\(DateTime\\)\\) of method DateTime\\:\\:setTimestamp\\(\\)$#', - '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\\.$#', diff --git a/system/I18n/Time.php b/system/I18n/Time.php index 906479470b17..255f879a90ec 100644 --- a/system/I18n/Time.php +++ b/system/I18n/Time.php @@ -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 diff --git a/system/I18n/TimeLegacy.php b/system/I18n/TimeLegacy.php index 403fced2108d..b62877ec40d6 100644 --- a/system/I18n/TimeLegacy.php +++ b/system/I18n/TimeLegacy.php @@ -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 */ diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index f96b870610b4..1f4f630750b2 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -78,7 +78,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc $time ??= ''; // If a test instance has been provided, use it instead. - if ($time === '' && static::$testNow instanceof self) { + if ($time === '' && static::$testNow instanceof static) { if ($timezone !== null) { $testNow = static::$testNow->setTimezone($timezone); $time = $testNow->format('Y-m-d H:i:s.u'); @@ -108,13 +108,13 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ public static function now($timezone = null, ?string $locale = null) { - return new self(null, $timezone, $locale); + return new static(null, $timezone, $locale); } /** @@ -125,13 +125,13 @@ public static function now($timezone = null, ?string $locale = null) * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ public static function parse(string $datetime, $timezone = null, ?string $locale = null) { - return new self($datetime, $timezone, $locale); + return new static($datetime, $timezone, $locale); } /** @@ -139,13 +139,13 @@ public static function parse(string $datetime, $timezone = null, ?string $locale * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ public static function today($timezone = null, ?string $locale = null) { - return new self(date('Y-m-d 00:00:00'), $timezone, $locale); + return new static(date('Y-m-d 00:00:00'), $timezone, $locale); } /** @@ -153,13 +153,13 @@ public static function today($timezone = null, ?string $locale = null) * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ public static function yesterday($timezone = null, ?string $locale = null) { - return new self(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); + return new static(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); } /** @@ -167,13 +167,13 @@ public static function yesterday($timezone = null, ?string $locale = null) * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ public static function tomorrow($timezone = null, ?string $locale = null) { - return new self(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); + return new static(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); } /** @@ -182,7 +182,7 @@ public static function tomorrow($timezone = null, ?string $locale = null) * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ @@ -196,7 +196,7 @@ public static function createFromDate(?int $year = null, ?int $month = null, ?in * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ @@ -210,7 +210,7 @@ public static function createFromTime(?int $hour = null, ?int $minutes = null, ? * * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ @@ -231,7 +231,7 @@ public static function create( $minutes ??= 0; $seconds ??= 0; - return new self(date('Y-m-d H:i:s', strtotime("{$year}-{$month}-{$day} {$hour}:{$minutes}:{$seconds}")), $timezone, $locale); + return new static(date('Y-m-d H:i:s', strtotime("{$year}-{$month}-{$day} {$hour}:{$minutes}:{$seconds}")), $timezone, $locale); } /** @@ -242,7 +242,7 @@ public static function create( * @param string $datetime * @param DateTimeZone|string|null $timezone * - * @return self + * @return static * * @throws Exception */ @@ -253,7 +253,7 @@ public static function createFromFormat($format, $datetime, $timezone = null) throw I18nException::forInvalidFormat($format); } - return new self($date->format('Y-m-d H:i:s.u'), $timezone); + return new static($date->format('Y-m-d H:i:s.u'), $timezone); } /** @@ -265,7 +265,7 @@ public static function createFromFormat($format, $datetime, $timezone = null) */ public static function createFromTimestamp(float|int $timestamp, $timezone = null, ?string $locale = null): static { - $time = new self('@' . $timestamp, 'UTC', $locale); + $time = new static('@' . $timestamp, 'UTC', $locale); $timezone ??= 'UTC'; @@ -275,7 +275,7 @@ public static function createFromTimestamp(float|int $timestamp, $timezone = nul /** * Takes an instance of DateTimeInterface and returns an instance of Time with it's same values. * - * @return self + * @return static * * @throws Exception */ @@ -284,13 +284,13 @@ public static function createFromInstance(DateTimeInterface $dateTime, ?string $ $date = $dateTime->format('Y-m-d H:i:s.u'); $timezone = $dateTime->getTimezone(); - return new self($date, $timezone, $locale); + return new static($date, $timezone, $locale); } /** * Takes an instance of DateTime and returns an instance of Time with it's same values. * - * @return self + * @return static * * @throws Exception * @@ -300,7 +300,7 @@ public static function createFromInstance(DateTimeInterface $dateTime, ?string $ */ public static function instance(DateTime $dateTime, ?string $locale = null) { - return self::createFromInstance($dateTime, $locale); + return static::createFromInstance($dateTime, $locale); } /** @@ -345,9 +345,9 @@ public static function setTestNow($datetime = null, $timezone = null, ?string $l // Convert to a Time instance if (is_string($datetime)) { - $datetime = new self($datetime, $timezone, $locale); - } elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof self) { - $datetime = new self($datetime->format('Y-m-d H:i:s.u'), $timezone); + $datetime = new static($datetime, $timezone, $locale); + } elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof static) { + $datetime = new static($datetime->format('Y-m-d H:i:s.u'), $timezone); } static::$testNow = $datetime; @@ -475,7 +475,7 @@ public function getWeekOfYear(): string public function getAge() { // future dates have no age - return max(0, $this->difference(self::now())->getYears()); + return max(0, $this->difference(static::now())->getYears()); } /** @@ -532,7 +532,7 @@ public function getTimezoneName(): string * * @param int|string $value * - * @return self + * @return static * * @throws Exception */ @@ -546,7 +546,7 @@ public function setYear($value) * * @param int|string $value * - * @return self + * @return static * * @throws Exception */ @@ -568,7 +568,7 @@ public function setMonth($value) * * @param int|string $value * - * @return self + * @return static * * @throws Exception */ @@ -592,7 +592,7 @@ public function setDay($value) * * @param int|string $value * - * @return self + * @return static * * @throws Exception */ @@ -610,7 +610,7 @@ public function setHour($value) * * @param int|string $value * - * @return self + * @return static * * @throws Exception */ @@ -628,7 +628,7 @@ public function setMinute($value) * * @param int|string $value * - * @return self + * @return static * * @throws Exception */ @@ -646,7 +646,7 @@ public function setSecond($value) * * @param int $value * - * @return self + * @return static * * @throws Exception */ @@ -656,7 +656,7 @@ protected function setValue(string $name, $value) ${$name} = $value; - return self::create( + return static::create( (int) $year, (int) $month, (int) $day, @@ -673,7 +673,7 @@ protected function setValue(string $name, $value) * * @param DateTimeZone|string $timezone * - * @return self + * @return static * * @throws Exception */ @@ -682,7 +682,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); } /** @@ -690,7 +690,7 @@ public function setTimezone($timezone) * * @param int $timestamp * - * @return self + * @return static * * @throws Exception */ @@ -699,7 +699,7 @@ public function setTimestamp($timestamp) { $time = date('Y-m-d H:i:s', $timestamp); - return self::parse($time, $this->timezone, $this->locale); + return static::parse($time, $this->timezone, $this->locale); } // -------------------------------------------------------------------- @@ -1085,7 +1085,7 @@ public function difference($testTime, ?string $timezone = null) if (is_string($testTime)) { $timezone = ($timezone !== null) ? new DateTimeZone($timezone) : $this->timezone; $testTime = new DateTime($testTime, $timezone); - } elseif ($testTime instanceof self) { + } elseif ($testTime instanceof static) { $testTime = $testTime->toDateTime(); } @@ -1116,7 +1116,7 @@ public function difference($testTime, ?string $timezone = null) */ public function getUTCObject($time, ?string $timezone = null) { - if ($time instanceof self) { + if ($time instanceof static) { $time = $time->toDateTime(); } elseif (is_string($time)) { $timezone = $timezone ?: $this->timezone;