diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 52ffa812a315..6db903ddacec 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -35,10 +35,9 @@ trait RequestTrait protected $ipAddress = ''; /** - * Stores values we've retrieved from - * PHP globals. + * Stores values we've retrieved from PHP globals. * - * @var array + * @var array{get?: array, post?: array, request?: array, cookie?: array, server?: array} */ protected $globals = []; @@ -204,22 +203,27 @@ public function getServer($index = null, $filter = null, $flags = null) * @param array|int|null $flags * * @return mixed + * + * @deprecated 4.4.4 This method does not work from the beginning. Use `env()`. */ public function getEnv($index = null, $filter = null, $flags = null) { + // @phpstan-ignore-next-line return $this->fetchGlobal('env', $index, $filter, $flags); } /** * Allows manually setting the value of PHP global, like $_GET, $_POST, etc. * + * @param string $name Supergrlobal name (lowercase) + * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name * @param mixed $value * * @return $this */ - public function setGlobal(string $method, $value) + public function setGlobal(string $name, $value) { - $this->globals[$method] = $value; + $this->globals[$name] = $value; return $this; } @@ -234,19 +238,18 @@ public function setGlobal(string $method, $value) * * http://php.net/manual/en/filter.filters.sanitize.php * - * @param string $method Input filter constant + * @param string $name Supergrlobal name (lowercase) + * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name * @param array|string|null $index * @param int|null $filter Filter constant * @param array|int|null $flags Options * * @return array|bool|float|int|object|string|null */ - public function fetchGlobal(string $method, $index = null, ?int $filter = null, $flags = null) + public function fetchGlobal(string $name, $index = null, ?int $filter = null, $flags = null) { - $method = strtolower($method); - - if (! isset($this->globals[$method])) { - $this->populateGlobals($method); + if (! isset($this->globals[$name])) { + $this->populateGlobals($name); } // Null filters cause null values to return. @@ -257,9 +260,9 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null, if ($index === null) { $values = []; - foreach ($this->globals[$method] as $key => $value) { + foreach ($this->globals[$name] as $key => $value) { $values[$key] = is_array($value) - ? $this->fetchGlobal($method, $key, $filter, $flags) + ? $this->fetchGlobal($name, $key, $filter, $flags) : filter_var($value, $filter, $flags); } @@ -271,7 +274,7 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null, $output = []; foreach ($index as $key) { - $output[$key] = $this->fetchGlobal($method, $key, $filter, $flags); + $output[$key] = $this->fetchGlobal($name, $key, $filter, $flags); } return $output; @@ -279,7 +282,7 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null, // Does the index contain array notation? if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) { - $value = $this->globals[$method]; + $value = $this->globals[$name]; for ($i = 0; $i < $count; $i++) { $key = trim($matches[0][$i], '[]'); @@ -297,7 +300,7 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null, } if (! isset($value)) { - $value = $this->globals[$method][$index] ?? null; + $value = $this->globals[$name][$index] ?? null; } if (is_array($value) @@ -326,20 +329,23 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null, } /** - * Saves a copy of the current state of one of several PHP globals + * Saves a copy of the current state of one of several PHP globals, * so we can retrieve them later. * + * @param string $name Superglobal name (lowercase) + * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name + * * @return void */ - protected function populateGlobals(string $method) + protected function populateGlobals(string $name) { - if (! isset($this->globals[$method])) { - $this->globals[$method] = []; + if (! isset($this->globals[$name])) { + $this->globals[$name] = []; } // Don't populate ENV as it might contain // sensitive data that we don't want to get logged. - switch ($method) { + switch ($name) { case 'get': $this->globals['get'] = $_GET; break; diff --git a/user_guide_src/source/changelogs/v4.4.4.rst b/user_guide_src/source/changelogs/v4.4.4.rst index dfc862d62b62..aca70ba7874e 100644 --- a/user_guide_src/source/changelogs/v4.4.4.rst +++ b/user_guide_src/source/changelogs/v4.4.4.rst @@ -41,6 +41,10 @@ Changes Deprecations ************ +- **Request:** The :php:meth:`CodeIgniter\\HTTP\\Request::getEnv()` method is + deprecated. This method does not work from the beginning. Use :php:func:`env()` + instead. + ********** Bugs Fixed ********** diff --git a/user_guide_src/source/incoming/incomingrequest.rst b/user_guide_src/source/incoming/incomingrequest.rst index c6db5b28fe4c..ba3f12a0a0ec 100644 --- a/user_guide_src/source/incoming/incomingrequest.rst +++ b/user_guide_src/source/incoming/incomingrequest.rst @@ -131,6 +131,9 @@ The ``getServer()`` method will pull from ``$_SERVER``. getEnv() -------- +.. deprecated:: 4.4.4 This method does not work from the beginning. Use + :php:func:`env()` instead. + The ``getEnv()`` method will pull from ``$_ENV``. * ``$request->getEnv()`` diff --git a/user_guide_src/source/incoming/request.rst b/user_guide_src/source/incoming/request.rst index d15da89ec14e..7c00380b9dc9 100644 --- a/user_guide_src/source/incoming/request.rst +++ b/user_guide_src/source/incoming/request.rst @@ -107,6 +107,9 @@ Class Reference .. php:method:: getEnv([$index = null[, $filter = null[, $flags = null]]]) + .. deprecated:: 4.4.4 This method does not work from the beginning. Use + :php:func:`env()` instead. + :param mixed $index: Value name :param int $filter: The type of filter to apply. A list of filters can be found in `PHP manual `__. :param int|array $flags: Flags to apply. A list of flags can be found in `PHP manual `__.