Skip to content

Commit ca50f24

Browse files
authored
Merge pull request #7982 from pjsde/refactor-session-constant-usage
refactor: remove $_SESSION from methods and functions
2 parents 528200f + b5d123a commit ca50f24

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed

phpstan-baseline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@
23132313
];
23142314
$ignoreErrors[] = [
23152315
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
2316-
'count' => 7,
2316+
'count' => 6,
23172317
'path' => __DIR__ . '/system/HTTP/IncomingRequest.php',
23182318
];
23192319
$ignoreErrors[] = [

system/CodeIgniter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,13 +1064,13 @@ public function storePreviousURL($uri)
10641064
}
10651065

10661066
if (isset($_SESSION)) {
1067-
$_SESSION['_ci_previous_url'] = URI::createURIString(
1067+
session()->set('_ci_previous_url', URI::createURIString(
10681068
$uri->getScheme(),
10691069
$uri->getAuthority(),
10701070
$uri->getPath(),
10711071
$uri->getQuery(),
10721072
$uri->getFragment()
1073-
);
1073+
));
10741074
}
10751075
}
10761076

system/HTTP/IncomingRequest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -827,35 +827,42 @@ public function getUserAgent()
827827
*/
828828
public function getOldInput(string $key)
829829
{
830-
// If the session hasn't been started, or no
831-
// data was previously saved, we're done.
832-
if (empty($_SESSION['_ci_old_input'])) {
830+
// If the session hasn't been started, we're done.
831+
if (! isset($_SESSION)) {
832+
return null;
833+
}
834+
835+
// Get previously saved in session
836+
$old = session('_ci_old_input');
837+
838+
// If no data was previously saved, we're done.
839+
if ($old === null) {
833840
return null;
834841
}
835842

836843
// Check for the value in the POST array first.
837-
if (isset($_SESSION['_ci_old_input']['post'][$key])) {
838-
return $_SESSION['_ci_old_input']['post'][$key];
844+
if (isset($old['post'][$key])) {
845+
return $old['post'][$key];
839846
}
840847

841848
// Next check in the GET array.
842-
if (isset($_SESSION['_ci_old_input']['get'][$key])) {
843-
return $_SESSION['_ci_old_input']['get'][$key];
849+
if (isset($old['get'][$key])) {
850+
return $old['get'][$key];
844851
}
845852

846853
helper('array');
847854

848855
// Check for an array value in POST.
849-
if (isset($_SESSION['_ci_old_input']['post'])) {
850-
$value = dot_array_search($key, $_SESSION['_ci_old_input']['post']);
856+
if (isset($old['post'])) {
857+
$value = dot_array_search($key, $old['post']);
851858
if ($value !== null) {
852859
return $value;
853860
}
854861
}
855862

856863
// Check for an array value in GET.
857-
if (isset($_SESSION['_ci_old_input']['get'])) {
858-
$value = dot_array_search($key, $_SESSION['_ci_old_input']['get']);
864+
if (isset($old['get'])) {
865+
$value = dot_array_search($key, $old['get']);
859866
if ($value !== null) {
860867
return $value;
861868
}

system/Helpers/form_helper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,12 @@ function set_radio(string $field, string $value = '', bool $default = false): st
701701
*/
702702
function validation_errors()
703703
{
704-
session();
704+
$errors = session('_ci_validation_errors');
705705

706706
// Check the session to see if any were
707707
// passed along from a redirect withErrors() request.
708-
if (isset($_SESSION['_ci_validation_errors']) && (ENVIRONMENT === 'testing' || ! is_cli())) {
709-
return $_SESSION['_ci_validation_errors'];
708+
if ($errors !== null && (ENVIRONMENT === 'testing' || ! is_cli())) {
709+
return $errors;
710710
}
711711

712712
$validation = Services::validation();

system/Helpers/url_helper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ function previous_url(bool $returnObject = false)
8888
{
8989
// Grab from the session first, if we have it,
9090
// since it's more reliable and safer.
91-
// Otherwise, grab a sanitized version from $_SERVER.
92-
$referer = $_SESSION['_ci_previous_url'] ?? Services::request()->getServer('HTTP_REFERER', FILTER_SANITIZE_URL);
91+
if (isset($_SESSION)) {
92+
$referer = session('_ci_previous_url');
93+
}
9394

94-
$referer ??= site_url('/');
95+
// Otherwise, grab a sanitized version from $_SERVER.
96+
$referer ??= request()->getServer('HTTP_REFERER', FILTER_SANITIZE_URL) ?? site_url('/');
9597

9698
return $returnObject ? new URI($referer) : $referer;
9799
}

tests/system/CodeIgniterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Tests\Support\Filters\Customfilter;
2828

2929
/**
30+
* @runTestsInSeparateProcesses
31+
*
3032
* @backupGlobals enabled
3133
*
3234
* @internal

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ protected function tearDown(): void
5252
$_SERVER = [];
5353
}
5454

55+
/**
56+
* @runInSeparateProcess
57+
* @preserveGlobalState disabled
58+
*
59+
* @group SeparateProcess
60+
*/
5561
public function testPreviousURLUsesSessionFirst(): void
5662
{
5763
$uri1 = 'http://example.com/one?two';
5864
$uri2 = 'http://example.com/two?foo';
5965

60-
$_SERVER['HTTP_REFERER'] = $uri1;
61-
$_SESSION['_ci_previous_url'] = $uri2;
66+
$_SERVER['HTTP_REFERER'] = $uri1;
67+
session()->set('_ci_previous_url', $uri2);
6268

6369
$this->config->baseURL = 'http://example.com/public';
6470

@@ -80,6 +86,12 @@ private function createRequest(string $uri): void
8086
Factories::injectMock('config', 'App', $this->config);
8187
}
8288

89+
/**
90+
* @runInSeparateProcess
91+
* @preserveGlobalState disabled
92+
*
93+
* @group SeparateProcess
94+
*/
8395
public function testPreviousURLUsesRefererIfNeeded(): void
8496
{
8597
$uri1 = 'http://example.com/one?two';

tests/system/View/ParserPluginTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ public function testCurrentURL(): void
4242
$this->assertSame(current_url(), $this->parser->renderString($template));
4343
}
4444

45+
/**
46+
* @runInSeparateProcess
47+
* @preserveGlobalState disabled
48+
*
49+
* @group SeparateProcess
50+
*/
4551
public function testPreviousURL(): void
4652
{
4753
$template = '{+ previous_url +}';
4854

4955
// Ensure a previous URL exists to work with.
50-
$_SESSION['_ci_previous_url'] = 'http://example.com/foo';
56+
session()->set('_ci_previous_url', 'http://example.com/foo');
5157

5258
$this->assertSame(previous_url(), $this->parser->renderString($template));
5359
}

0 commit comments

Comments
 (0)