Completed
Push — develop ( 75d3bd...a06533 )
by Adrien
21:48
created

Memory::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Collection;
4
5
/**
6
 * This is the default implementation for in-memory cell collection.
7
 *
8
 * Alternatives implementation should leverage off-memory, non-volatile storage
9
 * to reduce overall memory usage.
10
 */
11
class Memory implements \Psr\SimpleCache\CacheInterface
12
{
13
    private $cache = [];
14
15
    public function clear()
16
    {
17
        $this->cache = [];
18
19
        return true;
20
    }
21
22 18
    public function delete($key)
23
    {
24 18
        unset($this->cache[$key]);
25
26 18
        return true;
27
    }
28
29 2
    public function deleteMultiple($keys)
30
    {
31 2
        foreach ($keys as $key) {
32 1
            $this->delete($key);
33
        }
34
35 2
        return true;
36
    }
37
38 70
    public function get($key, $default = null)
39
    {
40 70
        if ($this->has($key)) {
41 69
            return $this->cache[$key];
42
        }
43
44 1
        return $default;
45
    }
46
47 2
    public function getMultiple($keys, $default = null)
48
    {
49 2
        $results = [];
50 2
        foreach ($keys as $key) {
51 2
            $results[$key] = $this->get($key, $default);
52
        }
53
54 2
        return $results;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $results; (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...
55
    }
56
57 70
    public function has($key)
58
    {
59 70
        return array_key_exists($key, $this->cache);
60
    }
61
62 72
    public function set($key, $value, $ttl = null)
63
    {
64 72
        $this->cache[$key] = $value;
65
66 72
        return true;
67
    }
68
69 2
    public function setMultiple($values, $ttl = null)
70
    {
71 2
        foreach ($values as $key => $value) {
72 2
            $this->set($key, $value);
73
        }
74
75 2
        return true;
76
    }
77
}
78