Completed
Push — master ( 81fe65...156170 )
by Ryota
04:40
created

ApcuCache::deleteMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: polidog
5
 * Date: 2017/02/23
6
 */
7
8
namespace Polidog\SsrBundle\Cache;
9
10
11
use Psr\SimpleCache\CacheInterface;
12
use Sabre\Cache\Apcu;
13
14
class ApcuCache implements CacheInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $namespace;
20
21
    /**
22
     * @var Apcu
23
     */
24
    private $apcu;
25
26
    /**
27
     * ApcuCache constructor.
28
     * @param string $namespace
29
     */
30
    public function __construct($namespace, Apcu $apcu = null)
31
    {
32
        $this->namespace = $namespace;
33
        if (null === $apcu) {
34
            $apcu = new Apcu();
35
        }
36
        $this->apcu = $apcu;
37
    }
38
39
40
    public function get($key, $default = null)
41
    {
42
        return $this->apcu->get($this->getKey($key), $default);
43
        // TODO: Implement get() method.
44
    }
45
46
    public function set($key, $value, $ttl = null)
47
    {
48
        return $this->apcu->set($this->get($key), $value, $ttl);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->apcu->set($this->get($key), $value, $ttl); of type boolean|array adds the type array to the return on line 48 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::set of type boolean.
Loading history...
49
    }
50
51
    public function delete($key)
52
    {
53
        return $this->apcu->delete($this->get($key));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->apcu->delete($this->get($key)); of type boolean|string[] adds the type string[] to the return on line 53 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::delete of type boolean.
Loading history...
54
    }
55
56
    public function clear()
57
    {
58
        return $this->apcu->clear();
59
    }
60
61
    public function getMultiple($keys, $default = null)
62
    {
63
        return $this->apcu->getMultiple($this->getMultipleKeys($keys), $default);
0 ignored issues
show
Documentation introduced by
$this->getMultipleKeys($keys) is of type array, but the function expects a object<Sabre\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Best Practice introduced by
The return type of return $this->apcu->getM...Keys($keys), $default); (Generator) 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...
64
    }
65
66
    public function setMultiple($values, $ttl = null)
67
    {
68
        return $this->apcu->setMultiple($this->iteratorMultiple($values), $ttl);
0 ignored issues
show
Documentation introduced by
$this->iteratorMultiple($values) is of type object<Generator>, but the function expects a object<Sabre\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Compatibility introduced by
The expression $this->apcu->setMultiple...ltiple($values), $ttl); of type boolean|array adds the type array to the return on line 68 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::setMultiple of type boolean.
Loading history...
69
    }
70
71
    public function deleteMultiple($keys)
72
    {
73
        return $this->apcu->deleteMultiple($this->getMultipleKeys($keys));
0 ignored issues
show
Documentation introduced by
$this->getMultipleKeys($keys) is of type array, but the function expects a object<Sabre\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Compatibility introduced by
The expression $this->apcu->deleteMulti...etMultipleKeys($keys)); of type boolean|string[] adds the type string[] to the return on line 73 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::deleteMultiple of type boolean.
Loading history...
74
    }
75
76
    public function has($key)
77
    {
78
        return $this->apcu->has($this->getKey($key));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->apcu->has($this->getKey($key)); of type boolean|string[] adds the type string[] to the return on line 78 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::has of type boolean.
Loading history...
79
    }
80
81
    private function getKey($key)
82
    {
83
        return $this->namespace . '_' . $key;
84
    }
85
86
    private function getMultipleKeys($keys)
87
    {
88
        $_keys = [];
89
        foreach ($keys as $key) {
90
            $_keys[] = $this->get($key);
91
        }
92
        return $_keys;
93
    }
94
95
    private function iteratorMultiple($values)
96
    {
97
        foreach ($values as $key => $value) {
98
            yield $this->getKey($key) => $value;
99
        }
100
    }
101
102
103
104
}