diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 160ba4d97cd8..7a03f82c5e21 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + |parser_callable_string|parser_callable>]]> @@ -64,8 +64,7 @@ - - + diff --git a/system/Cookie/Cookie.php b/system/Cookie/Cookie.php index 73670ce4e7b1..e144d84ba031 100644 --- a/system/Cookie/Cookie.php +++ b/system/Cookie/Cookie.php @@ -20,7 +20,6 @@ use CodeIgniter\I18n\Time; use Config\Cookie as CookieConfig; use DateTimeInterface; -use ReturnTypeWillChange; /** * A `Cookie` class represents an immutable HTTP cookie value object. @@ -606,12 +605,9 @@ public function offsetExists($offset): bool * * @param string $offset * - * @return bool|int|string - * * @throws InvalidArgumentException */ - #[ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet($offset): bool|int|string { if (! $this->offsetExists($offset)) { throw new InvalidArgumentException(sprintf('Undefined offset "%s".', $offset)); diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 6541d985d33a..43182cfb9b2a 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -33,7 +33,6 @@ use DateTimeInterface; use Exception; use JsonSerializable; -use ReturnTypeWillChange; use Traversable; use UnitEnum; @@ -602,8 +601,7 @@ protected function dataCaster(): ?DataCaster * * @return array */ - #[ReturnTypeWillChange] - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } diff --git a/system/Files/File.php b/system/Files/File.php index a493b59e2a32..83a304f26e99 100644 --- a/system/Files/File.php +++ b/system/Files/File.php @@ -17,7 +17,7 @@ use CodeIgniter\Files\Exceptions\FileNotFoundException; use CodeIgniter\I18n\Time; use Config\Mimes; -use ReturnTypeWillChange; +use RuntimeException; use SplFileInfo; /** @@ -59,13 +59,11 @@ public function __construct(string $path, bool $checkFile = false) * * Implementations SHOULD return the value stored in the "size" key of * the file in the $_FILES array if available, as PHP calculates this based - * on the actual size transmitted. A RuntimeException will be thrown if the file - * does not exist or an error occurs. + * on the actual size transmitted. * - * @return false|int The file size in bytes, or false on failure + * @throws RuntimeException if the file does not exist or an error occurs */ - #[ReturnTypeWillChange] - public function getSize() + public function getSize(): false|int { return $this->size ?? ($this->size = parent::getSize()); } diff --git a/system/I18n/TimeLegacy.php b/system/I18n/TimeLegacy.php index 7dfa0526304a..034cab0dadcf 100644 --- a/system/I18n/TimeLegacy.php +++ b/system/I18n/TimeLegacy.php @@ -15,7 +15,6 @@ use DateTime; use Exception; -use ReturnTypeWillChange; /** * Legacy Time class. @@ -55,12 +54,9 @@ class TimeLegacy extends DateTime * * @param int $timestamp * - * @return static - * * @throws Exception */ - #[ReturnTypeWillChange] - public function setTimestamp($timestamp) + public function setTimestamp($timestamp): static { $time = date('Y-m-d H:i:s', $timestamp); diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index 42cd1724b8c4..d9abb16e29a2 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -23,7 +23,6 @@ use IntlCalendar; use IntlDateFormatter; use Locale; -use ReturnTypeWillChange; /** * This trait has properties and methods for Time and TimeLegacy. @@ -238,16 +237,15 @@ public static function create( * Provides a replacement for DateTime's own createFromFormat function, that provides * more flexible timeZone handling * + * @psalm-external-mutation-free + * * @param string $format * @param string $datetime * @param DateTimeZone|string|null $timezone * - * @return static - * * @throws Exception */ - #[ReturnTypeWillChange] - public static function createFromFormat($format, $datetime, $timezone = null) + public static function createFromFormat($format, $datetime, $timezone = null): static { if (! $date = parent::createFromFormat($format, $datetime)) { throw I18nException::forInvalidFormat($format); @@ -673,16 +671,14 @@ protected function setValue(string $name, $value) * * @param DateTimeZone|string $timezone * - * @return static - * * @throws Exception */ - #[ReturnTypeWillChange] - public function setTimezone($timezone) + public function setTimezone($timezone): static { $timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); + $dateTime = $this->toDateTime()->setTimezone($timezone); - return static::createFromInstance($this->toDateTime()->setTimezone($timezone), $this->locale); + return static::createFromInstance($dateTime, $this->locale); } // -------------------------------------------------------------------- diff --git a/system/Session/Handlers/ArrayHandler.php b/system/Session/Handlers/ArrayHandler.php index 0db48bb4b0c2..2643173cc742 100644 --- a/system/Session/Handlers/ArrayHandler.php +++ b/system/Session/Handlers/ArrayHandler.php @@ -13,8 +13,6 @@ namespace CodeIgniter\Session\Handlers; -use ReturnTypeWillChange; - /** * Session handler using static array for storage. * Intended only for use during testing. @@ -41,12 +39,8 @@ public function open($path, $name): bool * Reads the session data from the session storage, and returns the results. * * @param string $id The session ID. - * - * @return false|string Returns an encoded string of the read data. - * If nothing was read, it must return false. */ - #[ReturnTypeWillChange] - public function read($id) + public function read($id): string { return ''; } @@ -85,11 +79,8 @@ public function destroy($id): bool * * @param int $max_lifetime Sessions that have not updated * for the last max_lifetime seconds will be removed. - * - * @return false|int Returns the number of deleted sessions on success, or false on failure. */ - #[ReturnTypeWillChange] - public function gc($max_lifetime) + public function gc($max_lifetime): int { return 1; } diff --git a/system/Session/Handlers/Database/PostgreHandler.php b/system/Session/Handlers/Database/PostgreHandler.php index be0621efb945..1471d44d1860 100644 --- a/system/Session/Handlers/Database/PostgreHandler.php +++ b/system/Session/Handlers/Database/PostgreHandler.php @@ -15,7 +15,6 @@ use CodeIgniter\Database\BaseBuilder; use CodeIgniter\Session\Handlers\DatabaseHandler; -use ReturnTypeWillChange; /** * Session handler for Postgre @@ -59,11 +58,8 @@ protected function prepareData(string $data): string * * @param int $max_lifetime Sessions that have not updated * for the last max_lifetime seconds will be removed. - * - * @return false|int Returns the number of deleted sessions on success, or false on failure. */ - #[ReturnTypeWillChange] - public function gc($max_lifetime) + public function gc($max_lifetime): false|int { $separator = '\''; $interval = implode($separator, ['', "{$max_lifetime} second", '']); diff --git a/system/Session/Handlers/DatabaseHandler.php b/system/Session/Handlers/DatabaseHandler.php index e19986461384..c4ce2b8222e7 100644 --- a/system/Session/Handlers/DatabaseHandler.php +++ b/system/Session/Handlers/DatabaseHandler.php @@ -18,7 +18,6 @@ use CodeIgniter\Session\Exceptions\SessionException; use Config\Database; use Config\Session as SessionConfig; -use ReturnTypeWillChange; /** * Base database session handler. @@ -107,12 +106,8 @@ public function open($path, $name): bool * Reads the session data from the session storage, and returns the results. * * @param string $id The session ID - * - * @return false|string Returns an encoded string of the read data. - * If nothing was read, it must return false. */ - #[ReturnTypeWillChange] - public function read($id) + public function read($id): false|string { if ($this->lockSession($id) === false) { $this->fingerprint = md5(''); @@ -281,8 +276,7 @@ public function destroy($id): bool * * @return false|int Returns the number of deleted sessions on success, or false on failure. */ - #[ReturnTypeWillChange] - public function gc($max_lifetime) + public function gc($max_lifetime): false|int { return $this->db->table($this->table)->where( 'timestamp <', diff --git a/system/Session/Handlers/FileHandler.php b/system/Session/Handlers/FileHandler.php index 3e400fb9af45..f5e9261af0f4 100644 --- a/system/Session/Handlers/FileHandler.php +++ b/system/Session/Handlers/FileHandler.php @@ -16,7 +16,6 @@ use CodeIgniter\I18n\Time; use CodeIgniter\Session\Exceptions\SessionException; use Config\Session as SessionConfig; -use ReturnTypeWillChange; /** * Session handler using file system for storage. @@ -115,12 +114,8 @@ public function open($path, $name): bool * Reads the session data from the session storage, and returns the results. * * @param string $id The session ID. - * - * @return false|string Returns an encoded string of the read data. - * If nothing was read, it must return false. */ - #[ReturnTypeWillChange] - public function read($id) + public function read($id): false|string { // This might seem weird, but PHP 5.6 introduced session_reset(), // which re-reads session data @@ -267,11 +262,8 @@ public function destroy($id): bool * * @param int $max_lifetime Sessions that have not updated * for the last max_lifetime seconds will be removed. - * - * @return false|int Returns the number of deleted sessions on success, or false on failure. */ - #[ReturnTypeWillChange] - public function gc($max_lifetime) + public function gc($max_lifetime): false|int { if (! is_dir($this->savePath) || ($directory = opendir($this->savePath)) === false) { $this->logger->debug("Session: Garbage collector couldn't list files under directory '" . $this->savePath . "'."); diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index 90d02f6f394b..6e28e6c9488e 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -17,7 +17,6 @@ use CodeIgniter\Session\Exceptions\SessionException; use Config\Session as SessionConfig; use Memcached; -use ReturnTypeWillChange; /** * Session handler using Memcached for persistence. @@ -138,12 +137,8 @@ public function open($path, $name): bool * Reads the session data from the session storage, and returns the results. * * @param string $id The session ID. - * - * @return false|string Returns an encoded string of the read data. - * If nothing was read, it must return false. */ - #[ReturnTypeWillChange] - public function read($id) + public function read($id): false|string { if (isset($this->memcached) && $this->lockSession($id)) { if (! isset($this->sessionID)) { @@ -243,11 +238,8 @@ public function destroy($id): bool * * @param int $max_lifetime Sessions that have not updated * for the last max_lifetime seconds will be removed. - * - * @return false|int Returns the number of deleted sessions on success, or false on failure. */ - #[ReturnTypeWillChange] - public function gc($max_lifetime) + public function gc($max_lifetime): int { return 1; } diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index 213272bb87c7..4a6d8a6b8327 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -18,7 +18,6 @@ use Config\Session as SessionConfig; use Redis; use RedisException; -use ReturnTypeWillChange; /** * Session handler using Redis for persistence. @@ -204,13 +203,9 @@ public function open($path, $name): bool * * @param string $id The session ID. * - * @return false|string Returns an encoded string of the read data. - * If nothing was read, it must return false. - * * @throws RedisException */ - #[ReturnTypeWillChange] - public function read($id) + public function read($id): false|string { if (isset($this->redis) && $this->lockSession($id)) { if (! isset($this->sessionID)) { @@ -333,11 +328,8 @@ public function destroy($id): bool * * @param int $max_lifetime Sessions that have not updated * for the last max_lifetime seconds will be removed. - * - * @return false|int Returns the number of deleted sessions on success, or false on failure. */ - #[ReturnTypeWillChange] - public function gc($max_lifetime) + public function gc($max_lifetime): int { return 1; } diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index c1fe14c0fcb6..400f181f3244 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -217,6 +217,24 @@ Method Signature Changes - Added native return types to ``CodeIgniter\Debug\Toolbar`` methods: - ``prepare(): void`` - ``respond(): void`` +- Methods previously having ``#[ReturnTypeWillChange]`` attribute have been updated to include the native return types: + - ``CodeIgniter\Cookie\Cookie::offsetGet($offset): bool|int|string`` + - ``CodeIgniter\Entity\Entity::jsonSerialize(): array`` + - ``CodeIgniter\Files\File::getSize(): false|int`` + - ``CodeIgniter\I18n\TimeLegacy::setTimestamp($timestamp): static`` + - ``CodeIgniter\I18n\TimeTrait::createFromFormat($format, $time, $timezone = null): static`` + - ``CodeIgniter\I18n\TimeTrait::setTimezone($timezone): static`` + - ``CodeIgniter\Session\Handlers\Database\PostgreHandler::gc($max_lifetime): false|int`` + - ``CodeIgniter\Session\Handlers\ArrayHandler::read($id): string`` + - ``CodeIgniter\Session\Handlers\ArrayHandler::gc($max_lifetime): int`` + - ``CodeIgniter\Session\Handlers\DatabaseHandler::read($id): false|string`` + - ``CodeIgniter\Session\Handlers\DatabaseHandler::gc($max_lifetime): false|int`` + - ``CodeIgniter\Session\Handlers\FileHandler::read($id): false|string`` + - ``CodeIgniter\Session\Handlers\FileHandler::gc($max_lifetime): false|int`` + - ``CodeIgniter\Session\Handlers\MemcachedHandler::read($id): false|string`` + - ``CodeIgniter\Session\Handlers\MemcachedHandler::gc($max_lifetime): int`` + - ``CodeIgniter\Session\Handlers\RedisHandler::read($id): false|string`` + - ``CodeIgniter\Session\Handlers\RedisHandler::gc($max_lifetime): int`` Property Signature Changes ==========================