Completed
Push — 2.0 ( b22aa0...07e083 )
by Marco
11:24 queued 07:13
created

FilesystemXattr   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 204
Duplicated Lines 18.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 1
dl 38
loc 204
ccs 30
cts 66
cp 0.4545
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A test() 0 5 2
A get() 0 17 3
B set() 5 21 5
A delete() 0 15 2
A getMultiple() 11 11 2
A setMultiple() 11 11 2
A deleteMultiple() 11 11 2
B has() 0 24 5
A stats() 0 5 1
A clear() 0 21 4

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 namespace Comodojo\Cache\Drivers;
2
3
use \Exception;
4
5
/**
6
 * @package     Comodojo Cache
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
class FilesystemXattr extends AbstractDriver {
22
23
    const DRIVER_NAME = "filesystem-xattr";
24
25
    /**
26
     * Cache (files) repository
27
     *
28
     * @param string
29
     */
30
    protected $cache_folder;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 47
    public function __construct(array $configuration) {
36
37 47
        $this->cache_folder = $configuration['cache-folder'];
38
39 47
        if ( $this->test() === false ) throw new Exception("Folder ".$this->cache_folder." not writable");
40
41 47
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 47
    public function test() {
47
48 47
        return file_exists($this->cache_folder) && is_writable($this->cache_folder);
49
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 35
    public function get($key, $namespace) {
56
57 35
        if ( $this->has($key, $namespace) ) {
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
59 19
            $cacheFile = $this->cache_folder."$key-$namespace.cache";
60
61 19
            $data = @file_get_contents($cacheFile);
62
63 19
            if ( $data === false ) throw new Exception("Error reading cache content $cacheFile");
64
65 19
            return $data;
66
67
        }
68
69 23
        return null;
70
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function set($key, $namespace, $value, $ttl = null) {
77
78
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
79
80 View Code Duplication
        if ( $ttl == null || $ttl == 0 ) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $ttl of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
81
            $ttl = 0;
82
        } else {
83
            $ttl = time() + intval($ttl);
84
        }
85
86
        $cached = @file_put_contents($cacheFile, $value, LOCK_EX);
87
88
        if ( $cached === false ) throw new Exception("Error writing cache object $cacheFile");
89
90
        $tagged = @xattr_set($cacheFile, "EXPIRE", $ttl, XATTR_DONTFOLLOW);
91
92
        if ( $tagged === false ) throw new Exception("Error writing cache ttl $cacheFile");
93
94
        return true;
95
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function delete($key, $namespace) {
102
103
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
104
105
        if ( file_exists($cacheFile) ) {
106
107
            $u = unlink($cacheFile);
108
109
            return $u;
110
111
        }
112
113
        return false;
114
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function clear($namespace = null) {
121
122
        $result = [];
123
124
        if ( $namespace === null ) {
125
126
            $file_list = glob($this->cache_folder."*.cache");
127
128
        } else {
129
130
            $file_list = glob($this->cache_folder."*-$namespace.cache");
131
132
        }
133
134
        if ( empty($file_list) ) return true;
135
136
        foreach ( $file_list as $file ) $result[] = unlink($file);
137
138
        return !in_array(false, $result);
139
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2 View Code Duplication
    public function getMultiple(array $keys, $namespace) {
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...
146
147 2
        $result = [];
148
149 2
        foreach ( $keys as $key ) {
150 2
            $result[$key] = $this->get($key, $namespace);
151
        }
152
153 2
        return $result;
154
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1 View Code Duplication
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
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...
161
162 1
        $result = [];
163
164 1
        foreach ( $key_values as $key => $value ) {
165 1
            $result[] = $this->set($key, $namespace, $value, $ttl);
166
        }
167
168 1
        return !in_array(false, $result);
169
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 3 View Code Duplication
    public function deleteMultiple(array $keys, $namespace) {
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...
176
177 3
        $result = [];
178
179 3
        foreach ( $keys as $key ) {
180 3
            $result[] = $this->delete($key, $namespace);
181
        }
182
183 3
        return !in_array(false, $result);
184
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function has($key, $namespace) {
191
192
        $time = time();
193
194
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
195
196
        if ( file_exists($cacheFile) ) {
197
198
            $expire = xattr_get($cacheFile, "EXPIRE", XATTR_DONTFOLLOW);
199
200
            if ( $expire === false ) {
201
                throw new Exception("Error reading cache ttl for $cacheFile");
202
            } else if ( $expire < $time && $expire != 0 ) {
203
                $this->delete($key, $namespace);
0 ignored issues
show
Documentation introduced by
$key is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
                return false;
205
            } else {
206
                return true;
207
            }
208
209
        }
210
211
        return false;
212
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 2
    public function stats() {
219
220 2
        return ['objects' => count(glob($this->cache_folder."*.cache"))];
221
222
    }
223
224
}
225