Skip to content

Commit

Permalink
Merge pull request #7982 from pjsde/refactor-session-constant-usage
Browse files Browse the repository at this point in the history
refactor: remove $_SESSION from methods and functions
  • Loading branch information
kenjis authored Oct 3, 2023
2 parents 528200f + b5d123a commit ca50f24
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,7 @@
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 7,
'count' => 6,
'path' => __DIR__ . '/system/HTTP/IncomingRequest.php',
];
$ignoreErrors[] = [
Expand Down
4 changes: 2 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,13 +1064,13 @@ public function storePreviousURL($uri)
}

if (isset($_SESSION)) {
$_SESSION['_ci_previous_url'] = URI::createURIString(
session()->set('_ci_previous_url', URI::createURIString(
$uri->getScheme(),
$uri->getAuthority(),
$uri->getPath(),
$uri->getQuery(),
$uri->getFragment()
);
));
}
}

Expand Down
29 changes: 18 additions & 11 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,35 +827,42 @@ public function getUserAgent()
*/
public function getOldInput(string $key)
{
// If the session hasn't been started, or no
// data was previously saved, we're done.
if (empty($_SESSION['_ci_old_input'])) {
// If the session hasn't been started, we're done.
if (! isset($_SESSION)) {
return null;
}

// Get previously saved in session
$old = session('_ci_old_input');

// If no data was previously saved, we're done.
if ($old === null) {
return null;
}

// Check for the value in the POST array first.
if (isset($_SESSION['_ci_old_input']['post'][$key])) {
return $_SESSION['_ci_old_input']['post'][$key];
if (isset($old['post'][$key])) {
return $old['post'][$key];
}

// Next check in the GET array.
if (isset($_SESSION['_ci_old_input']['get'][$key])) {
return $_SESSION['_ci_old_input']['get'][$key];
if (isset($old['get'][$key])) {
return $old['get'][$key];
}

helper('array');

// Check for an array value in POST.
if (isset($_SESSION['_ci_old_input']['post'])) {
$value = dot_array_search($key, $_SESSION['_ci_old_input']['post']);
if (isset($old['post'])) {
$value = dot_array_search($key, $old['post']);
if ($value !== null) {
return $value;
}
}

// Check for an array value in GET.
if (isset($_SESSION['_ci_old_input']['get'])) {
$value = dot_array_search($key, $_SESSION['_ci_old_input']['get']);
if (isset($old['get'])) {
$value = dot_array_search($key, $old['get']);
if ($value !== null) {
return $value;
}
Expand Down
6 changes: 3 additions & 3 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,12 @@ function set_radio(string $field, string $value = '', bool $default = false): st
*/
function validation_errors()
{
session();
$errors = session('_ci_validation_errors');

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

$validation = Services::validation();
Expand Down
8 changes: 5 additions & 3 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ function previous_url(bool $returnObject = false)
{
// Grab from the session first, if we have it,
// since it's more reliable and safer.
// Otherwise, grab a sanitized version from $_SERVER.
$referer = $_SESSION['_ci_previous_url'] ?? Services::request()->getServer('HTTP_REFERER', FILTER_SANITIZE_URL);
if (isset($_SESSION)) {
$referer = session('_ci_previous_url');
}

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

return $returnObject ? new URI($referer) : $referer;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use Tests\Support\Filters\Customfilter;

/**
* @runTestsInSeparateProcesses
*
* @backupGlobals enabled
*
* @internal
Expand Down
16 changes: 14 additions & 2 deletions tests/system/Helpers/URLHelper/MiscUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ protected function tearDown(): void
$_SERVER = [];
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @group SeparateProcess
*/
public function testPreviousURLUsesSessionFirst(): void
{
$uri1 = 'http://example.com/one?two';
$uri2 = 'http://example.com/two?foo';

$_SERVER['HTTP_REFERER'] = $uri1;
$_SESSION['_ci_previous_url'] = $uri2;
$_SERVER['HTTP_REFERER'] = $uri1;
session()->set('_ci_previous_url', $uri2);

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

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

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @group SeparateProcess
*/
public function testPreviousURLUsesRefererIfNeeded(): void
{
$uri1 = 'http://example.com/one?two';
Expand Down
8 changes: 7 additions & 1 deletion tests/system/View/ParserPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ public function testCurrentURL(): void
$this->assertSame(current_url(), $this->parser->renderString($template));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @group SeparateProcess
*/
public function testPreviousURL(): void
{
$template = '{+ previous_url +}';

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

$this->assertSame(previous_url(), $this->parser->renderString($template));
}
Expand Down

0 comments on commit ca50f24

Please sign in to comment.