Skip to content

Commit

Permalink
Merge pull request #8234 from kenjis/fix-request-getEnv
Browse files Browse the repository at this point in the history
fix: make Request::getEnv() deprecated
  • Loading branch information
kenjis authored Nov 22, 2023
2 parents 5412fe4 + fe64ca0 commit cfc7ea7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
48 changes: 27 additions & 21 deletions system/HTTP/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
}
Expand All @@ -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.
Expand All @@ -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);
}

Expand All @@ -271,15 +274,15 @@ 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;
}

// 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], '[]');
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/changelogs/v4.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
**********
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/incoming/incomingrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()``
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/incoming/request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.php.net/manual/en/filter.filters.php>`__.
:param int|array $flags: Flags to apply. A list of flags can be found in `PHP manual <https://www.php.net/manual/en/filter.filters.flags.php>`__.
Expand Down

0 comments on commit cfc7ea7

Please sign in to comment.