1 | <?php |
||
13 | class Memcached implements ICache |
||
14 | { |
||
15 | use Injector; |
||
16 | |||
17 | /** |
||
18 | * @var Container キャッシュ依存コンテナ |
||
19 | */ |
||
20 | private $cacheContainer; |
||
21 | |||
22 | /** |
||
23 | * @var string キャッシュ接頭辞 |
||
24 | */ |
||
25 | private $cachePrefix; |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function __construct(Container $cacheContainer) |
||
31 | { |
||
32 | $this->cacheContainer = $cacheContainer; |
||
33 | $this->cachePrefix = $this->cacheContainer->cachePrefix . '.' . $this->cacheContainer->classPrefix . '.'; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 4 | public function add($key, $value, $ttl = 0, $overwrite = false): bool |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 4 | public function get($key) |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 1 | public function delete($key): bool |
|
97 | { |
||
98 | 1 | if (!$this->isAvailableCacheLibrary()) { |
|
99 | return false; |
||
100 | } |
||
101 | 1 | $key = $this->cachePrefix . $key; |
|
102 | 1 | $this->cacheContainer->driver->delete($key); |
|
103 | |||
104 | 1 | if ($this->verifyReturnCode($this->cacheContainer->codes['notfound'])) { |
|
105 | $this->logger->info("Execute cache cleared: " . $key); |
||
106 | return true; |
||
107 | } else { |
||
108 | 1 | $this->logger->warn("Failed to clear cache: " . $key); |
|
109 | 1 | return false; |
|
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 1 | public function clear(): bool |
|
117 | { |
||
118 | 1 | if (!$this->isAvailableCacheLibrary()) { |
|
119 | return false; |
||
120 | } |
||
121 | 1 | $allKeys = $this->cacheContainer->driver->getAllKeys(); |
|
122 | 1 | if ($allKeys === false) { |
|
123 | 1 | $this->logger->warn("Can't get cache keys: " . $this->cachePrefix . "*"); |
|
124 | 1 | $this->cacheContainer->driver->flush(); |
|
125 | |||
126 | 1 | return true; |
|
127 | } |
||
128 | |||
129 | $prefixLength = strlen($this->cachePrefix); |
||
130 | $targetKeys = []; |
||
131 | foreach ($allKeys as $key) { |
||
132 | if (substr($key, 0, $prefixLength)) { |
||
133 | $targetKeys[] = $key; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $this->cacheContainer->driver->deleteMulti($targetKeys); |
||
138 | |||
139 | if ($this->verifyReturnCode($this->cacheContainer->codes['notfound'])) { |
||
140 | $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*"); |
||
141 | return true; |
||
142 | } else { |
||
143 | $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*"); |
||
144 | return false; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * リターンコードを検証する |
||
150 | * @param int $code 想定コード |
||
151 | * @return bool 検証結果 |
||
152 | */ |
||
153 | 4 | private function verifyReturnCode(int $code): bool |
|
164 | |||
165 | /** |
||
166 | * キャッシュライブラリが使用可能か検査する |
||
167 | * @return bool 使用可能でtrue |
||
168 | */ |
||
169 | 4 | private function isAvailableCacheLibrary(): bool |
|
179 | } |
||
180 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.