Completed
Push — 2.0 ( a51c1d...cacff6 )
by Marco
04:49
created

FilesystemXattr   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 168
Duplicated Lines 36.31 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 1
dl 61
loc 168
ccs 30
cts 30
cp 1
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
A stats() 0 5 1
B set() 5 21 5
A delete() 0 15 2
B clear() 23 23 4
A getMultiple() 11 11 2
A setMultiple() 11 11 2
A deleteMultiple() 11 11 2
B has() 0 24 5

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
 * Apcu provider
7
 *
8
 * @package     Comodojo Spare Parts
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class FilesystemXattr extends AbstractDriver {
24
25
    const DRIVER_NAME = "filesystem-xattr";
26
27
    protected $cache_folder;
28
29 47
    public function __construct(array $configuration) {
30
31 47
        $this->cache_folder = $configuration['cache-folder'];
32
33 47
        if ( $this->test() === false ) throw new Exception("Folder ".$this->cache_folder." not writable");
34
35 47
    }
36
37 47
    public function test() {
38
39 47
        return file_exists($this->cache_folder) && is_writable($this->cache_folder);
40
41
    }
42
43 34
    public function get($key, $namespace) {
44
45 34
        if ( $this->has($key, $namespace) ) {
46
47 19
            $cacheFile = $this->cache_folder."$key-$namespace.cache";
48
49 19
            $data = @file_get_contents($cacheFile);
50
51 19
            if ( $data === false ) throw new Exception("Error reading cache content $cacheFile");
52
53 19
            return $data;
54
55
        }
56
57 22
        return null;
58
59
    }
60
61
    public function set($key, $namespace, $value, $ttl = null) {
62
63
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
64
65 View Code Duplication
        if ( $ttl == null || $ttl == 0 ) {
0 ignored issues
show
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...
66
            $ttl = 0;
67
        } else {
68
            $ttl = time()+intval($ttl);
69
        }
70
71
        $cached = @file_put_contents($cacheFile, $value, LOCK_EX);
72
73
        if ( $cached === false ) throw new Exception("Error writing cache object $cacheFile");
74
75
        $tagged = @xattr_set($cacheFile, "EXPIRE", $ttl, XATTR_DONTFOLLOW);
76
77
        if ( $tagged === false ) throw new Exception("Error writing cache ttl $cacheFile");
78
79
        return true;
80
81
    }
82
83
    public function delete($key, $namespace) {
84
85
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
86
87
        if ( file_exists($cacheFile) ) {
88
89
            $u = unlink($cacheFile);
90
91
            return $u;
92
93
        }
94
95
        return false;
96
97
    }
98
99 View Code Duplication
    public function clear($namespace = 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...
100
101
        $file_list;
0 ignored issues
show
Bug introduced by
The variable $file_list seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
102
103
        $result = [];
104
105
        if ( $namespace === null ) {
106
107
            $file_list = glob($this->cache_folder."*.cache");
108
109
        } else {
110
111
            $file_list = glob($this->cache_folder."*-$namespace.cache");
112
113
        }
114
115
        if ( empty($file_list) ) return true;
116
117
        foreach ( $file_list as $file ) $result[] = unlink($file);
118
119
        return !in_array(false, $result);
120
121
    }
122
123 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...
124
125 2
        $result = [];
126
127 2
        foreach ($keys as $key) {
128 2
            $result[$key] = $this->get($key, $namespace);
129
        }
130
131 2
        return $result;
132
133
    }
134
135 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...
136
137 1
        $result = [];
138
139 1
        foreach ($key_values as $key => $value) {
140 1
            $result[] = $this->set($key, $namespace, $value, $ttl);
141
        }
142
143 1
        return !in_array(false, $result);
144
145
    }
146
147 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...
148
149 3
        $result = [];
150
151 3
        foreach ($keys as $key) {
152 3
            $result[] = $this->delete($key, $namespace);
153
        }
154
155 3
        return !in_array(false, $result);
156
157
    }
158
159
    public function has($key, $namespace) {
160
161
        $time = time();
162
163
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
164
165
        if ( file_exists($cacheFile) ) {
166
167
            $expire = xattr_get($cacheFile, "EXPIRE", XATTR_DONTFOLLOW);
168
169
            if ( $expire === false ) {
170
                throw new Exception("Error reading cache ttl for $cacheFile");
171
            } else if ( $expire < $time && $expire != 0 ) {
172
                $this->delete($key, $namespace);
173
                return false;
174
            } else {
175
                return true;
176
            }
177
178
        }
179
180
        return false;
181
182
    }
183
184 2
    public function stats() {
185
186 2
        return ['objects' => count(glob($this->cache_folder."*.cache"))];
187
188
    }
189
190
}
191