@@ -447,26 +447,22 @@ public function close()
447
447
* If $data is an array, it is expected to be an array of key/value pairs
448
448
* to be set as session properties.
449
449
*
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
452
452
*
453
453
* @return void
454
454
*/
455
455
public function set ($ data , $ value = null )
456
456
{
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 ];
465
458
466
- return ;
459
+ if (array_is_list ($ data )) {
460
+ $ data = array_fill_keys ($ data , null );
467
461
}
468
462
469
- $ _SESSION [$ data ] = $ value ;
463
+ foreach ($ data as $ sessionKey => $ sessionValue ) {
464
+ $ _SESSION [$ sessionKey ] = $ sessionValue ;
465
+ }
470
466
}
471
467
472
468
/**
@@ -478,31 +474,27 @@ public function set($data, $value = null)
478
474
*
479
475
* Replaces the legacy method $session->userdata();
480
476
*
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
482
478
*
483
- * @return array|bool|float|int|object| string|null The property value(s )
479
+ * @return ($key is string ? mixed : array<string, mixed> )
484
480
*/
485
481
public function get (?string $ key = null )
486
482
{
487
- if ($ key !== null && $ key !== '' && (null !== ($ value = $ _SESSION [$ key ] ?? null ) || null !== ($ value = dot_array_search ($ key , $ _SESSION ?? [])))) {
488
- return $ value ;
489
- }
490
-
491
483
if (! isset ($ _SESSION ) || $ _SESSION === []) {
492
484
return $ key === null ? [] : null ;
493
485
}
494
486
495
- if ($ key !== null && $ key !== '' ) {
496
- return null ;
487
+ $ key ??= '' ;
488
+
489
+ if ($ key !== '' ) {
490
+ return $ _SESSION [$ key ] ?? dot_array_search ($ key , $ _SESSION );
497
491
}
498
492
499
493
$ userdata = [];
500
- $ _exclude = array_merge (['__ci_vars ' ], $ this ->getFlashKeys (), $ this ->getTempKeys ());
494
+ $ exclude = array_merge (['__ci_vars ' ], $ this ->getFlashKeys (), $ this ->getTempKeys ());
501
495
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 )) {
506
498
$ userdata [$ key ] = $ _SESSION [$ key ];
507
499
}
508
500
}
@@ -542,29 +534,27 @@ public function push(string $key, array $data)
542
534
* identifiers to remove. Otherwise, it is interpreted as the identifier
543
535
* of a specific session property to remove.
544
536
*
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.
546
538
*
547
539
* @return void
548
540
*/
549
541
public function remove ($ key )
550
542
{
551
- if (is_array ($ key )) {
552
- foreach ($ key as $ k ) {
553
- unset($ _SESSION [$ k ]);
554
- }
543
+ $ key = is_array ($ key ) ? $ key : [$ key ];
555
544
556
- return ;
545
+ foreach ($ key as $ k ) {
546
+ unset($ _SESSION [$ k ]);
557
547
}
558
-
559
- unset($ _SESSION [$ key ]);
560
548
}
561
549
562
550
/**
563
551
* Magic method to set variables in the session by simply calling
564
552
* $session->foo = bar;
565
553
*
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
568
558
*/
569
559
public function __set (string $ key , $ value )
570
560
{
@@ -577,7 +567,7 @@ public function __set(string $key, $value)
577
567
*
578
568
* @param string $key Identifier of the session property to remove.
579
569
*
580
- * @return string|null
570
+ * @return mixed
581
571
*/
582
572
public function __get (string $ key )
583
573
{
@@ -596,14 +586,15 @@ public function __get(string $key)
596
586
597
587
/**
598
588
* 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()`.
601
592
*
602
593
* @param string $key Identifier of the session property to remove.
603
594
*/
604
595
public function __isset (string $ key ): bool
605
596
{
606
- return isset ($ _SESSION [$ key ]) || ( $ key === 'session_id ' ) ;
597
+ return isset ($ _SESSION [$ key ]) || $ key === 'session_id ' ;
607
598
}
608
599
609
600
/**
@@ -615,8 +606,8 @@ public function __isset(string $key): bool
615
606
* Otherwise, it is interpreted as the identifier of a specific
616
607
* flashdata property, with $value containing the property value.
617
608
*
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
620
611
*
621
612
* @return void
622
613
*/
@@ -631,24 +622,27 @@ public function setFlashdata($data, $value = null)
631
622
*
632
623
* If the item key is null, return all flashdata.
633
624
*
634
- * @param string $key Property identifier
625
+ * @param string|null $key Property identifier
635
626
*
636
- * @return array|null The requested property value, or an associative array of them
627
+ * @return ($key is string ? mixed : array<string, mixed>)
637
628
*/
638
629
public function getFlashdata (?string $ key = null )
639
630
{
631
+ $ _SESSION ['__ci_vars ' ] ??= [];
632
+
640
633
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 ;
643
639
}
644
640
645
641
$ flashdata = [];
646
642
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 ];
652
646
}
653
647
}
654
648
@@ -658,7 +652,7 @@ public function getFlashdata(?string $key = null)
658
652
/**
659
653
* Keeps a single piece of flash data alive for one more request.
660
654
*
661
- * @param array |string $key Property identifier or array of them
655
+ * @param list<string> |string $key Property identifier or array of them
662
656
*
663
657
* @return void
664
658
*/
@@ -668,41 +662,31 @@ public function keepFlashdata($key)
668
662
}
669
663
670
664
/**
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.
674
667
*
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
676
669
*/
677
670
public function markAsFlashdata ($ key ): bool
678
671
{
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 ' ] ??= [];
689
674
690
- return true ;
691
- }
675
+ foreach ($ keys as $ sessionKey ) {
676
+ if (! isset ($ _SESSION [$ sessionKey ])) {
677
+ return false ;
678
+ }
692
679
693
- if (! isset ($ _SESSION [$ key ])) {
694
- return false ;
680
+ $ _SESSION ['__ci_vars ' ][$ sessionKey ] = 'new ' ;
695
681
}
696
682
697
- $ _SESSION ['__ci_vars ' ][$ key ] = 'new ' ;
698
-
699
683
return true ;
700
684
}
701
685
702
686
/**
703
687
* Unmark data in the session as flashdata.
704
688
*
705
- * @param array |string $key Property identifier or array of them
689
+ * @param list<string> |string $key Property identifier or array of them
706
690
*
707
691
* @return void
708
692
*/
@@ -730,7 +714,7 @@ public function unmarkFlashdata($key)
730
714
/**
731
715
* Retrieve all of the keys for session data marked as flashdata.
732
716
*
733
- * @return array The property names of all flashdata
717
+ * @return list<string>
734
718
*/
735
719
public function getFlashKeys (): array
736
720
{
@@ -753,9 +737,9 @@ public function getFlashKeys(): array
753
737
* Sets new data into the session, and marks it as temporary data
754
738
* with a set lifespan.
755
739
*
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
759
743
*
760
744
* @return void
761
745
*/
@@ -769,24 +753,27 @@ public function setTempdata($data, $value = null, int $ttl = 300)
769
753
* Returns either a single piece of tempdata, or all temp data currently
770
754
* in the session.
771
755
*
772
- * @param string $key Session data key
756
+ * @param string|null $key Session data key
773
757
*
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>)
775
759
*/
776
760
public function getTempdata (?string $ key = null )
777
761
{
762
+ $ _SESSION ['__ci_vars ' ] ??= [];
763
+
778
764
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 ;
781
770
}
782
771
783
772
$ tempdata = [];
784
773
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 ];
790
777
}
791
778
}
792
779
@@ -810,10 +797,10 @@ public function removeTempdata(string $key)
810
797
* Mark one of more pieces of data as being temporary, meaning that
811
798
* it has a set lifespan within the session.
812
799
*
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.
815
801
*
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
817
804
*/
818
805
public function markAsTempdata ($ key , int $ ttl = 300 ): bool
819
806
{
@@ -858,7 +845,7 @@ public function markAsTempdata($key, int $ttl = 300): bool
858
845
* Unmarks temporary data in the session, effectively removing its
859
846
* lifespan and allowing it to live as long as the session does.
860
847
*
861
- * @param array |string $key Property identifier or array of them
848
+ * @param list<string> |string $key Property identifier or array of them
862
849
*
863
850
* @return void
864
851
*/
@@ -885,6 +872,8 @@ public function unmarkTempdata($key)
885
872
886
873
/**
887
874
* Retrieve the keys of all session data that have been marked as temporary data.
875
+ *
876
+ * @return list<string>
888
877
*/
889
878
public function getTempKeys (): array
890
879
{
0 commit comments