SessionFunctions   D
last analyzed

Complexity

Total Complexity 57

Size/Duplication

Total Lines 694
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 57
lcom 0
cbo 0
dl 0
loc 694
ccs 175
cts 175
cp 1
rs 4.946
c 0
b 0
f 0

57 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 44 1
A getRetrieveCallable() 0 4 1
A getPutCallable() 0 4 1
A getHasCallable() 0 4 1
A getDeleteCallable() 0 4 1
A getIteratorCallable() 0 4 1
A getAbortCallable() 0 4 1
A getCacheExpireCallable() 0 4 1
A getCacheLimiterCallable() 0 4 1
A getCreateIdCallable() 0 4 1
A getDecodeCallable() 0 4 1
A getDestroyCallable() 0 4 1
A getEncodeCallable() 0 4 1
A getGcCallable() 0 4 1
A getGetCookieParamsCallable() 0 4 1
A getIdCallable() 0 4 1
A getModuleNameCallable() 0 4 1
A getNameCallable() 0 4 1
A getRegenerateIdCallable() 0 4 1
A getRegisterShutdownCallable() 0 4 1
A getResetCallable() 0 4 1
A getSavePathCallable() 0 4 1
A getSetCookieParamsCallable() 0 4 1
A getSetSaveHandlerCallable() 0 4 1
A getStartCallable() 0 4 1
A getStatusCallable() 0 4 1
A getUnsetCallable() 0 4 1
A getWriteCloseCallable() 0 4 1
A getCouldBeStartedCallable() 0 4 1
A setRetrieveCallable() 0 6 1
A setPutCallable() 0 6 1
A setHasCallable() 0 6 1
A setDeleteCallable() 0 6 1
A setIteratorCallable() 0 6 1
A setAbortCallable() 0 6 1
A setCacheExpireCallable() 0 6 1
A setCacheLimiterCallable() 0 6 1
A setCreateIdCallable() 0 6 1
A setDecodeCallable() 0 6 1
A setDestroyCallable() 0 6 1
A setEncodeCallable() 0 6 1
A setGcCallable() 0 6 1
A setGetCookieParamsCallable() 0 6 1
A setIdCallable() 0 6 1
A setModuleNameCallable() 0 6 1
A setNameCallable() 0 6 1
A setRegenerateIdCallable() 0 6 1
A setRegisterShutdownCallable() 0 6 1
A setResetCallable() 0 6 1
A setSavePathCallable() 0 6 1
A setSetCookieParamsCallable() 0 6 1
A setSetSaveHandlerCallable() 0 6 1
A setStartCallable() 0 6 1
A setStatusCallable() 0 6 1
A setUnsetCallable() 0 6 1
A setWriteCloseCallable() 0 6 1
A setCouldBeStartedCallable() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like SessionFunctions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SessionFunctions, and based on these observations, apply Extract Interface, too.

1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Session;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use ArrayIterator;
22
use Iterator;
23
use Limoncello\Application\Contracts\Session\SessionFunctionsInterface;
24
use function array_key_exists;
25
use function session_status;
26
27
/**
28
 * @package Limoncello\Application
29
 *
30
 * @SuppressWarnings(PHPMD.Superglobals)
31
 * @SuppressWarnings(PHPMD.LongVariable)
32
 * @SuppressWarnings(PHPMD.TooManyFields)
33
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
34
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
35
 */
