Passed
Branch master (91bdce)
by Mehmet
02:27 queued 54s
created

APCUCache::decrement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Soupmix\Cache;
4
5
use Soupmix\Cache\Exceptions\InvalidArgumentException;
6
use Psr\SimpleCache\CacheInterface;
7
use DateInterval;
8
use DateTime;
9
10
class APCUCache implements CacheInterface
11
{
12
    const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':'];
13
14
    /**
15
     * {@inheritDoc}
16
     */
17 1
    public function get($key, $default = null)
18
    {
19 1
        $this->checkReservedCharacters($key);
20 1
        $value = apcu_fetch($key);
21 1
        return $value ?: $default;
22
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 5
    public function set($key, $value, $ttl = null)
28
    {
29 5
        $this->checkReservedCharacters($key);
30 3
        if ($ttl instanceof DateInterval) {
31 1
            $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time();
32
        }
33 3
        return apcu_store($key, $value, (int) $ttl);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_store($key, $value, (int) $ttl); of type boolean|array adds the type array to the return on line 33 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::set of type boolean.
Loading history...
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 1
    public function delete($key)
40
    {
41 1
        $this->checkReservedCharacters($key);
42 1
        return (bool) apcu_delete($key);
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 1
    public function clear()
49
    {
50 1
        return apcu_clear_cache();
51
    }
52
    /**
53
     * {@inheritDoc}
54
     */
55 1
    public function getMultiple($keys, $default = null)
56
    {
57 1
        $defaults = array_fill(0, count($keys), $default);
58 1
        foreach ($keys as $key) {
59 1
            $this->checkReservedCharacters($key);
60
        }
61 1
        return array_merge(apcu_fetch($keys), $defaults);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(apcu_fetch($keys), $defaults); (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...
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    public function setMultiple($values, $ttl = null)
68
    {
69 1
        foreach ($values as $key => $value) {
70 1
            $this->checkReservedCharacters($key);
71
        }
72 1
        if ($ttl instanceof DateInterval) {
73 1
            $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time();
74
        }
75 1
        $result =  apcu_store($values, null, $ttl);
76 1
        return empty($result);
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 1
    public function deleteMultiple($keys)
83
    {
84 1
        $ret = [];
85 1
        foreach ($keys as $key) {
86 1
            $this->checkReservedCharacters($key);
87 1
            $ret[$key] = apcu_delete($key);
88
        }
89 1
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $ret; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::deleteMultiple of type boolean.

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...
90
    }
91
92 1
    public function increment($key, $step = 1)
93
    {
94 1
        $this->checkReservedCharacters($key);
95 1
        return apcu_inc($key, $step);
96
    }
97
98 1
    public function decrement($key, $step = 1)
99
    {
100 1
        $this->checkReservedCharacters($key);
101 1
        return apcu_dec($key, $step);
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 1
    public function has($key)
108
    {
109 1
        $this->checkReservedCharacters($key);
110 1
        return apcu_exists($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_exists($key); of type boolean|string[] adds the type string[] to the return on line 110 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::has of type boolean.
Loading history...
111
    }
112
113 6
    private function checkReservedCharacters($key)
114
    {
115 6
        if (!is_string($key)) {
116 1
            $message = sprintf('key %s is not a string.', $key);
117 1
            throw new InvalidArgumentException($message);
118
        }
119 5
        foreach (self::PSR16_RESERVED_CHARACTERS as $needle) {
120 5
            if (strpos($key, $needle) !== false) {
121 1
                $message = sprintf('%s string is not a legal value.', $key);
122 1
                throw new InvalidArgumentException($message);
123
            }
124
        }
125 4
    }
126
}
127