diff --git a/system/Session/Session.php b/system/Session/Session.php index 0540c1508a9e..34e3ad3033b9 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -447,26 +447,22 @@ public function close() * If $data is an array, it is expected to be an array of key/value pairs * to be set as session properties. * - * @param array|string $data Property name or associative array of properties - * @param array|bool|float|int|object|string|null $value Property value if single key provided + * @param array|list|string $data Property name or associative array of properties + * @param mixed $value Property value if single key provided * * @return void */ public function set($data, $value = null) { - if (is_array($data)) { - foreach ($data as $key => &$value) { - if (is_int($key)) { - $_SESSION[$value] = null; - } else { - $_SESSION[$key] = $value; - } - } + $data = is_array($data) ? $data : [$data => $value]; - return; + if (array_is_list($data)) { + $data = array_fill_keys($data, null); } - $_SESSION[$data] = $value; + foreach ($data as $sessionKey => $sessionValue) { + $_SESSION[$sessionKey] = $sessionValue; + } } /** @@ -478,31 +474,27 @@ public function set($data, $value = null) * * Replaces the legacy method $session->userdata(); * - * @param non-empty-string|null $key Identifier of the session property to retrieve + * @param string|null $key Identifier of the session property to retrieve * - * @return array|bool|float|int|object|string|null The property value(s) + * @return ($key is string ? mixed : array) */ public function get(?string $key = null) { - if ($key !== null && $key !== '' && (null !== ($value = $_SESSION[$key] ?? null) || null !== ($value = dot_array_search($key, $_SESSION ?? [])))) { - return $value; - } - if (! isset($_SESSION) || $_SESSION === []) { return $key === null ? [] : null; } - if ($key !== null && $key !== '') { - return null; + $key ??= ''; + + if ($key !== '') { + return $_SESSION[$key] ?? dot_array_search($key, $_SESSION); } $userdata = []; - $_exclude = array_merge(['__ci_vars'], $this->getFlashKeys(), $this->getTempKeys()); + $exclude = array_merge(['__ci_vars'], $this->getFlashKeys(), $this->getTempKeys()); - $keys = array_keys($_SESSION); - - foreach ($keys as $key) { - if (! in_array($key, $_exclude, true)) { + foreach (array_keys($_SESSION) as $key) { + if (! in_array($key, $exclude, true)) { $userdata[$key] = $_SESSION[$key]; } } @@ -523,8 +515,8 @@ public function has(string $key): bool /** * Push new value onto session value that is array. * - * @param string $key Identifier of the session property we are interested in. - * @param array $data value to be pushed to existing session key. + * @param string $key Identifier of the session property we are interested in. + * @param array $data value to be pushed to existing session key. * * @return void */ @@ -542,29 +534,27 @@ public function push(string $key, array $data) * identifiers to remove. Otherwise, it is interpreted as the identifier * of a specific session property to remove. * - * @param array|string $key Identifier of the session property or properties to remove. + * @param list|string $key Identifier of the session property or properties to remove. * * @return void */ public function remove($key) { - if (is_array($key)) { - foreach ($key as $k) { - unset($_SESSION[$k]); - } + $key = is_array($key) ? $key : [$key]; - return; + foreach ($key as $k) { + unset($_SESSION[$k]); } - - unset($_SESSION[$key]); } /** * Magic method to set variables in the session by simply calling * $session->foo = bar; * - * @param string $key Identifier of the session property to set. - * @param array|string $value + * @param string $key Identifier of the session property to set. + * @param mixed $value + * + * @return void */ public function __set(string $key, $value) { @@ -577,7 +567,7 @@ public function __set(string $key, $value) * * @param string $key Identifier of the session property to remove. * - * @return string|null + * @return mixed */ public function __get(string $key) { @@ -596,14 +586,15 @@ public function __get(string $key) /** * Magic method to check for session variables. - * Different from has() in that it will validate 'session_id' as well. - * Mostly used by internal PHP functions, users should stick to has() + * + * Different from `has()` in that it will validate 'session_id' as well. + * Mostly used by internal PHP functions, users should stick to `has()`. * * @param string $key Identifier of the session property to remove. */ public function __isset(string $key): bool { - return isset($_SESSION[$key]) || ($key === 'session_id'); + return isset($_SESSION[$key]) || $key === 'session_id'; } /** @@ -615,8 +606,8 @@ public function __isset(string $key): bool * Otherwise, it is interpreted as the identifier of a specific * flashdata property, with $value containing the property value. * - * @param array|string $data Property identifier or associative array of properties - * @param array|bool|float|int|object|string|null $value Property value if $data is a scalar + * @param array|string $data Property identifier or associative array of properties + * @param mixed $value Property value if $data is a scalar * * @return void */ @@ -631,24 +622,27 @@ public function setFlashdata($data, $value = null) * * If the item key is null, return all flashdata. * - * @param string $key Property identifier + * @param string|null $key Property identifier * - * @return array|null The requested property value, or an associative array of them + * @return ($key is string ? mixed : array) */ public function getFlashdata(?string $key = null) { + $_SESSION['__ci_vars'] ??= []; + if (isset($key)) { - return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) - && ! is_int($_SESSION['__ci_vars'][$key])) ? $_SESSION[$key] : null; + if (! isset($_SESSION['__ci_vars'][$key]) || is_int($_SESSION['__ci_vars'][$key])) { + return null; + } + + return $_SESSION[$key] ?? null; } $flashdata = []; - if (isset($_SESSION['__ci_vars'])) { - foreach ($_SESSION['__ci_vars'] as $key => &$value) { - if (! is_int($value)) { - $flashdata[$key] = $_SESSION[$key]; - } + foreach ($_SESSION['__ci_vars'] as $key => $value) { + if (! is_int($value)) { + $flashdata[$key] = $_SESSION[$key]; } } @@ -658,7 +652,7 @@ public function getFlashdata(?string $key = null) /** * Keeps a single piece of flash data alive for one more request. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * * @return void */ @@ -668,33 +662,23 @@ public function keepFlashdata($key) } /** - * Mark a session property or properties as flashdata. - * - * @param array|string $key Property identifier or array of them + * Mark a session property or properties as flashdata. This returns + * `false` if any of the properties were not already set. * - * @return bool False if any of the properties are not already set + * @param list|string $key Property identifier or array of them */ public function markAsFlashdata($key): bool { - if (is_array($key)) { - foreach ($key as $sessionKey) { - if (! isset($_SESSION[$sessionKey])) { - return false; - } - } - - $new = array_fill_keys($key, 'new'); - - $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) ? array_merge($_SESSION['__ci_vars'], $new) : $new; + $keys = is_array($key) ? $key : [$key]; - return true; - } - - if (! isset($_SESSION[$key])) { - return false; + foreach ($keys as $sessionKey) { + if (! isset($_SESSION[$sessionKey])) { + return false; + } } - $_SESSION['__ci_vars'][$key] = 'new'; + $_SESSION['__ci_vars'] ??= []; + $_SESSION['__ci_vars'] = [...$_SESSION['__ci_vars'], ...array_fill_keys($keys, 'new')]; return true; } @@ -702,7 +686,7 @@ public function markAsFlashdata($key): bool /** * Unmark data in the session as flashdata. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * * @return void */ @@ -730,7 +714,7 @@ public function unmarkFlashdata($key) /** * Retrieve all of the keys for session data marked as flashdata. * - * @return array The property names of all flashdata + * @return list */ public function getFlashKeys(): array { @@ -753,9 +737,9 @@ public function getFlashKeys(): array * Sets new data into the session, and marks it as temporary data * with a set lifespan. * - * @param array|string $data Session data key or associative array of items - * @param array|bool|float|int|object|string|null $value Value to store - * @param int $ttl Time-to-live in seconds + * @param array|list|string $data Session data key or associative array of items + * @param mixed $value Value to store + * @param int $ttl Time-to-live in seconds * * @return void */ @@ -769,24 +753,27 @@ public function setTempdata($data, $value = null, int $ttl = 300) * Returns either a single piece of tempdata, or all temp data currently * in the session. * - * @param string $key Session data key + * @param string|null $key Session data key * - * @return array|bool|float|int|object|string|null Session data value or null if not found. + * @return ($key is string ? mixed : array) */ public function getTempdata(?string $key = null) { + $_SESSION['__ci_vars'] ??= []; + if (isset($key)) { - return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) - && is_int($_SESSION['__ci_vars'][$key])) ? $_SESSION[$key] : null; + if (! isset($_SESSION['__ci_vars'][$key]) || ! is_int($_SESSION['__ci_vars'][$key])) { + return null; + } + + return $_SESSION[$key] ?? null; } $tempdata = []; - if (isset($_SESSION['__ci_vars'])) { - foreach ($_SESSION['__ci_vars'] as $key => &$value) { - if (is_int($value)) { - $tempdata[$key] = $_SESSION[$key]; - } + foreach ($_SESSION['__ci_vars'] as $key => $value) { + if (is_int($value)) { + $tempdata[$key] = $_SESSION[$key]; } } @@ -810,10 +797,10 @@ public function removeTempdata(string $key) * Mark one of more pieces of data as being temporary, meaning that * it has a set lifespan within the session. * - * @param array|string $key Property identifier or array of them - * @param int $ttl Time to live, in seconds + * Returns `false` if any of the properties were not set. * - * @return bool False if any of the properties were not set + * @param array|list|string $key Property identifier or array of them + * @param int $ttl Time to live, in seconds */ public function markAsTempdata($key, int $ttl = 300): bool { @@ -858,7 +845,7 @@ public function markAsTempdata($key, int $ttl = 300): bool * Unmarks temporary data in the session, effectively removing its * lifespan and allowing it to live as long as the session does. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * * @return void */ @@ -885,6 +872,8 @@ public function unmarkTempdata($key) /** * Retrieve the keys of all session data that have been marked as temporary data. + * + * @return list */ public function getTempKeys(): array { diff --git a/system/Session/SessionInterface.php b/system/Session/SessionInterface.php index 4efc6f78bed4..fc441c534494 100644 --- a/system/Session/SessionInterface.php +++ b/system/Session/SessionInterface.php @@ -43,8 +43,8 @@ public function destroy(); * If $data is an array, it is expected to be an array of key/value pairs * to be set as session properties. * - * @param array|string $data Property name or associative array of properties - * @param array|bool|float|int|object|string|null $value Property value if single key provided + * @param array|list|string $data Property name or associative array of properties + * @param mixed $value Property value if single key provided * * @return void */ @@ -59,9 +59,9 @@ public function set($data, $value = null); * * Replaces the legacy method $session->userdata(); * - * @param non-empty-string|null $key Identifier of the session property to retrieve + * @param string|null $key Identifier of the session property to retrieve * - * @return array|bool|float|int|object|string|null The property value(s) + * @return ($key is string ? mixed : array) */ public function get(?string $key = null); @@ -79,7 +79,7 @@ public function has(string $key): bool; * identifiers to remove. Otherwise, it is interpreted as the identifier * of a specific session property to remove. * - * @param array|string $key Identifier of the session property or properties to remove. + * @param list|string $key Identifier of the session property or properties to remove. * * @return void */ @@ -94,8 +94,8 @@ public function remove($key); * Otherwise, it is interpreted as the identifier of a specific * flashdata property, with $value containing the property value. * - * @param array|string $data Property identifier or associative array of properties - * @param array|string $value Property value if $data is a scalar + * @param array|string $data Property identifier or associative array of properties + * @param mixed $value Property value if $data is a scalar * * @return void */ @@ -106,35 +106,35 @@ public function setFlashdata($data, $value = null); * * If the item key is null, return all flashdata. * - * @param string $key Property identifier + * @param string|null $key Property identifier * - * @return array|null The requested property value, or an associative - * array of them + * @return ($key is string ? mixed : array) */ public function getFlashdata(?string $key = null); /** * Keeps a single piece of flash data alive for one more request. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * * @return void */ public function keepFlashdata($key); /** - * Mark a session property or properties as flashdata. + * Mark a session property or properties as flashdata. This returns + * `false` if any of the properties were not already set. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * - * @return false if any of the properties are not already set + * @return bool */ public function markAsFlashdata($key); /** * Unmark data in the session as flashdata. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * * @return void */ @@ -143,7 +143,7 @@ public function unmarkFlashdata($key); /** * Retrieve all of the keys for session data marked as flashdata. * - * @return array The property names of all flashdata + * @return list */ public function getFlashKeys(): array; @@ -151,9 +151,9 @@ public function getFlashKeys(): array; * Sets new data into the session, and marks it as temporary data * with a set lifespan. * - * @param array|string $data Session data key or associative array of items - * @param array|bool|float|int|object|string|null $value Value to store - * @param int $ttl Time-to-live in seconds + * @param array|list|string $data Session data key or associative array of items + * @param mixed $value Value to store + * @param int $ttl Time-to-live in seconds * * @return void */ @@ -163,9 +163,9 @@ public function setTempdata($data, $value = null, int $ttl = 300); * Returns either a single piece of tempdata, or all temp data currently * in the session. * - * @param string $key Session data key + * @param string|null $key Session data key * - * @return array|bool|float|int|object|string|null Session data value or null if not found. + * @return ($key is string ? mixed : array) */ public function getTempdata(?string $key = null); @@ -182,10 +182,12 @@ public function removeTempdata(string $key); * Mark one of more pieces of data as being temporary, meaning that * it has a set lifespan within the session. * - * @param array|string $key Property identifier or array of them - * @param int $ttl Time to live, in seconds + * Returns `false` if any of the properties were not set. + * + * @param array|list|string $key Property identifier or array of them + * @param int $ttl Time to live, in seconds * - * @return bool False if any of the properties were not set + * @return bool */ public function markAsTempdata($key, int $ttl = 300); @@ -193,7 +195,7 @@ public function markAsTempdata($key, int $ttl = 300); * Unmarks temporary data in the session, effectively removing its * lifespan and allowing it to live as long as the session does. * - * @param array|string $key Property identifier or array of them + * @param list|string $key Property identifier or array of them * * @return void */ @@ -201,6 +203,8 @@ public function unmarkTempdata($key); /** * Retrieve the keys of all session data that have been marked as temporary data. + * + * @return list */ public function getTempKeys(): array; } diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 298407775ba9..316b3203b5a8 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 3371 errors +# total 3334 errors includes: - argument.type.neon - assign.propertyType.neon diff --git a/utils/phpstan-baseline/method.childReturnType.neon b/utils/phpstan-baseline/method.childReturnType.neon index f443154a490d..f66d38d4d6e4 100644 --- a/utils/phpstan-baseline/method.childReturnType.neon +++ b/utils/phpstan-baseline/method.childReturnType.neon @@ -1,4 +1,4 @@ -# total 38 errors +# total 37 errors parameters: ignoreErrors: @@ -152,11 +152,6 @@ parameters: count: 1 path: ../../system/Model.php - - - message: '#^Return type \(bool\) of method CodeIgniter\\Session\\Session\:\:markAsFlashdata\(\) should be covariant with return type \(false\) of method CodeIgniter\\Session\\SessionInterface\:\:markAsFlashdata\(\)$#' - count: 1 - path: ../../system/Session/Session.php - - message: '#^Return type \(array\{code\: int\|string\|null, message\: string\|null\}\) of method CodeIgniter\\Test\\Mock\\MockConnection\:\:error\(\) should be covariant with return type \(array\\) of method CodeIgniter\\Database\\ConnectionInterface\\:\:error\(\)$#' count: 1 diff --git a/utils/phpstan-baseline/missingType.iterableValue.neon b/utils/phpstan-baseline/missingType.iterableValue.neon index af31bfb997ff..5dfa442af4ab 100644 --- a/utils/phpstan-baseline/missingType.iterableValue.neon +++ b/utils/phpstan-baseline/missingType.iterableValue.neon @@ -1,4 +1,4 @@ -# total 1618 errors +# total 1582 errors parameters: ignoreErrors: @@ -5152,186 +5152,6 @@ parameters: count: 1 path: ../../system/Session/Handlers/BaseHandler.php - - - message: '#^Method CodeIgniter\\Session\\Session\:\:__set\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:getFlashKeys\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:getFlashdata\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:getTempKeys\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:getTempdata\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:keepFlashdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:markAsFlashdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:markAsTempdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:push\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:remove\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:set\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:set\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:setFlashdata\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:setFlashdata\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:setTempdata\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:setTempdata\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:unmarkFlashdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\Session\:\:unmarkTempdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/Session.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:getFlashKeys\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:getFlashdata\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:getTempKeys\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:getTempdata\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:keepFlashdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:markAsFlashdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:markAsTempdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:remove\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:set\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:set\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:setFlashdata\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:setFlashdata\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:setTempdata\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:setTempdata\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:unmarkFlashdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - - - message: '#^Method CodeIgniter\\Session\\SessionInterface\:\:unmarkTempdata\(\) has parameter \$key with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Session/SessionInterface.php - - message: '#^Method CodeIgniter\\Superglobals\:\:__construct\(\) has parameter \$get with no value type specified in iterable type array\.$#' count: 1