Driver_Xcache   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A clear() 0 11 2
A delete() 0 11 3
A info() 0 11 1
B keys() 0 24 3
A load() 0 11 3
A save() 0 10 2
1
<?php
2
3
/**
4
 * Provides caching functionality for XCache
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 XCache
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_Xcache 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_XCache
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 (!extension_loaded('xcache')) {
46
47
            throw new \Exception('Extension "xcache" 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
        $cache_count = xcache_count(XC_TYPE_VAR);
60
61
        for ($i = 0; $i < $cache_count; $i++) {
62
63
            xcache_clear_cache(XC_TYPE_VAR, $i);
64
        }
65
66
        return true;
67
    }
68
69
    /**
70
     * Removes a cached key
71
     *
72
     * @param string $key Name of the key
73
     * @param int    $ttl Time to life used for the key
74
     *
75
     * @return boolean
76
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
77
78
    public function delete($key, $ttl = 0)
79
    {
80
        $token = empty($ttl) ? $key : 'ttl_' . $key;
81
82
        if (xcache_isset($token)) {
83
84
            xcache_unset($token);
85
        }
86
87
        return true;
88
    }
89
90
    /**
91
     * Returns a formatted array with statistics
92
     *
93
     * @return array
94
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
95
96
    public function info()
97
    {
98
        $info = parent::info();
99
100
        $info['version'] = phpversion('xcache');
101
        $info['client']  = '';
102
        $info['server']  = '';
103
        $info['keys']    = xcache_count(XC_TYPE_VAR);
104
105
        return $info;
106
    }
107
108
    /**
109
     * Returns a formatted array with all keys and additional information
110
     *
111
     * @return array
112
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
113
114
    public function keys()
115
    {
116
        $form = [];
117
118
        $cache_count = xcache_count(XC_TYPE_VAR);
119
120
        for ($i = 0; $i < $cache_count; $i++) {
121
122
            $info = xcache_list(XC_TYPE_VAR, $i);
123
124
            foreach ($info['cache_list'] AS $num => $data) {
125
126
                $handle = $data['name'] . ' (' . $i . '.' . $num . ')';
127
128
                $form[$handle] = ['name' => $handle,
129
                                  'time' => $data['ctime'],
130
                                  'size' => $data['size']];
131
            }
132
        }
133
134
        ksort($form);
135
136
        return array_values($form);
137
    }
138
139
    /**
140
     * Fetches the desired key
141
     *
142
     * @param string $key Name of the key
143
     * @param int    $ttl Time to life used for the key
144
     *
145
     * @return array
146
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
147
148
    public function load($key, $ttl = 0)
149
    {
150
        $token = empty($ttl) ? $key : 'ttl_' . $key;
151
152
        if (xcache_isset($token)) {
153
154
            return xcache_get($token);
155
        }
156
157
        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...
158
    }
159
160
    /**
161
     * Stores the key with its value in the cache
162
     *
163
     * @param string $key   Name of the key
164
     * @param array  $value Content to be stored
165
     * @param int    $ttl   Time to life used for the key
166
     *
167
     * @return boolean
168
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
169
170
    public function save($key, $value, $ttl = 0)
171
    {
172
        $token = empty($ttl) ? $key : 'ttl_' . $key;
173
174
        xcache_set($token, $value, $ttl);
175
176
        $this->log($key);
177
178
        return true;
179
    }
180
}
181