Skip to content

Commit fc0cb19

Browse files
authored
refactor: Use native return types instead of using #[ReturnTypeWillChange] (#9900)
* refactor: Use native return types instead of using `#[ReturnTypeWillChange]` * Add changelog * Fix some errors in psalm
1 parent c7e784f commit fc0cb19

File tree

13 files changed

+44
-86
lines changed

13 files changed

+44
-86
lines changed

psalm-baseline.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.13.0@70cdf647255a1362b426bb0f522a85817b8c791c">
2+
<files psalm-version="6.14.3@d0b040a91f280f071c1abcb1b77ce3822058725a">
33
<file src="app/Config/View.php">
44
<UndefinedDocblockClass>
55
<code><![CDATA[array<string, list<parser_callable_string>|parser_callable_string|parser_callable>]]></code>
@@ -64,8 +64,7 @@
6464
</file>
6565
<file src="system/I18n/TimeTrait.php">
6666
<MissingImmutableAnnotation>
67-
<code><![CDATA[#[ReturnTypeWillChange]]]></code>
68-
<code><![CDATA[#[ReturnTypeWillChange]]]></code>
67+
<code><![CDATA[public function setTimezone($timezone): static]]></code>
6968
</MissingImmutableAnnotation>
7069
</file>
7170
<file src="system/Language/Language.php">

system/Cookie/Cookie.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use CodeIgniter\I18n\Time;
2121
use Config\Cookie as CookieConfig;
2222
use DateTimeInterface;
23-
use ReturnTypeWillChange;
2423

2524
/**
2625
* A `Cookie` class represents an immutable HTTP cookie value object.
@@ -606,12 +605,9 @@ public function offsetExists($offset): bool
606605
*
607606
* @param string $offset
608607
*
609-
* @return bool|int|string
610-
*
611608
* @throws InvalidArgumentException
612609
*/
613-
#[ReturnTypeWillChange]
614-
public function offsetGet($offset)
610+
public function offsetGet($offset): bool|int|string
615611
{
616612
if (! $this->offsetExists($offset)) {
617613
throw new InvalidArgumentException(sprintf('Undefined offset "%s".', $offset));

system/Entity/Entity.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use DateTimeInterface;
3434
use Exception;
3535
use JsonSerializable;
36-
use ReturnTypeWillChange;
3736
use Traversable;
3837
use UnitEnum;
3938

@@ -602,8 +601,7 @@ protected function dataCaster(): ?DataCaster
602601
*
603602
* @return array<string, mixed>
604603
*/
605-
#[ReturnTypeWillChange]
606-
public function jsonSerialize()
604+
public function jsonSerialize(): array
607605
{
608606
return $this->toArray();
609607
}

system/Files/File.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use CodeIgniter\Files\Exceptions\FileNotFoundException;
1818
use CodeIgniter\I18n\Time;
1919
use Config\Mimes;
20-
use ReturnTypeWillChange;
20+
use RuntimeException;
2121
use SplFileInfo;
2222

2323
/**
@@ -59,13 +59,11 @@ public function __construct(string $path, bool $checkFile = false)
5959
*
6060
* Implementations SHOULD return the value stored in the "size" key of
6161
* the file in the $_FILES array if available, as PHP calculates this based
62-
* on the actual size transmitted. A RuntimeException will be thrown if the file
63-
* does not exist or an error occurs.
62+
* on the actual size transmitted.
6463
*
65-
* @return false|int The file size in bytes, or false on failure
64+
* @throws RuntimeException if the file does not exist or an error occurs
6665
*/
67-
#[ReturnTypeWillChange]
68-
public function getSize()
66+
public function getSize(): false|int
6967
{
7068
return $this->size ?? ($this->size = parent::getSize());
7169
}

system/I18n/TimeLegacy.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use DateTime;
1717
use Exception;
18-
use ReturnTypeWillChange;
1918

2019
/**
2120
* Legacy Time class.
@@ -55,12 +54,9 @@ class TimeLegacy extends DateTime
5554
*
5655
* @param int $timestamp
5756
*
58-
* @return static
59-
*
6057
* @throws Exception
6158
*/
62-
#[ReturnTypeWillChange]
63-
public function setTimestamp($timestamp)
59+
public function setTimestamp($timestamp): static
6460
{
6561
$time = date('Y-m-d H:i:s', $timestamp);
6662

system/I18n/TimeTrait.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use IntlCalendar;
2424
use IntlDateFormatter;
2525
use Locale;
26-
use ReturnTypeWillChange;
2726

2827
/**
2928
* This trait has properties and methods for Time and TimeLegacy.
@@ -238,16 +237,15 @@ public static function create(
238237
* Provides a replacement for DateTime's own createFromFormat function, that provides
239238
* more flexible timeZone handling
240239
*
240+
* @psalm-external-mutation-free
241+
*
241242
* @param string $format
242243
* @param string $datetime
243244
* @param DateTimeZone|string|null $timezone
244245
*
245-
* @return static
246-
*
247246
* @throws Exception
248247
*/
249-
#[ReturnTypeWillChange]
250-
public static function createFromFormat($format, $datetime, $timezone = null)
248+
public static function createFromFormat($format, $datetime, $timezone = null): static
251249
{
252250
if (! $date = parent::createFromFormat($format, $datetime)) {
253251
throw I18nException::forInvalidFormat($format);
@@ -673,16 +671,14 @@ protected function setValue(string $name, $value)
673671
*
674672
* @param DateTimeZone|string $timezone
675673
*
676-
* @return static
677-
*
678674
* @throws Exception
679675
*/
680-
#[ReturnTypeWillChange]
681-
public function setTimezone($timezone)
676+
public function setTimezone($timezone): static
682677
{
683678
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
679+
$dateTime = $this->toDateTime()->setTimezone($timezone);
684680

685-
return static::createFromInstance($this->toDateTime()->setTimezone($timezone), $this->locale);
681+
return static::createFromInstance($dateTime, $this->locale);
686682
}
687683

688684
// --------------------------------------------------------------------

system/Session/Handlers/ArrayHandler.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace CodeIgniter\Session\Handlers;
1515

16-
use ReturnTypeWillChange;
17-
1816
/**
1917
* Session handler using static array for storage.
2018
* Intended only for use during testing.
@@ -41,12 +39,8 @@ public function open($path, $name): bool
4139
* Reads the session data from the session storage, and returns the results.
4240
*
4341
* @param string $id The session ID.
44-
*
45-
* @return false|string Returns an encoded string of the read data.
46-
* If nothing was read, it must return false.
4742
*/
48-
#[ReturnTypeWillChange]
49-
public function read($id)
43+
public function read($id): string
5044
{
5145
return '';
5246
}
@@ -85,11 +79,8 @@ public function destroy($id): bool
8579
*
8680
* @param int $max_lifetime Sessions that have not updated
8781
* for the last max_lifetime seconds will be removed.
88-
*
89-
* @return false|int Returns the number of deleted sessions on success, or false on failure.
9082
*/
91-
#[ReturnTypeWillChange]
92-
public function gc($max_lifetime)
83+
public function gc($max_lifetime): int
9384
{
9485
return 1;
9586
}

system/Session/Handlers/Database/PostgreHandler.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseBuilder;
1717
use CodeIgniter\Session\Handlers\DatabaseHandler;
18-
use ReturnTypeWillChange;
1918

2019
/**
2120
* Session handler for Postgre
@@ -59,11 +58,8 @@ protected function prepareData(string $data): string
5958
*
6059
* @param int $max_lifetime Sessions that have not updated
6160
* for the last max_lifetime seconds will be removed.
62-
*
63-
* @return false|int Returns the number of deleted sessions on success, or false on failure.
6461
*/
65-
#[ReturnTypeWillChange]
66-
public function gc($max_lifetime)
62+
public function gc($max_lifetime): false|int
6763
{
6864
$separator = '\'';
6965
$interval = implode($separator, ['', "{$max_lifetime} second", '']);

system/Session/Handlers/DatabaseHandler.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use CodeIgniter\Session\Exceptions\SessionException;
1919
use Config\Database;
2020
use Config\Session as SessionConfig;
21-
use ReturnTypeWillChange;
2221

2322
/**
2423
* Base database session handler.
@@ -107,12 +106,8 @@ public function open($path, $name): bool
107106
* Reads the session data from the session storage, and returns the results.
108107
*
109108
* @param string $id The session ID
110-
*
111-
* @return false|string Returns an encoded string of the read data.
112-
* If nothing was read, it must return false.
113109
*/
114-
#[ReturnTypeWillChange]
115-
public function read($id)
110+
public function read($id): false|string
116111
{
117112
if ($this->lockSession($id) === false) {
118113
$this->fingerprint = md5('');
@@ -281,8 +276,7 @@ public function destroy($id): bool
281276
*
282277
* @return false|int Returns the number of deleted sessions on success, or false on failure.
283278
*/
284-
#[ReturnTypeWillChange]
285-
public function gc($max_lifetime)
279+
public function gc($max_lifetime): false|int
286280
{
287281
return $this->db->table($this->table)->where(
288282
'timestamp <',

system/Session/Handlers/FileHandler.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\I18n\Time;
1717
use CodeIgniter\Session\Exceptions\SessionException;
1818
use Config\Session as SessionConfig;
19-
use ReturnTypeWillChange;
2019

2120
/**
2221
* Session handler using file system for storage.
@@ -115,12 +114,8 @@ public function open($path, $name): bool
115114
* Reads the session data from the session storage, and returns the results.
116115
*
117116
* @param string $id The session ID.
118-
*
119-
* @return false|string Returns an encoded string of the read data.
120-
* If nothing was read, it must return false.
121117
*/
122-
#[ReturnTypeWillChange]
123-
public function read($id)
118+
public function read($id): false|string
124119
{
125120
// This might seem weird, but PHP 5.6 introduced session_reset(),
126121
// which re-reads session data
@@ -267,11 +262,8 @@ public function destroy($id): bool
267262
*
268263
* @param int $max_lifetime Sessions that have not updated
269264
* for the last max_lifetime seconds will be removed.
270-
*
271-
* @return false|int Returns the number of deleted sessions on success, or false on failure.
272265
*/
273-
#[ReturnTypeWillChange]
274-
public function gc($max_lifetime)
266+
public function gc($max_lifetime): false|int
275267
{
276268
if (! is_dir($this->savePath) || ($directory = opendir($this->savePath)) === false) {
277269
$this->logger->debug("Session: Garbage collector couldn't list files under directory '" . $this->savePath . "'.");

0 commit comments

Comments
 (0)