Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make Request::getEnv() deprecated #8234

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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