From a841e1de3d1841ebb3ce0329b1392f4716ec9026 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 20 Nov 2023 09:21:39 +0900 Subject: [PATCH 1/3] refactor: rename param names --- system/HTTP/RequestTrait.php | 45 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 52ffa812a315..0acfbe184b43 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 = []; @@ -213,13 +212,15 @@ public function getEnv($index = null, $filter = null, $flags = null) /** * 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 +235,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 +257,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 +271,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 +279,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 +297,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 +326,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 Supergrlobal 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; From cbb096727367ce8a526d83a2948b679f0b609519 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 20 Nov 2023 09:33:23 +0900 Subject: [PATCH 2/3] docs: make Request::getEnv() deprecated See https://github.com/codeigniter4/CodeIgniter4/commit/b0975ec5dba#diff-fb41662a8e01f56cacc449d12f5d386be73835879b2f49f452cb2aef8ac03466R441-R442 --- system/HTTP/RequestTrait.php | 3 +++ user_guide_src/source/changelogs/v4.4.4.rst | 4 ++++ user_guide_src/source/incoming/incomingrequest.rst | 3 +++ user_guide_src/source/incoming/request.rst | 3 +++ 4 files changed, 13 insertions(+) diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 0acfbe184b43..01ff12c73ca4 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -203,9 +203,12 @@ 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); } 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 `__. From fe64ca018ecfbfac4a68af46777e67c8e0800fa9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 20 Nov 2023 12:06:43 +0900 Subject: [PATCH 3/3] docs: fix typo Co-authored-by: John Paul E. Balandan, CPA --- system/HTTP/RequestTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 01ff12c73ca4..6db903ddacec 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -332,7 +332,7 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f * Saves a copy of the current state of one of several PHP globals, * so we can retrieve them later. * - * @param string $name Supergrlobal name (lowercase) + * @param string $name Superglobal name (lowercase) * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name * * @return void