Driver_APC::info()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 8
nop 0
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Provides caching functionality for APC and APCu
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Cache
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\cache;
17
18
/**
19
 * Provides caching functionality for APC and APCu
20
 *
21
 * @category  Core
22
 * @package   Cache
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Driver_APC extends Base
30
{
31
    /**
32
     * Creates the cache handler object
33
     *
34
     * @param array $config Configuration details as an array
35
     *
36
     * @throws \Exception
37
     *
38
     * @return \csphere\core\cache\Driver_APC
39
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
40
41
    public function __construct(array $config)
42
    {
43
        parent::__construct($config);
44
45
        if (!function_exists('apc_exists')) {
46
47
            throw new \Exception('Extension "apc" outdated or not found');
48
        }
49
    }
50
51
    /**
52
     * Clears the cache content
53
     *
54
     * @return boolean
55
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
56
57
    public function clear()
58
    {
59
        // Workaround for APCu version below 4.0.2
60
        $reflection = new \ReflectionFunction('apc_clear_cache');
61
        $parameters = $reflection->getParameters();
62
63
        if ($parameters[0]->getName() == 'info') {
0 ignored issues
show
Bug introduced by
Consider using $parameters[0]->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
64
65
            apc_clear_cache();
66
67
        } else {
68
69
            apc_clear_cache('user');
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * Removes a cached key
77
     *
78
     * @param string $key Name of the key
79
     * @param int    $ttl Time to life used for the key
80
     *
81
     * @return boolean
82
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
83
84
    public function delete($key, $ttl = 0)
85
    {
86
        $token = empty($ttl) ? $key : 'ttl_' . $key;
87
88
        if (apc_exists($token)) {
89
90
            apc_delete($token);
91
        }
92
93
        return true;
94
    }
95
96
    /**
97
     * Returns a formatted array with statistics
98
     *
99
     * @return array
100
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
101
102
    public function info()
103
    {
104
        $info = parent::info();
105
106
        $stats = apc_cache_info('user');
107
        $keys  = isset($stats['nentries']) ? $stats['nentries'] : '';
108
109
        if (empty($keys) && isset ($stats['cache_list'])) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
110
111
            $keys = count($stats['cache_list']);
112
        }
113
114
        $info['version'] = phpversion('apc');
115
        $info['client']  = '';
116
        $info['server']  = '';
117
        $info['keys']    = $keys;
118
119
        // Check for apcu
120
        if (extension_loaded('apcu')) {
121
122
            $info['client'] = 'apcu ' . phpversion('apcu');
123
        }
124
125
        return $info;
126
    }
127
128
    /**
129
     * Returns a formatted array with all keys and additional information
130
     *
131
     * @return array
132
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
133
134
    public function keys()
135
    {
136
        $form = [];
137
138
        $info = apc_cache_info('user');
139
        $list = isset($info['cache_list']) ? $info['cache_list'] : [];
140
141
        foreach ($list AS $num => $data) {
142
143
            // Key is named "key" in APCu, but "info" in APC
144
            $key    = isset($data['key']) ? $data['key'] : $data['info'];
145
            $handle = $key . ' (' . $num . ')';
146
147
            $form[$handle] = ['name' => $handle, 'time' => $data['mtime'],
148
                                   'size' => $data['mem_size']];
149
        }
150
151
        ksort($form);
152
153
        return array_values($form);
154
    }
155
156
    /**
157
     * Fetches the desired key
158
     *
159
     * @param string $key Name of the key
160
     * @param int    $ttl Time to life used for the key
161
     *
162
     * @return array
163
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
164
165
    public function load($key, $ttl = 0)
166
    {
167
        $token = empty($ttl) ? $key : 'ttl_' . $key;
168
169
        if (apc_exists($token)) {
170
171
            return apc_fetch($token);
172
        }
173
174
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method csphere\core\cache\Base::load of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
175
    }
176
177
    /**
178
     * Stores the key with its value in the cache
179
     *
180
     * @param string $key   Name of the key
181
     * @param array  $value Content to be stored
182
     * @param int    $ttl   Time to life used for the key
183
     *
184
     * @return boolean
185
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
186
187
    public function save($key, $value, $ttl = 0)
188
    {
189
        $token = empty($ttl) ? $key : 'ttl_' . $key;
190
191
        apc_store($token, $value, $ttl);
192
193
        $this->log($key);
194
195
        return true;
196
    }
197
}
198