Passed
Push — master ( 5c9a79...879726 )
by Michael
02:51 queued 01:58
created

Registry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEntry() 0 7 2
A isEntry() 0 3 1
A setEntry() 0 12 2
A lockEntry() 0 5 1
A getInstance() 0 8 2
A unsetAll() 0 4 1
A unsetEntry() 0 3 1
A unlockEntry() 0 3 1
A isLocked() 0 3 1
1
<?php namespace XoopsModules\Mymenus;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright       XOOPS Project (https://xoops.org)
15
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
16
 * @package         Mymenus
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 */
20
21
defined('XOOPS_ROOT_PATH') || die('Restricted access');
22
23
/**
24
 * Class Registry
25
 */
26
class Registry
27
{
28
    protected $entries;
29
    protected $locks;
30
31
    /**
32
     *
33
     */
34
    protected function __construct()
35
    {
36
        $this->entries = [];
37
        $this->locks   = [];
38
    }
39
40
    /**
41
     * @return Registry
42
     */
43
    public static function getInstance()
44
    {
45
        static $instance;
46
        if (null === $instance) {
47
            $instance = new static();
48
        }
49
50
        return $instance;
51
    }
52
53
    /**
54
     * @TODO: move hard coded language string to language file
55
     *
56
     * @param $key
57
     * @param $item
58
     *
59
     * @return bool
60
     */
61
    public function setEntry($key, $item)
62
    {
63
        $ret = true;
64
        if (true === $this->isLocked($key)) {
65
            trigger_error("Unable to set entry `{$key}`. Entry is locked.", E_USER_WARNING);
66
67
            $ret = false;
68
        }
69
70
        $this->entries[$key] = $item;
71
72
        return $ret;
73
    }
74
75
    /**
76
     * @param $key
77
     */
78
    public function unsetEntry($key)
79
    {
80
        unset($this->entries[$key]);
81
    }
82
83
    /**
84
     * @param $key
85
     *
86
     * @return null
87
     */
88
    public function getEntry($key)
89
    {
90
        if (false === isset($this->entries[$key])) {
91
            return null;
92
        }
93
94
        return $this->entries[$key];
95
    }
96
97
    /**
98
     * @param $key
99
     *
100
     * @return bool
101
     */
102
    public function isEntry($key)
103
    {
104
        return (null !== $this->getEntry($key));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getEntry($key) targeting XoopsModules\Mymenus\Registry::getEntry() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
    }
106
107
    /**
108
     * @param $key
109
     *
110
     * @return bool
111
     */
112
    public function lockEntry($key)
113
    {
114
        $this->locks[$key] = true;
115
116
        return true;
117
    }
118
119
    /**
120
     * @param $key
121
     */
122
    public function unlockEntry($key)
123
    {
124
        unset($this->locks[$key]);
125
    }
126
127
    /**
128
     * @param $key
129
     *
130
     * @return bool
131
     */
132
    public function isLocked($key)
133
    {
134
        return (true === isset($this->locks[$key]));
135
    }
136
137
    public function unsetAll()
138
    {
139
        $this->entries = [];
140
        $this->locks   = [];
141
    }
142
}
143