Skip to content

Commit cfc7ea7

Browse files
authored
Merge pull request #8234 from kenjis/fix-request-getEnv
fix: make Request::getEnv() deprecated
2 parents 5412fe4 + fe64ca0 commit cfc7ea7

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

system/HTTP/RequestTrait.php

+27-21
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ trait RequestTrait
3535
protected $ipAddress = '';
3636

3737
/**
38-
* Stores values we've retrieved from
39-
* PHP globals.
38+
* Stores values we've retrieved from PHP globals.
4039
*
41-
* @var array
40+
* @var array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
4241
*/
4342
protected $globals = [];
4443

@@ -204,22 +203,27 @@ public function getServer($index = null, $filter = null, $flags = null)
204203
* @param array|int|null $flags
205204
*
206205
* @return mixed
206+
*
207+
* @deprecated 4.4.4 This method does not work from the beginning. Use `env()`.
207208
*/
208209
public function getEnv($index = null, $filter = null, $flags = null)
209210
{
211+
// @phpstan-ignore-next-line
210212
return $this->fetchGlobal('env', $index, $filter, $flags);
211213
}
212214

213215
/**
214216
* Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
215217
*
218+
* @param string $name Supergrlobal name (lowercase)
219+
* @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
216220
* @param mixed $value
217221
*
218222
* @return $this
219223
*/
220-
public function setGlobal(string $method, $value)
224+
public function setGlobal(string $name, $value)
221225
{
222-
$this->globals[$method] = $value;
226+
$this->globals[$name] = $value;
223227

224228
return $this;
225229
}
@@ -234,19 +238,18 @@ public function setGlobal(string $method, $value)
234238
*
235239
* http://php.net/manual/en/filter.filters.sanitize.php
236240
*
237-
* @param string $method Input filter constant
241+
* @param string $name Supergrlobal name (lowercase)
242+
* @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
238243
* @param array|string|null $index
239244
* @param int|null $filter Filter constant
240245
* @param array|int|null $flags Options
241246
*
242247
* @return array|bool|float|int|object|string|null
243248
*/
244-
public function fetchGlobal(string $method, $index = null, ?int $filter = null, $flags = null)
249+
public function fetchGlobal(string $name, $index = null, ?int $filter = null, $flags = null)
245250
{
246-
$method = strtolower($method);
247-
248-
if (! isset($this->globals[$method])) {
249-
$this->populateGlobals($method);
251+
if (! isset($this->globals[$name])) {
252+
$this->populateGlobals($name);
250253
}
251254

252255
// Null filters cause null values to return.
@@ -257,9 +260,9 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
257260
if ($index === null) {
258261
$values = [];
259262

260-
foreach ($this->globals[$method] as $key => $value) {
263+
foreach ($this->globals[$name] as $key => $value) {
261264
$values[$key] = is_array($value)
262-
? $this->fetchGlobal($method, $key, $filter, $flags)
265+
? $this->fetchGlobal($name, $key, $filter, $flags)
263266
: filter_var($value, $filter, $flags);
264267
}
265268

@@ -271,15 +274,15 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
271274
$output = [];
272275

273276
foreach ($index as $key) {
274-
$output[$key] = $this->fetchGlobal($method, $key, $filter, $flags);
277+
$output[$key] = $this->fetchGlobal($name, $key, $filter, $flags);
275278
}
276279

277280
return $output;
278281
}
279282

280283
// Does the index contain array notation?
281284
if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) {
282-
$value = $this->globals[$method];
285+
$value = $this->globals[$name];
283286

284287
for ($i = 0; $i < $count; $i++) {
285288
$key = trim($matches[0][$i], '[]');
@@ -297,7 +300,7 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
297300
}
298301

299302
if (! isset($value)) {
300-
$value = $this->globals[$method][$index] ?? null;
303+
$value = $this->globals[$name][$index] ?? null;
301304
}
302305

303306
if (is_array($value)
@@ -326,20 +329,23 @@ public function fetchGlobal(string $method, $index = null, ?int $filter = null,
326329
}
327330

328331
/**
329-
* Saves a copy of the current state of one of several PHP globals
332+
* Saves a copy of the current state of one of several PHP globals,
330333
* so we can retrieve them later.
331334
*
335+
* @param string $name Superglobal name (lowercase)
336+
* @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
337+
*
332338
* @return void
333339
*/
334-
protected function populateGlobals(string $method)
340+
protected function populateGlobals(string $name)
335341
{
336-
if (! isset($this->globals[$method])) {
337-
$this->globals[$method] = [];
342+
if (! isset($this->globals[$name])) {
343+
$this->globals[$name] = [];
338344
}
339345

340346
// Don't populate ENV as it might contain
341347
// sensitive data that we don't want to get logged.
342-
switch ($method) {
348+
switch ($name) {
343349
case 'get':
344350
$this->globals['get'] = $_GET;
345351
break;

user_guide_src/source/changelogs/v4.4.4.rst

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Changes
4141
Deprecations
4242
************
4343

44+
- **Request:** The :php:meth:`CodeIgniter\\HTTP\\Request::getEnv()` method is
45+
deprecated. This method does not work from the beginning. Use :php:func:`env()`
46+
instead.
47+
4448
**********
4549
Bugs Fixed
4650
**********

user_guide_src/source/incoming/incomingrequest.rst

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ The ``getServer()`` method will pull from ``$_SERVER``.
131131
getEnv()
132132
--------
133133

134+
.. deprecated:: 4.4.4 This method does not work from the beginning. Use
135+
:php:func:`env()` instead.
136+
134137
The ``getEnv()`` method will pull from ``$_ENV``.
135138

136139
* ``$request->getEnv()``

user_guide_src/source/incoming/request.rst

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ Class Reference
107107

108108
.. php:method:: getEnv([$index = null[, $filter = null[, $flags = null]]])
109109
110+
.. deprecated:: 4.4.4 This method does not work from the beginning. Use
111+
:php:func:`env()` instead.
112+
110113
:param mixed $index: Value name
111114
: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>`__.
112115
: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>`__.

0 commit comments

Comments
 (0)