Passed
Push — master ( 4750e8...7ce0c8 )
by Christoffer
02:11
created

RuntimeCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A has() 0 3 1
A set() 0 4 1
A deleteMultiple() 0 4 2
A getMultiple() 0 5 1
A delete() 0 4 1
A get() 0 3 1
A setMultiple() 0 4 2
1
<?php
2
3
namespace Digia\GraphQL\Cache;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
class RuntimeCache implements CacheInterface
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $cache = [];
14
15
    /**
16
     * @inheritdoc
17
     */
18
    public function get($key, $default = null)
19
    {
20
        return $this->cache[$key] ?? $default;
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function has($key): bool
27
    {
28
        return isset($this->cache[$key]);
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function delete($key): bool
35
    {
36
        unset($this->cache[$key]);
37
        return true;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function set($key, $value, $ttl = null): bool
44
    {
45
        $this->cache[$key] = $value;
46
        return true;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function clear()
53
    {
54
        return $this->cache = [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cache = array() returns the type array which is incompatible with the return type mandated by Psr\SimpleCache\CacheInterface::clear() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getMultiple($keys, $default = null)
61
    {
62
        return array_filter($this->cache, function ($key) use ($keys) {
63
            return \in_array($key, $keys, true);
64
        }, ARRAY_FILTER_USE_KEY);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function setMultiple($values, $ttl = null)
71
    {
72
        foreach ($values as $key => $value) {
73
            $this->set($key, $value);
74
        }
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function deleteMultiple($keys)
81
    {
82
        foreach ($keys as $key) {
83
            $this->delete($key);
84
        }
85
    }
86
}
87