Skip to content

Commit c94a95d

Browse files
committed
refactor: replace self with static
1 parent 98a6d35 commit c94a95d

File tree

4 files changed

+44
-64
lines changed

4 files changed

+44
-64
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7495,18 +7495,6 @@
74957495
'count' => 1,
74967496
'path' => __DIR__ . '/system/I18n/Time.php',
74977497
];
7498-
$ignoreErrors[] = [
7499-
// identifier: method.childReturnType
7500-
'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\Time\\) of method CodeIgniter\\\\I18n\\\\Time\\:\\:setTimestamp\\(\\) should be covariant with return type \\(static\\(DateTimeImmutable\\)\\) of method DateTimeImmutable\\:\\:setTimestamp\\(\\)$#',
7501-
'count' => 1,
7502-
'path' => __DIR__ . '/system/I18n/Time.php',
7503-
];
7504-
$ignoreErrors[] = [
7505-
// identifier: method.childReturnType
7506-
'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\Time\\) of method CodeIgniter\\\\I18n\\\\Time\\:\\:setTimezone\\(\\) should be covariant with return type \\(static\\(DateTimeImmutable\\)\\) of method DateTimeImmutable\\:\\:setTimezone\\(\\)$#',
7507-
'count' => 1,
7508-
'path' => __DIR__ . '/system/I18n/Time.php',
7509-
];
75107498
$ignoreErrors[] = [
75117499
// identifier: ternary.shortNotAllowed
75127500
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
@@ -7519,18 +7507,6 @@
75197507
'count' => 1,
75207508
'path' => __DIR__ . '/system/I18n/TimeLegacy.php',
75217509
];
7522-
$ignoreErrors[] = [
7523-
// identifier: method.childReturnType
7524-
'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\TimeLegacy\\) of method CodeIgniter\\\\I18n\\\\TimeLegacy\\:\\:setTimestamp\\(\\) should be covariant with return type \\(static\\(DateTime\\)\\) of method DateTime\\:\\:setTimestamp\\(\\)$#',
7525-
'count' => 1,
7526-
'path' => __DIR__ . '/system/I18n/TimeLegacy.php',
7527-
];
7528-
$ignoreErrors[] = [
7529-
// identifier: method.childReturnType
7530-
'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\TimeLegacy\\) of method CodeIgniter\\\\I18n\\\\TimeLegacy\\:\\:setTimezone\\(\\) should be covariant with return type \\(static\\(DateTime\\)\\) of method DateTime\\:\\:setTimezone\\(\\)$#',
7531-
'count' => 1,
7532-
'path' => __DIR__ . '/system/I18n/TimeLegacy.php',
7533-
];
75347510
$ignoreErrors[] = [
75357511
// identifier: ternary.shortNotAllowed
75367512
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',

system/I18n/Time.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* @property-read string $weekOfYear
4040
* @property-read string $year
4141
*
42+
* @phpstan-consistent-constructor
43+
*
4244
* @see \CodeIgniter\I18n\TimeTest
4345
*/
4446
class Time extends DateTimeImmutable implements Stringable

system/I18n/TimeLegacy.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* @property string $weekOfYear read-only
4040
* @property string $year read-only
4141
*
42+
* @phpstan-consistent-constructor
43+
*
4244
* @deprecated Use Time instead.
4345
* @see \CodeIgniter\I18n\TimeLegacyTest
4446
*/

system/I18n/TimeTrait.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
7878
$time ??= '';
7979

8080
// If a test instance has been provided, use it instead.
81-
if ($time === '' && static::$testNow instanceof self) {
81+
if ($time === '' && static::$testNow instanceof static) {
8282
if ($timezone !== null) {
8383
$testNow = static::$testNow->setTimezone($timezone);
8484
$time = $testNow->format('Y-m-d H:i:s.u');
@@ -108,13 +108,13 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
108108
*
109109
* @param DateTimeZone|string|null $timezone
110110
*
111-
* @return self
111+
* @return static
112112
*
113113
* @throws Exception
114114
*/
115115
public static function now($timezone = null, ?string $locale = null)
116116
{
117-
return new self(null, $timezone, $locale);
117+
return new static(null, $timezone, $locale);
118118
}
119119

120120
/**
@@ -125,55 +125,55 @@ public static function now($timezone = null, ?string $locale = null)
125125
*
126126
* @param DateTimeZone|string|null $timezone
127127
*
128-
* @return self
128+
* @return static
129129
*
130130
* @throws Exception
131131
*/
132132
public static function parse(string $datetime, $timezone = null, ?string $locale = null)
133133
{
134-
return new self($datetime, $timezone, $locale);
134+
return new static($datetime, $timezone, $locale);
135135
}
136136

137137
/**
138138
* Return a new time with the time set to midnight.
139139
*
140140
* @param DateTimeZone|string|null $timezone
141141
*
142-
* @return self
142+
* @return static
143143
*
144144
* @throws Exception
145145
*/
146146
public static function today($timezone = null, ?string $locale = null)
147147
{
148-
return new self(date('Y-m-d 00:00:00'), $timezone, $locale);
148+
return new static(date('Y-m-d 00:00:00'), $timezone, $locale);
149149
}
150150

151151
/**
152152
* Returns an instance set to midnight yesterday morning.
153153
*
154154
* @param DateTimeZone|string|null $timezone
155155
*
156-
* @return self
156+
* @return static
157157
*
158158
* @throws Exception
159159
*/
160160
public static function yesterday($timezone = null, ?string $locale = null)
161161
{
162-
return new self(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale);
162+
return new static(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale);
163163
}
164164

165165
/**
166166
* Returns an instance set to midnight tomorrow morning.
167167
*
168168
* @param DateTimeZone|string|null $timezone
169169
*
170-
* @return self
170+
* @return static
171171
*
172172
* @throws Exception
173173
*/
174174
public static function tomorrow($timezone = null, ?string $locale = null)
175175
{
176-
return new self(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale);
176+
return new static(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale);
177177
}
178178

179179
/**
@@ -182,7 +182,7 @@ public static function tomorrow($timezone = null, ?string $locale = null)
182182
*
183183
* @param DateTimeZone|string|null $timezone
184184
*
185-
* @return self
185+
* @return static
186186
*
187187
* @throws Exception
188188
*/
@@ -196,7 +196,7 @@ public static function createFromDate(?int $year = null, ?int $month = null, ?in
196196
*
197197
* @param DateTimeZone|string|null $timezone
198198
*
199-
* @return self
199+
* @return static
200200
*
201201
* @throws Exception
202202
*/
@@ -210,7 +210,7 @@ public static function createFromTime(?int $hour = null, ?int $minutes = null, ?
210210
*
211211
* @param DateTimeZone|string|null $timezone
212212
*
213-
* @return self
213+
* @return static
214214
*
215215
* @throws Exception
216216
*/
@@ -231,7 +231,7 @@ public static function create(
231231
$minutes ??= 0;
232232
$seconds ??= 0;
233233

234-
return new self(date('Y-m-d H:i:s', strtotime("{$year}-{$month}-{$day} {$hour}:{$minutes}:{$seconds}")), $timezone, $locale);
234+
return new static(date('Y-m-d H:i:s', strtotime("{$year}-{$month}-{$day} {$hour}:{$minutes}:{$seconds}")), $timezone, $locale);
235235
}
236236

237237
/**
@@ -242,7 +242,7 @@ public static function create(
242242
* @param string $datetime
243243
* @param DateTimeZone|string|null $timezone
244244
*
245-
* @return self
245+
* @return static
246246
*
247247
* @throws Exception
248248
*/
@@ -253,7 +253,7 @@ public static function createFromFormat($format, $datetime, $timezone = null)
253253
throw I18nException::forInvalidFormat($format);
254254
}
255255

256-
return new self($date->format('Y-m-d H:i:s.u'), $timezone);
256+
return new static($date->format('Y-m-d H:i:s.u'), $timezone);
257257
}
258258

259259
/**
@@ -265,7 +265,7 @@ public static function createFromFormat($format, $datetime, $timezone = null)
265265
*/
266266
public static function createFromTimestamp(float|int $timestamp, $timezone = null, ?string $locale = null): static
267267
{
268-
$time = new self('@' . $timestamp, 'UTC', $locale);
268+
$time = new static('@' . $timestamp, 'UTC', $locale);
269269

270270
$timezone ??= 'UTC';
271271

@@ -275,7 +275,7 @@ public static function createFromTimestamp(float|int $timestamp, $timezone = nul
275275
/**
276276
* Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
277277
*
278-
* @return self
278+
* @return static
279279
*
280280
* @throws Exception
281281
*/
@@ -284,13 +284,13 @@ public static function createFromInstance(DateTimeInterface $dateTime, ?string $
284284
$date = $dateTime->format('Y-m-d H:i:s.u');
285285
$timezone = $dateTime->getTimezone();
286286

287-
return new self($date, $timezone, $locale);
287+
return new static($date, $timezone, $locale);
288288
}
289289

290290
/**
291291
* Takes an instance of DateTime and returns an instance of Time with it's same values.
292292
*
293-
* @return self
293+
* @return static
294294
*
295295
* @throws Exception
296296
*
@@ -300,7 +300,7 @@ public static function createFromInstance(DateTimeInterface $dateTime, ?string $
300300
*/
301301
public static function instance(DateTime $dateTime, ?string $locale = null)
302302
{
303-
return self::createFromInstance($dateTime, $locale);
303+
return static::createFromInstance($dateTime, $locale);
304304
}
305305

306306
/**
@@ -345,9 +345,9 @@ public static function setTestNow($datetime = null, $timezone = null, ?string $l
345345

346346
// Convert to a Time instance
347347
if (is_string($datetime)) {
348-
$datetime = new self($datetime, $timezone, $locale);
349-
} elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof self) {
350-
$datetime = new self($datetime->format('Y-m-d H:i:s.u'), $timezone);
348+
$datetime = new static($datetime, $timezone, $locale);
349+
} elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof static) {
350+
$datetime = new static($datetime->format('Y-m-d H:i:s.u'), $timezone);
351351
}
352352

353353
static::$testNow = $datetime;
@@ -475,7 +475,7 @@ public function getWeekOfYear(): string
475475
public function getAge()
476476
{
477477
// future dates have no age
478-
return max(0, $this->difference(self::now())->getYears());
478+
return max(0, $this->difference(static::now())->getYears());
479479
}
480480

481481
/**
@@ -532,7 +532,7 @@ public function getTimezoneName(): string
532532
*
533533
* @param int|string $value
534534
*
535-
* @return self
535+
* @return static
536536
*
537537
* @throws Exception
538538
*/
@@ -546,7 +546,7 @@ public function setYear($value)
546546
*
547547
* @param int|string $value
548548
*
549-
* @return self
549+
* @return static
550550
*
551551
* @throws Exception
552552
*/
@@ -568,7 +568,7 @@ public function setMonth($value)
568568
*
569569
* @param int|string $value
570570
*
571-
* @return self
571+
* @return static
572572
*
573573
* @throws Exception
574574
*/
@@ -592,7 +592,7 @@ public function setDay($value)
592592
*
593593
* @param int|string $value
594594
*
595-
* @return self
595+
* @return static
596596
*
597597
* @throws Exception
598598
*/
@@ -610,7 +610,7 @@ public function setHour($value)
610610
*
611611
* @param int|string $value
612612
*
613-
* @return self
613+
* @return static
614614
*
615615
* @throws Exception
616616
*/
@@ -628,7 +628,7 @@ public function setMinute($value)
628628
*
629629
* @param int|string $value
630630
*
631-
* @return self
631+
* @return static
632632
*
633633
* @throws Exception
634634
*/
@@ -646,7 +646,7 @@ public function setSecond($value)
646646
*
647647
* @param int $value
648648
*
649-
* @return self
649+
* @return static
650650
*
651651
* @throws Exception
652652
*/
@@ -656,7 +656,7 @@ protected function setValue(string $name, $value)
656656

657657
${$name} = $value;
658658

659-
return self::create(
659+
return static::create(
660660
(int) $year,
661661
(int) $month,
662662
(int) $day,
@@ -673,7 +673,7 @@ protected function setValue(string $name, $value)
673673
*
674674
* @param DateTimeZone|string $timezone
675675
*
676-
* @return self
676+
* @return static
677677
*
678678
* @throws Exception
679679
*/
@@ -682,15 +682,15 @@ public function setTimezone($timezone)
682682
{
683683
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
684684

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

688688
/**
689689
* Returns a new instance with the date set to the new timestamp.
690690
*
691691
* @param int $timestamp
692692
*
693-
* @return self
693+
* @return static
694694
*
695695
* @throws Exception
696696
*/
@@ -699,7 +699,7 @@ public function setTimestamp($timestamp)
699699
{
700700
$time = date('Y-m-d H:i:s', $timestamp);
701701

702-
return self::parse($time, $this->timezone, $this->locale);
702+
return static::parse($time, $this->timezone, $this->locale);
703703
}
704704

705705
// --------------------------------------------------------------------
@@ -1085,7 +1085,7 @@ public function difference($testTime, ?string $timezone = null)
10851085
if (is_string($testTime)) {
10861086
$timezone = ($timezone !== null) ? new DateTimeZone($timezone) : $this->timezone;
10871087
$testTime = new DateTime($testTime, $timezone);
1088-
} elseif ($testTime instanceof self) {
1088+
} elseif ($testTime instanceof static) {
10891089
$testTime = $testTime->toDateTime();
10901090
}
10911091

@@ -1116,7 +1116,7 @@ public function difference($testTime, ?string $timezone = null)
11161116
*/
11171117
public function getUTCObject($time, ?string $timezone = null)
11181118
{
1119-
if ($time instanceof self) {
1119+
if ($time instanceof static) {
11201120
$time = $time->toDateTime();
11211121
} elseif (is_string($time)) {
11221122
$timezone = $timezone ?: $this->timezone;

0 commit comments

Comments
 (0)