Completed
Pull Request — master (#155)
by Thomas
12:04
created

BrowscapCache   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 212
Duplicated Lines 13.21 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 74.13%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 25
c 7
b 2
f 0
lcom 1
cbo 1
dl 28
loc 212
ccs 43
cts 58
cp 0.7413
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getVersion() 14 14 4
A getReleaseDate() 0 14 4
A getType() 14 14 4
B getItem() 0 31 5
A setItem() 0 14 2
A removeItem() 0 8 2
A flush() 0 4 1
A hasItem() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @package    Cache
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Cache;
32
33
use WurflCache\Adapter\AdapterInterface;
34
35
/**
36
 * a cache proxy to be able to use the cache adapters provided by the WurflCache package
37
 *
38
 * @category   Browscap-PHP
39
 * @package    Cache
40
 * @author     Thomas Müller <[email protected]>
41
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
42
 * @version    3.0
43
 * @license    http://www.opensource.org/licenses/MIT MIT License
44
 * @link       https://github.com/browscap/browscap-php/
45
 */
46
class BrowscapCache implements BrowscapCacheInterface
47
{
48
    /**
49
     * Path to the cache directory
50
     *
51
     * @var \WurflCache\Adapter\AdapterInterface
52
     */
53
    private $cache = null;
54
55
    /**
56
     * Detected browscap version (read from INI file)
57
     *
58
     * @var int
59
     */
60
    private $version = null;
61
62
    /**
63
     * Release date of the Browscap data (read from INI file)
64
     *
65
     * @var string
66
     */
67
    private $releaseDate;
68
    
69
    /**
70
     * Type of the Browscap data (read from INI file)
71
     *
72
     * @var string
73
     */
74
    private $type;
75
76
    /**
77
     * Constructor class, checks for the existence of (and loads) the cache and
78
     * if needed updated the definitions
79
     *
80
     * @param \WurflCache\Adapter\AdapterInterface $adapter
81
     * @param int                                  $updateInterval
82
     */
83 17
    public function __construct(AdapterInterface $adapter, $updateInterval = BrowscapCacheInterface::CACHE_LIVETIME)
84
    {
85 17
        $this->cache = $adapter;
86 17
        $this->cache->setExpiration((int) $updateInterval);
87 17
    }
88
89
    /**
90
     * Gets the version of the Browscap data
91
     *
92
     * @return int
93
     */
94 6 View Code Duplication
    public function getVersion()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96 6
        if ($this->version === null) {
97 6
            $success = true;
98
99 6
            $version = $this->getItem('browscap.version', false, $success);
100
101 6
            if ($version !== null && $success) {
102 4
                $this->version = (int) $version;
103
            }
104
        }
105
106 6
        return $this->version;
107
    }
108
109
    /**
110
     * Gets the release date of the Browscap data
111
     *
112
     * @return string
113
     */
114 1
    public function getReleaseDate()
115
    {
116 1
        if ($this->releaseDate === null) {
117 1
            $success = true;
118
119 1
            $releaseDate = $this->getItem('browscap.releaseDate', false, $success);
120
121 1
            if ($releaseDate !== null && $success) {
122 1
                $this->releaseDate = $releaseDate;
123
            }
124
        }
125
126 1
        return $this->releaseDate;
127
    }
128
    
129
    /**
130
     * Gets the type of the Browscap data
131
     *
132
     * @return string
133
     */
134 1 View Code Duplication
    public function getType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135 1
    {
136 1
        if ($this->type === null) {
137 1
            $success = true;
138
    
139 1
            $type = $this->getItem('browscap.type', false, $success);
140
    
141 1
            if ($type !== null && $success) {
142 1
                $this->type = $type;
143
            }
144
        }
145
    
146 1
        return $this->type;
147
    }
148
149
    /**
150
     * Get an item.
151
     *
152
     * @param string $cacheId
153
     * @param bool   $withVersion
154
     * @param bool   $success
155
     *
156
     * @return mixed Data on success, null on failure
157
     */
158 7
    public function getItem($cacheId, $withVersion = true, & $success = null)
159
    {
160 7
        if ($withVersion) {
161
            $cacheId .= '.'.$this->getVersion();
162
        }
163
164 7
        if (!$this->cache->hasItem($cacheId)) {
165 7
            $success = false;
166
167 7
            return null;
168
        }
169
170 6
        $success = null;
171 6
        $data    = $this->cache->getItem($cacheId, $success);
172
173 6
        if (!$success) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $success of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
174
            $success = false;
175
176
            return null;
177
        }
178
179 6
        if (!isset($data['content'])) {
180
            $success = false;
181
182
            return null;
183
        }
184
185 6
        $success = true;
186
187 6
        return unserialize($data['content']);
188
    }
189
190
    /**
191
     * save the content into an php file
192
     *
193
     * @param string $cacheId     The cache id
194
     * @param mixed  $content     The content to store
195
     * @param bool   $withVersion
196
     *
197
     * @return boolean whether the file was correctly written to the disk
198
     */
199 6
    public function setItem($cacheId, $content, $withVersion = true)
200
    {
201
        // Get the whole PHP code
202
        $data = array(
203 6
            'content' => serialize($content),
204 6
        );
205
206 6
        if ($withVersion) {
207 3
            $cacheId .= '.'.$this->getVersion();
208
        }
209
210
        // Save and return
211 6
        return $this->cache->setItem($cacheId, $data);
212
    }
213
214
    /**
215
     * Test if an item exists.
216
     *
217
     * @param string $cacheId
218
     * @param bool   $withVersion
219
     *
220
     * @return bool
221
     */
222
    public function hasItem($cacheId, $withVersion = true)
223
    {
224
        if ($withVersion) {
225
            $cacheId .= '.'.$this->getVersion();
226
        }
227
228
        return $this->cache->hasItem($cacheId);
229
    }
230
231
    /**
232
     * Remove an item.
233
     *
234
     * @param string $cacheId
235
     * @param bool   $withVersion
236
     *
237
     * @return bool
238
     */
239
    public function removeItem($cacheId, $withVersion = true)
240
    {
241
        if ($withVersion) {
242
            $cacheId .= '.'.$this->getVersion();
243
        }
244
245
        return $this->cache->removeItem($cacheId);
246
    }
247
248
    /**
249
     * Flush the whole storage
250
     *
251
     * @return bool
252
     */
253
    public function flush()
254
    {
255
        return $this->cache->flush();
256
    }
257
}
258