Test Failed
Push — master ( d844b5...1ec8b0 )
by Joao
34s
created

BaseCacheEngine::getMultiple()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace ByJG\Cache\Psr16;
4
5
use ByJG\Cache\CacheAvailabilityInterface;
6
use ByJG\Cache\InvalidArgumentException;
7
use Psr\SimpleCache\CacheInterface;
8
9
abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInterface
10
{
11
    public function getMultiple($keys, $default = null)
12
    {
13
        if (!is_array($keys)) {
14
            throw new InvalidArgumentException('getMultipleKeys expected an array');
15
        }
16
        $result = [];
17
        foreach ($keys as $key) {
18
            $result[$key] = $this->get($key, $default);
19
        }
20
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (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...
21
    }
22
23
    public function setMultiple($values, $ttl = null)
24
    {
25
        foreach ($values as $key => $value) {
26
            $this->set($key, $value, $ttl);
27
        }
28
    }
29
30
    public function deleteMultiple($keys)
31
    {
32
        foreach ($keys as $key) {
33
            $this->delete($key);
34
        }
35
    }
36
37
    abstract public function isAvailable();
38
39
    protected function addToNow($ttl)
40
    {
41
        if (is_numeric($ttl)) {
42
            return strtotime("+$ttl second");
43
        }
44
45
        if ($ttl instanceof \DateInterval) {
46
            $now = new \DateTime();
47
            $now->add($ttl);
48
            return $now->getTimestamp();
49
        }
50
51
        return null;
52
    }
53
}