Test Failed
Push — master ( 59d968...d064f5 )
by Ilya
05:02
created

ApcuCache::buildKeyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Paillechat\ApcuSimpleCache;
4
5
use Paillechat\ApcuSimpleCache\Exception\ApcuInvalidCacheKeyException;
6
use Psr\SimpleCache\CacheInterface;
7
8
class ApcuCache implements CacheInterface
9
{
10
    /**
11
     * @var string
12
     */
13 16
    private $namespace;
14
    /**
15 16
     * @var int
16
     */
17 2
    private $defaultLifetime;
18
19
    public function __construct($namespace = '', $defaultLifetime = 0)
20
    {
21
        $this->namespace = $namespace;
22
        $this->defaultLifetime = $defaultLifetime;
23 2
    }
24
25 2
    /**
26
     * {@inheritdoc}
27 2
     */
28
    public function get($key, $default = null)
29
    {
30
        $this->assertKeyName($key);
31
        $key = $this->buildKeyName($key);
32
33 2
        return apcu_fetch($key) ?:$default;
34
    }
35 2
36
    /**
37 2
     * {@inheritdoc}
38
     */
39
    public function set($key, $value, $ttl = null)
40
    {
41
        $this->assertKeyName($key);
42
        $key = $this->buildKeyName($key);
43 4
44
        $ttl = is_null($ttl) ? $this-$this->defaultLifetime : $ttl;
45 4
46
        return apcu_store($key, $value, (int) $ttl);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_store($key, $value, (int) $ttl); of type boolean|array adds the type array to the return on line 46 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::set of type boolean.
Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51 2
     */
52
    public function delete($key)
53 2
    {
54
        $this->assertKeyName($key);
55 2
        $key = $this->buildKeyName($key);
56
57 2
        return apcu_delete($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_delete($key); of type boolean|string[] adds the type string[] to the return on line 57 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::delete of type boolean.
Loading history...
58 2
    }
59 2
60 1
    /**
61
     * {@inheritdoc}
62 2
     */
63
    public function clear()
64
    {
65
        return apcu_clear_cache();
66
    }
67
68 2
    /**
69
     * {@inheritdoc}
70 2
     */
71
    public function getMultiple($keys, $default = null)
72 2
    {
73
        $this->assertKeyNames($keys);
74 2
        $keys = $this->buildKeyNames($keys);
75
76
        $result = apcu_fetch($keys);
77
78
        if (!is_null($default) && is_array($result) && count($keys) > count($result)) {
79
            $notFoundKeys = array_diff($keys, array_keys($result));
80 2
            $result = array_merge($result, array_fill_keys($notFoundKeys, $default));
81
        }
82 2
83
        $mappedResult = [];
84 2
85
        foreach ($result as $key => $value) {
86 2
            $key = preg_replace("/^$this->namespace/", '', $key);
87
88
            $mappedResult[$key] = $value;
89
        }
90
91
        return $mappedResult;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $mappedResult; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::getMultiple of type Psr\SimpleCache\iterable.

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...
92 4
    }
93
94 4
    /**
95
     * {@inheritdoc}
96 4
     */
97
    public function setMultiple($values, $ttl = null)
98
    {
99 18
        $this->assertKeyNames(array_keys($values));
100
101 18
        $mappedByNamespaceValues = [];
102 14
103
        foreach ($values as $key => $value) {
104 4
            $mappedByNamespaceValues[$this->buildKeyName($key)] = $value;
105
        }
106
107
        $ttl = is_null($ttl) ? $this-$this->defaultLifetime : $ttl;
108 2
109 2
        $result = apcu_store($mappedByNamespaceValues, (int) $ttl);
110 2
111 2
        return $result === true ? true : (is_array($result) && count($result) == 0 ? true: false);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function deleteMultiple($keys)
118
    {
119
        $this->assertKeyNames($keys);
120
        $keys = $this->buildKeyNames($keys);
121
122
        $result = apcu_delete($keys);
123
124
        return count($result) === count($keys) ? false : true;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function has($key)
131
    {
132
        $this->assertKeyName($key);
133
        $key = $this->buildKeyName($key);
134
135
        return apcu_exists($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_exists($key); of type boolean|string[] adds the type string[] to the return on line 135 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::has of type boolean.
Loading history...
136
    }
137
138
    /**
139
     * @param string $key
140
     *
141
     * @return string
142
     */
143
    private function buildKeyName($key)
144
    {
145
        return $this->namespace . $key;
146
    }
147
148
    /**
149
     * @param string[] $keys
150
     *
151
     * @return string[]
152
     */
153
    private function buildKeyNames(array $keys)
154
    {
155
        return array_map(function($key) {
156
            return $this->buildKeyName($key);
157
        }, $keys);
158
159
    }
160
161
    /**
162
     * @param mixed $key
163
     *
164
     * @throws ApcuInvalidCacheKeyException
165
     */
166
    private function assertKeyName($key)
167
    {
168
        if (!is_string($key)) {
169
            throw new ApcuInvalidCacheKeyException();
170
        }
171
    }
172
173
    /**
174
     * @param string[] $keys
175
     *
176
     * @throws ApcuInvalidCacheKeyException
177
     */
178
    private function assertKeyNames(array $keys)
179
    {
180
        array_map(function ($value) {
181
            $this->assertKeyName($value);
182
        }, $keys);
183
    }
184
}
185