Failed Conditions
Push — v3.x ( 1f61fa...b0fa1f )
by Chad
02:14
created

src/Cache/NullCache.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Chadicus\Marvel\Api\Cache;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
/**
8
 * A PSR-16 implementation which does not save or store any data.
9
 */
10
final class NullCache extends AbstractCache implements CacheInterface
11
{
12
    /**
13
     * Fetches a value from the cache.
14
     *
15
     * @param string $key     The unique key of this item in the cache.
16
     * @param mixed  $default Default value to return if the key does not exist.
17
     *
18
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
19
     *
20
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
21
     */
22
    public function get($key, $default = null)
23
    {
24
        $this->verifyKey($key);
25
        return $default;
26
    }
27
28
    /**
29
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
30
     *
31
     * @param string                $key   The key of the item to store.
32
     * @param mixed                 $value The value of the item to store, must be serializable.
33
     * @param null|int|DateInterval $ttl   Optional. The TTL value of this item. If no value is sent and
34
     *                                     the driver supports TTL then the library may set a default value
35
     *                                     for it or let the driver take care of that.
36
     *
37
     * @return bool True on success and false on failure.
38
     *
39
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
40
     */
41
    public function set($key, $value, $ttl = null)
42
    {
43
        $this->verifyKey($key);
44
        return true;
45
    }
46
47
    /**
48
     * Delete an item from the cache by its unique key.
49
     *
50
     * @param string $key The unique cache key of the item to delete.
51
     *
52
     * @return bool True if the item was successfully removed. False if there was an error.
53
     *
54
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
55
     */
56
    public function delete($key)
57
    {
58
        $this->verifyKey($key);
59
        return true;
60
    }
61
62
    /**
63
     * Wipes clean the entire cache's keys.
64
     *
65
     * @return bool True on success and false on failure.
66
     */
67
    public function clear()
68
    {
69
        return true;
70
    }
71
72
    /**
73
     * Obtains multiple cache items by their unique keys.
74
     *
75
     * @param iterable $keys    A list of keys that can obtained in a single operation.
76
     * @param mixed    $default Default value to return for keys that do not exist.
77
     *
78
     * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
79
     *
80
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
81
     */
82
    public function getMultiple($keys, $default = null)
83
    {
84
        array_walk($keys, [$this, 'verifyKey']);
85
86
        $items = [];
87
        foreach ($keys as $key) {
88
            $items[$key] = $default;
89
        }
90
91
        return $items;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $items; (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
    }
93
94
    /**
95
     * Persists a set of key => value pairs in the cache, with an optional TTL.
96
     *
97
     * @param iterable              $values A list of key => value pairs for a multiple-set operation.
98
     * @param null|int|DateInterval $ttl    Optional. The TTL value of this item. If no value is sent and
99
     *                                      the driver supports TTL then the library may set a default value
100
     *                                      for it or let the driver take care of that.
101
     *
102
     * @return bool True on success and false on failure.
103
     *
104
     * @throws InvalidArgumentException Thrown if $values is neither an array nor a Traversable,
105
     *                                  or if any of the $values are not a legal value.
106
     */
107
    public function setMultiple($values, $ttl = null)
108
    {
109
        $keys = array_keys($values);
110
        array_walk($keys, [$this, 'verifyKey']);
111
        return true;
112
    }
113
114
    /**
115
     * Deletes multiple cache items in a single operation.
116
     *
117
     * @param iterable $keys A list of string-based keys to be deleted.
118
     *
119
     * @return bool True if the items were successfully removed. False if there was an error.
120
     *
121
     * @throws InvalidArgumentException Thrown if $keys is neither an array nor a Traversable,
122
     *                                  or if any of the $keys are not a legal value.
123
     */
124
    public function deleteMultiple($keys)
125
    {
126
        array_walk($keys, [$this, 'verifyKey']);
127
        return true;
128
    }
129
130
    /**
131
     * Determines whether an item is present in the cache.
132
     *
133
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
134
     * and not to be used within your live applications operations for get/set, as this method
135
     * is subject to a race condition where your has() will return true and immediately after,
136
     * another script can remove it making the state of your app out of date.
137
     *
138
     * @param string $key The cache item key.
139
     *
140
     * @return bool
141
     *
142
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
143
     */
144
    public function has($key)
145
    {
146
        $this->verifyKey($key);
147
        return false;
148
    }
149
}
150