RememberTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 50
rs 10
c 1
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rememberStatic() 0 15 5
A remember() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito;
14
15
trait RememberTrait
16
{
17
18
    private $__remember = [];
19
20
    private static $__rememberStatic = [];
21
22
    /**
23
     * Remember static
24
     *
25
     * @param string|array $key string
26
     * @param mixed $value value
27
     * @return mixed
28
     */
29
    protected static function rememberStatic($key, $value = null)
30
    {
31
        if ($value === null && is_array($key)) {
32
            self::$__rememberStatic = $key;
33
        }
34
        if (isset(self::$__rememberStatic[$key])) {
35
            return self::$__rememberStatic[$key];
36
        }
37
        if (is_callable($value)) {
38
            self::$__rememberStatic[$key] = call_user_func($value);
39
        } else {
40
            self::$__rememberStatic[$key] = $value;
41
        }
42
43
        return self::$__rememberStatic[$key];
44
    }
45
46
    /**
47
     * Remember static
48
     *
49
     * @param string $key string
50
     * @param mixed $value value
51
     * @return mixed
52
     */
53
    protected function remember($key, $value)
54
    {
55
        if (isset($this->__remember[$key])) {
56
            return $this->__remember[$key];
57
        }
58
        if (is_callable($value)) {
59
            $this->__remember[$key] = call_user_func($value);
60
        } else {
61
            $this->__remember[$key] = $value;
62
        }
63
64
        return $this->__remember[$key];
65
    }
66
}
67