Completed
Push — master ( e11a6e...0e0464 )
by Lars
01:40
created

AdapterApcu   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 152
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 0
dl 24
loc 152
ccs 24
cts 33
cp 0.7272
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A apcu_cache_exists() 0 4 1
B __construct() 24 24 7
A cacheClear() 0 4 1
A cacheInfo() 0 11 2
A exists() 0 4 1
A get() 0 8 2
A installed() 0 4 1
A remove() 0 4 1
A removeAll() 0 4 2
A set() 0 4 1
A setExpired() 0 4 1

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
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterApcu: a APCu-Cache adapter
9
 *
10
 * @see http://php.net/manual/de/book.apcu.php
11
 */
12
class AdapterApcu implements iAdapter
13
{
14
    /**
15
     * @var bool
16
     */
17
    public $installed = false;
18
19
    /**
20
     * @var bool
21
     */
22
    public $debug = false;
23
24
    /**
25
     * __construct()
26
     */
27 15 View Code Duplication
    public function __construct()
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...
28
    {
29
        if (
30 15
            \PHP_SAPI !== 'cli'
31
            &&
32
            \function_exists('apcu_store') === true
33
            &&
34
            \ini_get('apc.enabled')
35
        ) {
36
            $this->installed = true;
37
        }
38
39
        if (
40
            \PHP_SAPI === 'cli'
41
            &&
42 15
            \function_exists('apcu_store') === true
43
            &&
44 15
            \ini_get('apc.enable_cli')
45
        ) {
46 15
            \ini_set('apc.use_request_time', '0');
47
48 15
            $this->installed = true;
49
        }
50 15
    }
51
52
    /**
53
     * Check if apcu-cache exists.
54
     *
55
     * WARNING: we only keep this method for compatibly-reasons
56
     *          -> use ->exists($key)
57
     *
58
     * @param string $key
59
     *
60
     * @return bool
61
     *
62
     * @deprecated
63
     */
64
    public function apcu_cache_exists($key): bool
65
    {
66
        return $this->exists($key);
67
    }
68
69
    /**
70
     * Clears the APCu cache by type.
71
     *
72
     * @param string $type <p>WARNING: is not used in APCu only valid for APC</p>
73
     *
74
     * @return bool
75
     *
76
     * @internal
77
     */
78 1
    public function cacheClear(string $type): bool
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80 1
        return (bool) \apcu_clear_cache();
81
    }
82
83
    /**
84
     * Retrieves cached information from APCu's data store
85
     *
86
     * @param bool $limited    - If $limited is TRUE, the return value will exclude the individual list of cache
87
     *                         entries. This is useful when trying to optimize calls for statistics gathering
88
     *
89
     * @return array
90
     *               <p>Array of cached data (and meta-data) or empty array on failure.</p>
91
     */
92
    public function cacheInfo(bool $limited = false): array
93
    {
94
        /** @var array|false $return */
95
        $return = \apcu_cache_info($limited);
96
97
        if ($return === false) {
98
            return [];
99
        }
100
101
        return $return;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 7
    public function exists(string $key): bool
108
    {
109 7
        return (bool) \apcu_exists($key);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 3
    public function get(string $key)
116
    {
117 3
        if ($this->exists($key)) {
118 3
            return \apcu_fetch($key);
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 6
    public function installed(): bool
128
    {
129 6
        return $this->installed;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 1
    public function remove(string $key): bool
136
    {
137 1
        return (bool) \apcu_delete($key);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 1
    public function removeAll(): bool
144
    {
145 1
        return (bool) ($this->cacheClear('system') && $this->cacheClear('user'));
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 5
    public function set(string $key, $value): bool
152
    {
153 5
        return (bool) \apcu_store($key, $value);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 4
    public function setExpired(string $key, $data, int $ttl = 0): bool
160
    {
161 4
        return (bool) \apcu_store($key, $data, $ttl);
162
    }
163
}
164