Skip to content

Commit

Permalink
refactor: replace self with static
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 6, 2024
1 parent d8d7435 commit 6bec622
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 64 deletions.
24 changes: 0 additions & 24 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\.$#',
Expand All @@ -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\\.$#',
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
80 changes: 40 additions & 40 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -125,55 +125,55 @@ 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);
}

/**
* Return a new time with the time set to midnight.
*
* @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);
}

/**
* Returns an instance set to midnight yesterday morning.
*
* @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);
}

/**
* Returns an instance set to midnight tomorrow morning.
*
* @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);
}

/**
Expand All @@ -182,7 +182,7 @@ public static function tomorrow($timezone = null, ?string $locale = null)
*
* @param DateTimeZone|string|null $timezone
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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);
}

/**
Expand All @@ -242,7 +242,7 @@ public static function create(
* @param string $datetime
* @param DateTimeZone|string|null $timezone
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -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);
}

/**
Expand All @@ -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';

Expand All @@ -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
*/
Expand All @@ -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
*
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -532,7 +532,7 @@ public function getTimezoneName(): string
*
* @param int|string $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -546,7 +546,7 @@ public function setYear($value)
*
* @param int|string $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -568,7 +568,7 @@ public function setMonth($value)
*
* @param int|string $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -592,7 +592,7 @@ public function setDay($value)
*
* @param int|string $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -610,7 +610,7 @@ public function setHour($value)
*
* @param int|string $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -628,7 +628,7 @@ public function setMinute($value)
*
* @param int|string $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -646,7 +646,7 @@ public function setSecond($value)
*
* @param int $value
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -656,7 +656,7 @@ protected function setValue(string $name, $value)

${$name} = $value;

return self::create(
return static::create(
(int) $year,
(int) $month,
(int) $day,
Expand All @@ -673,7 +673,7 @@ protected function setValue(string $name, $value)
*
* @param DateTimeZone|string $timezone
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -682,15 +682,15 @@ 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);
}

/**
* Returns a new instance with the date set to the new timestamp.
*
* @param int $timestamp
*
* @return self
* @return static
*
* @throws Exception
*/
Expand All @@ -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);
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6bec622

Please sign in to comment.