36
class SessionFunctions implements SessionFunctionsInterface
37
{
38
    /**
39
     * @var callable
40
     */
41
    private $retrieveCallable;
42
43
    /**
44
     * @var callable
45
     */
46
    private $putCallable;
47
48
    /**
49
     * @var callable
50
     */
51
    private $hasCallable;
52
53
    /**
54
     * @var callable
55
     */
56
    private $deleteCallable;
57
58
    /**
59
     * @var callable
60
     */
61
    private $iteratorCallable;
62
63
    /**
64
     * @var callable
65
     */
66
    private $abortCallable;
67
68
    /**
69
     * @var callable
70
     */
71
    private $cacheLimiterCallable;
72
73
    /**
74
     * @var callable
75
     */
76
    private $createIdCallable;
77
78
    /**
79
     * @var callable
80
     */
81
    private $writeCloseCallable;
82
83
    /**
84
     * @var callable
85
     */
86
    private $unsetCallable;
87
88
    /**
89
     * @var callable
90
     */
91
    private $statusCallable;
92
93
    /**
94
     * @var callable
95
     */
96
    private $startCallable;
97
98
    /**
99
     * @var callable
100
     */
101
    private $setSaveHandlerCallable;
102
103
    /**
104
     * @var callable
105
     */
106
    private $setCookieParamsCallable;
107
108
    /**
109
     * @var callable
110
     */
111
    private $savePathCallable;
112
113
    /**
114
     * @var callable
115
     */
116
    private $resetCallable;
117
118
    /**
119
     * @var callable
120
     */
121
    private $registerShutdownCallable;
122
123
    /**
124
     * @var callable
125
     */
126
    private $regenerateIdCallable;
127
128
    /**
129
     * @var callable
130
     */
131
    private $nameCallable;
132
133
    /**
134
     * @var callable
135
     */
136
    private $moduleNameCallable;
137
138
    /**
139
     * @var callable
140
     */
141
    private $decodeCallable;
142
143
    /**
144
     * @var callable
145
     */
146
    private $destroyCallable;
147
148
    /**
149
     * @var callable
150
     */
151
    private $encodeCallable;
152
153
    /**
154
     * @var callable
155
     */
156
    private $gcCallable;
157
158
    /**
159
     * @var callable
160
     */
161
    private $getCookieParamsCallable;
162
163
    /**
164
     * @var callable
165
     */
166
    private $idCallable;
167
168
    /**
169
     * @var callable
170
     */
171
    private $cacheExpireCallable;
172
173
    /**
174
     * @var callable
175
     */
176 3
    private $couldBeStartedCallable;
177
178
    /**
179
     * Constructor.
180 1
     */
181 3
    public function __construct()
182
    {
183 1
        $this
184 3
            ->setRetrieveCallable(function (string $key) {
185
                return $_SESSION[$key];
186 1
            })
187 3
            ->setPutCallable(function (string $key, $serializable): void {
188
                $_SESSION[$key] = $serializable;
189 1
            })
190 3
            ->setHasCallable(function (string $key): bool {
191
                return array_key_exists($key, $_SESSION);
192 1
            })
193 3
            ->setDeleteCallable(function (string $key): void {
194
                unset($_SESSION[$key]);
195 1
            })
196 3
            ->setIteratorCallable(function (): Iterator {
197 3
                return new ArrayIterator($_SESSION);
198 3
            })
199 3
            ->setCouldBeStartedCallable(function (): bool {
200 3
                return session_status() === PHP_SESSION_NONE;
201 3
            })
202 3
            ->setAbortCallable('\session_abort')
203 3
            ->setCacheLimiterCallable('\session_cache_limiter')
204 3
            ->setCreateIdCallable('\session_create_id')
205 3
            ->setWriteCloseCallable('\session_write_close')
206 3
            ->setUnsetCallable('\session_unset')
207 3
            ->setStatusCallable('\session_status')
208 3
            ->setStartCallable('\session_start')
209 3
            ->setSetSaveHandlerCallable('\session_set_save_handler')
210 3
            ->setSetCookieParamsCallable('\session_set_cookie_params')
211 3
            ->setSavePathCallable('\session_save_path')
212 3
            ->setResetCallable('\session_reset')
213 3
            ->setRegisterShutdownCallable('\session_register_shutdown')
214 3
            ->setRegenerateIdCallable('\session_regenerate_id')
215 3
            ->setNameCallable('\session_name')
216 3
            ->setModuleNameCallable('\session_module_name')
217 3
            ->setDecodeCallable('\session_decode')
218 3
            ->setDestroyCallable('\session_destroy')
219
            ->setEncodeCallable('\session_encode')
220
            ->setGcCallable('\session_gc')
221
            ->setGetCookieParamsCallable('\session_get_cookie_params')
222
            ->setIdCallable('\session_id')
223
            ->setCacheExpireCallable('\session_cache_expire');
224 1
    }
225
226 1
    /**
227
     * @inheritdoc
228
     */
229
    public function getRetrieveCallable(): callable
230
    {
231
        return $this->retrieveCallable;
232 3
    }
233
234 3
    /**
235
     * @inheritdoc
236 3
     */
237
    public function setRetrieveCallable(callable $callable): SessionFunctionsInterface
238
    {
239
        $this->retrieveCallable = $callable;
240
241
        return $this;
242 1
    }
243
244 1
    /**
245
     * @inheritdoc
246
     */
247
    public function getPutCallable(): callable
248
    {
249
        return $this->putCallable;
250 3
    }
251
252 3
    /**
253
     * @inheritdoc
254 3
     */
255
    public function setPutCallable(callable $callable): SessionFunctionsInterface
256
    {
257
        $this->putCallable = $callable;
258
259
        return $this;
260 1
    }
261
262 1
    /**
263
     * @inheritdoc
264
     */
265
    public function getHasCallable(): callable
266
    {
267
        return $this->hasCallable;
268 3
    }
269
270 3
    /**
271
     * @inheritdoc
272 3
     */
273
    public function setHasCallable(callable $callable): SessionFunctionsInterface
274
    {
275
        $this->hasCallable = $callable;
276
277
        return $this;
278 1
    }
279
280 1
    /**
281
     * @inheritdoc
282
     */
283
    public function getDeleteCallable(): callable
284
    {
285
        return $this->deleteCallable;
286 3
    }
287
288 3
    /**
289
     * @inheritdoc
290 3
     */
291
    public function setDeleteCallable(callable $callable): SessionFunctionsInterface
292
    {
293
        $this->deleteCallable = $callable;
294
295
        return $this;
296 1
    }
297
298 1
    /**
299
     * @inheritdoc
300
     */
301
    public function getIteratorCallable(): callable
302
    {
303
        return $this->iteratorCallable;
304 3
    }
305
306 3
    /**
307
     * @inheritdoc
308 3
     */
309
    public function setIteratorCallable(callable $callable): SessionFunctionsInterface
310
    {
311
        $this->iteratorCallable = $callable;
312
313
        return $this;
314 1
    }
315
316 1
    /**
317
     * @inheritdoc
318
     */
319
    public function getAbortCallable(): callable
320
    {
321
        return $this->abortCallable;
322 3
    }
323
324 3
    /**
325
     * @inheritdoc
326 3
     */
327
    public function setAbortCallable(callable $callable): SessionFunctionsInterface
328
    {
329
        $this->abortCallable = $callable;
330
331
        return $this;
332 1
    }
333
334 1
    /**
335
     * @inheritdoc
336
     */
337
    public function getCacheExpireCallable(): callable
338
    {
339
        return $this->cacheExpireCallable;
340 3
    }
341
342 3
    /**
343
     * @inheritdoc
344 3
     */
345
    public function setCacheExpireCallable(callable $callable): SessionFunctionsInterface
346
    {
347
        $this->cacheExpireCallable = $callable;
348
349
        return $this;
350 1
    }
351
352 1
    /**
353
     * @inheritdoc
354
     */
355
    public function getCacheLimiterCallable(): callable
356
    {
357
        return $this->cacheLimiterCallable;
358 3
    }
359
360 3
    /**
361
     * @inheritdoc
362 3
     */
363
    public function setCacheLimiterCallable(callable $callable): SessionFunctionsInterface
364
    {
365
        $this->cacheLimiterCallable = $callable;
366
367
        return $this;
368 1
    }
369
370 1
    /**
371
     * @inheritdoc
372
     */
373
    public function getCreateIdCallable(): callable
374
    {
375
        return $this->createIdCallable;
376 3
    }
377
378 3
    /**
379
     * @inheritdoc
380 3
     */
381
    public function setCreateIdCallable(callable $callable): SessionFunctionsInterface
382
    {
383
        $this->createIdCallable = $callable;
384
385
        return $this;
386 1
    }
387
388 1
    /**
389
     * @inheritdoc
390
     */
391
    public function getDecodeCallable(): callable
392
    {
393
        return $this->decodeCallable;
394 3
    }
395
396 3
    /**
397
     * @inheritdoc
398 3
     */
399
    public function setDecodeCallable(callable $callable): SessionFunctionsInterface
400
    {
401
        $this->decodeCallable = $callable;
402
403
        return $this;
404 1
    }
405
406 1
    /**
407
     * @inheritdoc
408
     */
409
    public function getDestroyCallable(): callable
410
    {
411
        return $this->destroyCallable;
412 3
    }
413
414 3
    /**
415
     * @inheritdoc
416 3
     */
417
    public function setDestroyCallable(callable $callable): SessionFunctionsInterface
418
    {
419
        $this->destroyCallable = $callable;
420
421
        return $this;
422 1
    }
423
424 1
    /**
425
     * @inheritdoc
426
     */
427
    public function getEncodeCallable(): callable
428
    {
429
        return $this->encodeCallable;
430 3
    }
431
432 3
    /**
433
     * @inheritdoc
434 3
     */
435
    public function setEncodeCallable(callable $callable): SessionFunctionsInterface
436
    {
437
        $this->encodeCallable = $callable;
438
439
        return $this;
440 1
    }
441
442 1
    /**
443
     * @inheritdoc
444
     */
445
    public function getGcCallable(): callable
446
    {
447
        return $this->gcCallable;
448 3
    }
449
450 3
    /**
451
     * @inheritdoc
452 3
     */
453
    public function setGcCallable(callable $callable): SessionFunctionsInterface
454
    {
455
        $this->gcCallable = $callable;
456
457
        return $this;
458 1
    }
459
460 1
    /**
461
     * @inheritdoc
462
     */
463
    public function getGetCookieParamsCallable(): callable
464
    {
465
        return $this->getCookieParamsCallable;
466 3
    }
467
468 3
    /**
469
     * @inheritdoc
470 3
     */
471
    public function setGetCookieParamsCallable(callable $callable): SessionFunctionsInterface
472
    {
473
        $this->getCookieParamsCallable = $callable;
474
475
        return $this;
476 1
    }
477
478 1
    /**
479
     * @inheritdoc
480
     */
481
    public function getIdCallable(): callable
482
    {
483
        return $this->idCallable;
484 3
    }
485
486 3
    /**
487
     * @inheritdoc
488 3
     */
489
    public function setIdCallable(callable $callable): SessionFunctionsInterface
490
    {
491
        $this->idCallable = $callable;
492
493
        return $this;
494 1
    }
495
496 1
    /**
497
     * @inheritdoc
498
     */
499
    public function getModuleNameCallable(): callable
500
    {
501
        return $this->moduleNameCallable;
502 3
    }
503
504 3
    /**
505
     * @inheritdoc
506 3
     */
507
    public function setModuleNameCallable(callable $callable): SessionFunctionsInterface
508
    {
509
        $this->moduleNameCallable = $callable;
510
511
        return $this;
512 1
    }
513
514 1
    /**
515
     * @inheritdoc
516
     */
517
    public function getNameCallable(): callable
518
    {
519
        return $this->nameCallable;
520 3
    }
521
522 3
    /**
523
     * @inheritdoc
524 3
     */
525
    public function setNameCallable(callable $callable): SessionFunctionsInterface
526
    {
527
        $this->nameCallable = $callable;
528
529
        return $this;
530 1
    }
531
532 1
    /**
533
     * @inheritdoc
534
     */
535
    public function getRegenerateIdCallable(): callable
536
    {
537
        return $this->regenerateIdCallable;
538 3
    }
539
540 3
    /**
541
     * @inheritdoc
542 3
     */
543
    public function setRegenerateIdCallable(callable $callable): SessionFunctionsInterface
544
    {
545
        $this->regenerateIdCallable = $callable;
546
547
        return $this;
548 1
    }
549
550 1
    /**
551
     * @inheritdoc
552
     */
553
    public function getRegisterShutdownCallable(): callable
554
    {
555
        return $this->registerShutdownCallable;
556 3
    }
557
558 3
    /**
559
     * @inheritdoc
560 3
     */
561
    public function setRegisterShutdownCallable(callable $callable): SessionFunctionsInterface
562
    {
563
        $this->registerShutdownCallable = $callable;
564
565
        return $this;
566 1
    }
567
568 1
    /**
569
     * @inheritdoc
570
     */
571
    public function getResetCallable(): callable
572
    {
573
        return $this->resetCallable;
574 3
    }
575
576 3
    /**
577
     * @inheritdoc
578 3
     */
579
    public function setResetCallable(callable $callable): SessionFunctionsInterface
580
    {
581
        $this->resetCallable = $callable;
582
583
        return $this;
584 1
    }
585
586 1
    /**
587
     * @inheritdoc
588
     */
589
    public function getSavePathCallable(): callable
590
    {
591
        return $this->savePathCallable;
592 3
    }
593
594 3
    /**
595
     * @inheritdoc
596 3
     */
597
    public function setSavePathCallable(callable $callable): SessionFunctionsInterface
598
    {
599
        $this->savePathCallable = $callable;
600
601
        return $this;
602 1
    }
603
604 1
    /**
605
     * @inheritdoc
606
     */
607
    public function getSetCookieParamsCallable(): callable
608
    {
609
        return $this->setCookieParamsCallable;
610 3
    }
611
612 3
    /**
613
     * @inheritdoc
614 3
     */
615
    public function setSetCookieParamsCallable(callable $callable): SessionFunctionsInterface
616
    {
617
        $this->setCookieParamsCallable = $callable;
618
619
        return $this;
620 1
    }
621
622 1
    /**
623
     * @inheritdoc
624
     */
625
    public function getSetSaveHandlerCallable(): callable
626
    {
627
        return $this->setSaveHandlerCallable;
628 3
    }
629
630 3
    /**
631
     * @inheritdoc
632 3
     */
633
    public function setSetSaveHandlerCallable(callable $callable): SessionFunctionsInterface
634
    {
635
        $this->setSaveHandlerCallable = $callable;
636
637
        return $this;
638 2
    }
639
640 2
    /**
641
     * @inheritdoc
642
     */
643
    public function getStartCallable(): callable
644
    {
645
        return $this->startCallable;
646 3
    }
647
648 3
    /**
649
     * @inheritdoc
650 3
     */
651
    public function setStartCallable(callable $callable): SessionFunctionsInterface
652
    {
653
        $this->startCallable = $callable;
654
655
        return $this;
656 1
    }
657
658 1
    /**
659
     * @inheritdoc
660
     */
661
    public function getStatusCallable(): callable
662
    {
663
        return $this->statusCallable;
664 3
    }
665
666 3
    /**
667
     * @inheritdoc
668 3
     */
669
    public function setStatusCallable(callable $callable): SessionFunctionsInterface
670
    {
671
        $this->statusCallable = $callable;
672
673
        return $this;
674 1
    }
675
676 1
    /**
677
     * @inheritdoc
678
     */
679
    public function getUnsetCallable(): callable
680
    {
681
        return $this->unsetCallable;
682 3
    }
683
684 3
    /**
685
     * @inheritdoc
686 3
     */
687
    public function setUnsetCallable(callable $callable): SessionFunctionsInterface
688
    {
689
        $this->unsetCallable = $callable;
690
691
        return $this;
692 2
    }
693
694 2
    /**
695
     * @inheritdoc
696
     */
697
    public function getWriteCloseCallable(): callable
698
    {
699
        return $this->writeCloseCallable;
700 3
    }
701
702 3
    /**
703
     * @inheritdoc
704 3
     */
705
    public function setWriteCloseCallable(callable $callable): SessionFunctionsInterface
706
    {
707
        $this->writeCloseCallable = $callable;
708
709
        return $this;
710 1
    }
711
712 1
    /**
713
     * @inheritdoc
714
     */
715
    public function getCouldBeStartedCallable(): callable
716
    {
717
        return $this->couldBeStartedCallable;
718 3
    }
719
720 3
    /**
721
     * @inheritdoc
722 3
     */
723
    public function setCouldBeStartedCallable(callable $callable): SessionFunctionsInterface
724
    {
725
        $this->couldBeStartedCallable = $callable;
726
727
        return $this;
728
    }
729
}
730