Skip to content

Commit afc2ca8

Browse files
committed
refactor: fix Session and SessionInterface code
1 parent 8c6da93 commit afc2ca8

File tree

5 files changed

+110
-297
lines changed

5 files changed

+110
-297
lines changed

system/Session/Session.php

+78-89
Original file line numberDiff line numberDiff line change
@@ -447,26 +447,22 @@ public function close()
447447
* If $data is an array, it is expected to be an array of key/value pairs
448448
* to be set as session properties.
449449
*
450-
* @param array|string $data Property name or associative array of properties
451-
* @param array|bool|float|int|object|string|null $value Property value if single key provided
450+
* @param array<string, mixed>|list<string>|string $data Property name or associative array of properties
451+
* @param mixed $value Property value if single key provided
452452
*
453453
* @return void
454454
*/
455455
public function set($data, $value = null)
456456
{
457-
if (is_array($data)) {
458-
foreach ($data as $key => &$value) {
459-
if (is_int($key)) {
460-
$_SESSION[$value] = null;
461-
} else {
462-
$_SESSION[$key] = $value;
463-
}
464-
}
457+
$data = is_array($data) ? $data : [$data => $value];
465458

466-
return;
459+
if (array_is_list($data)) {
460+
$data = array_fill_keys($data, null);
467461
}
468462

469-
$_SESSION[$data] = $value;
463+
foreach ($data as $sessionKey => $sessionValue) {
464+
$_SESSION[$sessionKey] = $sessionValue;
465+
}
470466
}
471467

472468
/**
@@ -478,31 +474,27 @@ public function set($data, $value = null)
478474
*
479475
* Replaces the legacy method $session->userdata();
480476
*
481-
* @param non-empty-string|null $key Identifier of the session property to retrieve
477+
* @param string|null $key Identifier of the session property to retrieve
482478
*
483-
* @return array|bool|float|int|object|string|null The property value(s)
479+
* @return ($key is string ? mixed : array<string, mixed>)
484480
*/
485481
public function get(?string $key = null)
486482
{
487-
if ($key !== null && $key !== '' && (null !== ($value = $_SESSION[$key] ?? null) || null !== ($value = dot_array_search($key, $_SESSION ?? [])))) {
488-
return $value;
489-
}
490-
491483
if (! isset($_SESSION) || $_SESSION === []) {
492484
return $key === null ? [] : null;
493485
}
494486

495-
if ($key !== null && $key !== '') {
496-
return null;
487+
$key ??= '';
488+
489+
if ($key !== '') {
490+
return $_SESSION[$key] ?? dot_array_search($key, $_SESSION);
497491
}
498492

499493
$userdata = [];
500-
$_exclude = array_merge(['__ci_vars'], $this->getFlashKeys(), $this->getTempKeys());
494+
$exclude = array_merge(['__ci_vars'], $this->getFlashKeys(), $this->getTempKeys());
501495

502-
$keys = array_keys($_SESSION);
503-
504-
foreach ($keys as $key) {
505-
if (! in_array($key, $_exclude, true)) {
496+
foreach (array_keys($_SESSION) as $key) {
497+
if (! in_array($key, $exclude, true)) {
506498
$userdata[$key] = $_SESSION[$key];
507499
}
508500
}
@@ -542,29 +534,27 @@ public function push(string $key, array $data)
542534
* identifiers to remove. Otherwise, it is interpreted as the identifier
543535
* of a specific session property to remove.
544536
*
545-
* @param array|string $key Identifier of the session property or properties to remove.
537+
* @param list<string>|string $key Identifier of the session property or properties to remove.
546538
*
547539
* @return void
548540
*/
549541
public function remove($key)
550542
{
551-
if (is_array($key)) {
552-
foreach ($key as $k) {
553-
unset($_SESSION[$k]);
554-
}
543+
$key = is_array($key) ? $key : [$key];
555544

556-
return;
545+
foreach ($key as $k) {
546+
unset($_SESSION[$k]);
557547
}
558-
559-
unset($_SESSION[$key]);
560548
}
561549

562550
/**
563551
* Magic method to set variables in the session by simply calling
564552
* $session->foo = bar;
565553
*
566-
* @param string $key Identifier of the session property to set.
567-
* @param array|string $value
554+
* @param string $key Identifier of the session property to set.
555+
* @param mixed $value
556+
*
557+
* @return void
568558
*/
569559
public function __set(string $key, $value)
570560
{
@@ -577,7 +567,7 @@ public function __set(string $key, $value)
577567
*
578568
* @param string $key Identifier of the session property to remove.
579569
*
580-
* @return string|null
570+
* @return mixed
581571
*/
582572
public function __get(string $key)
583573
{
@@ -596,14 +586,15 @@ public function __get(string $key)
596586

597587
/**
598588
* Magic method to check for session variables.
599-
* Different from has() in that it will validate 'session_id' as well.
600-
* Mostly used by internal PHP functions, users should stick to has()
589+
*
590+
* Different from `has()` in that it will validate 'session_id' as well.
591+
* Mostly used by internal PHP functions, users should stick to `has()`.
601592
*
602593
* @param string $key Identifier of the session property to remove.
603594
*/
604595
public function __isset(string $key): bool
605596
{
606-
return isset($_SESSION[$key]) || ($key === 'session_id');
597+
return isset($_SESSION[$key]) || $key === 'session_id';
607598
}
608599

609600
/**
@@ -615,8 +606,8 @@ public function __isset(string $key): bool
615606
* Otherwise, it is interpreted as the identifier of a specific
616607
* flashdata property, with $value containing the property value.
617608
*
618-
* @param array|string $data Property identifier or associative array of properties
619-
* @param array|bool|float|int|object|string|null $value Property value if $data is a scalar
609+
* @param array<string, mixed>|string $data Property identifier or associative array of properties
610+
* @param mixed $value Property value if $data is a scalar
620611
*
621612
* @return void
622613
*/
@@ -631,24 +622,27 @@ public function setFlashdata($data, $value = null)
631622
*
632623
* If the item key is null, return all flashdata.
633624
*
634-
* @param string $key Property identifier
625+
* @param string|null $key Property identifier
635626
*
636-
* @return array|null The requested property value, or an associative array of them
627+
* @return ($key is string ? mixed : array<string, mixed>)
637628
*/
638629
public function getFlashdata(?string $key = null)
639630
{
631+
$_SESSION['__ci_vars'] ??= [];
632+
640633
if (isset($key)) {
641-
return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key])
642-
&& ! is_int($_SESSION['__ci_vars'][$key])) ? $_SESSION[$key] : null;
634+
if (! isset($_SESSION['__ci_vars'][$key]) || is_int($_SESSION['__ci_vars'][$key])) {
635+
return null;
636+
}
637+
638+
return $_SESSION[$key] ?? null;
643639
}
644640

645641
$flashdata = [];
646642

647-
if (isset($_SESSION['__ci_vars'])) {
648-
foreach ($_SESSION['__ci_vars'] as $key => &$value) {
649-
if (! is_int($value)) {
650-
$flashdata[$key] = $_SESSION[$key];
651-
}
643+
foreach ($_SESSION['__ci_vars'] as $key => $value) {
644+
if (! is_int($value)) {
645+
$flashdata[$key] = $_SESSION[$key];
652646
}
653647
}
654648

@@ -658,7 +652,7 @@ public function getFlashdata(?string $key = null)
658652
/**
659653
* Keeps a single piece of flash data alive for one more request.
660654
*
661-
* @param array|string $key Property identifier or array of them
655+
* @param list<string>|string $key Property identifier or array of them
662656
*
663657
* @return void
664658
*/
@@ -668,41 +662,31 @@ public function keepFlashdata($key)
668662
}
669663

670664
/**
671-
* Mark a session property or properties as flashdata.
672-
*
673-
* @param array|string $key Property identifier or array of them
665+
* Mark a session property or properties as flashdata. This returns
666+
* `false` if any of the properties were not already set.
674667
*
675-
* @return bool False if any of the properties are not already set
668+
* @param list<string>|string $key Property identifier or array of them
676669
*/
677670
public function markAsFlashdata($key): bool
678671
{
679-
if (is_array($key)) {
680-
foreach ($key as $sessionKey) {
681-
if (! isset($_SESSION[$sessionKey])) {
682-
return false;
683-
}
684-
}
685-
686-
$new = array_fill_keys($key, 'new');
687-
688-
$_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) ? array_merge($_SESSION['__ci_vars'], $new) : $new;
672+
$keys = is_array($key) ? $key : [$key];
673+
$_SESSION['__ci_vars'] ??= [];
689674

690-
return true;
691-
}
675+
foreach ($keys as $sessionKey) {
676+
if (! isset($_SESSION[$sessionKey])) {
677+
return false;
678+
}
692679

693-
if (! isset($_SESSION[$key])) {
694-
return false;
680+
$_SESSION['__ci_vars'][$sessionKey] = 'new';
695681
}
696682

697-
$_SESSION['__ci_vars'][$key] = 'new';
698-
699683
return true;
700684
}
701685

702686
/**
703687
* Unmark data in the session as flashdata.
704688
*
705-
* @param array|string $key Property identifier or array of them
689+
* @param list<string>|string $key Property identifier or array of them
706690
*
707691
* @return void
708692
*/
@@ -730,7 +714,7 @@ public function unmarkFlashdata($key)
730714
/**
731715
* Retrieve all of the keys for session data marked as flashdata.
732716
*
733-
* @return array The property names of all flashdata
717+
* @return list<string>
734718
*/
735719
public function getFlashKeys(): array
736720
{
@@ -753,9 +737,9 @@ public function getFlashKeys(): array
753737
* Sets new data into the session, and marks it as temporary data
754738
* with a set lifespan.
755739
*
756-
* @param array|string $data Session data key or associative array of items
757-
* @param array|bool|float|int|object|string|null $value Value to store
758-
* @param int $ttl Time-to-live in seconds
740+
* @param array<string, mixed>|list<string>|string $data Session data key or associative array of items
741+
* @param mixed $value Value to store
742+
* @param int $ttl Time-to-live in seconds
759743
*
760744
* @return void
761745
*/
@@ -769,24 +753,27 @@ public function setTempdata($data, $value = null, int $ttl = 300)
769753
* Returns either a single piece of tempdata, or all temp data currently
770754
* in the session.
771755
*
772-
* @param string $key Session data key
756+
* @param string|null $key Session data key
773757
*
774-
* @return array|bool|float|int|object|string|null Session data value or null if not found.
758+
* @return ($key is string ? mixed : array<string, mixed>)
775759
*/
776760
public function getTempdata(?string $key = null)
777761
{
762+
$_SESSION['__ci_vars'] ??= [];
763+
778764
if (isset($key)) {
779-
return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key])
780-
&& is_int($_SESSION['__ci_vars'][$key])) ? $_SESSION[$key] : null;
765+
if (! isset($_SESSION['__ci_vars'][$key]) || ! is_int($_SESSION['__ci_vars'][$key])) {
766+
return null;
767+
}
768+
769+
return $_SESSION[$key] ?? null;
781770
}
782771

783772
$tempdata = [];
784773

785-
if (isset($_SESSION['__ci_vars'])) {
786-
foreach ($_SESSION['__ci_vars'] as $key => &$value) {
787-
if (is_int($value)) {
788-
$tempdata[$key] = $_SESSION[$key];
789-
}
774+
foreach ($_SESSION['__ci_vars'] as $key => $value) {
775+
if (is_int($value)) {
776+
$tempdata[$key] = $_SESSION[$key];
790777
}
791778
}
792779

@@ -810,10 +797,10 @@ public function removeTempdata(string $key)
810797
* Mark one of more pieces of data as being temporary, meaning that
811798
* it has a set lifespan within the session.
812799
*
813-
* @param array|string $key Property identifier or array of them
814-
* @param int $ttl Time to live, in seconds
800+
* Returns `false` if any of the properties were not set.
815801
*
816-
* @return bool False if any of the properties were not set
802+
* @param array<string, mixed>|list<string>|string $key Property identifier or array of them
803+
* @param int $ttl Time to live, in seconds
817804
*/
818805
public function markAsTempdata($key, int $ttl = 300): bool
819806
{
@@ -858,7 +845,7 @@ public function markAsTempdata($key, int $ttl = 300): bool
858845
* Unmarks temporary data in the session, effectively removing its
859846
* lifespan and allowing it to live as long as the session does.
860847
*
861-
* @param array|string $key Property identifier or array of them
848+
* @param list<string>|string $key Property identifier or array of them
862849
*
863850
* @return void
864851
*/
@@ -885,6 +872,8 @@ public function unmarkTempdata($key)
885872

886873
/**
887874
* Retrieve the keys of all session data that have been marked as temporary data.
875+
*
876+
* @return list<string>
888877
*/
889878
public function getTempKeys(): array
890879
{

0 commit comments

Comments
 (0)