This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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 Soupmix\Cache; |
||
4 | |||
5 | use Soupmix\Cache\Exceptions\InvalidArgumentException; |
||
6 | use Psr\SimpleCache\CacheInterface; |
||
7 | use Redis; |
||
8 | use DateInterval; |
||
9 | use DateTime; |
||
10 | |||
11 | class RedisCache implements CacheInterface |
||
12 | { |
||
13 | const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':']; |
||
14 | |||
15 | private $handler; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * Connect to Redis service |
||
20 | * |
||
21 | * @param Redis $handler Configuration values that has dbIndex name and host's IP address |
||
22 | * |
||
23 | */ |
||
24 | 8 | public function __construct(Redis $handler) |
|
25 | { |
||
26 | 8 | if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) { |
|
27 | $handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); |
||
28 | } |
||
29 | 8 | $this->handler = $handler; |
|
30 | 8 | } |
|
31 | |||
32 | 1 | public function getConnection() |
|
33 | { |
||
34 | 1 | return $this->handler; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | 1 | public function get($key, $default = null) |
|
41 | { |
||
42 | 1 | $value = $this->handler->get($key); |
|
43 | 1 | return $value ? $value : $default; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | 5 | public function set($key, $value, $ttl = null) |
|
50 | { |
||
51 | 5 | $this->checkKeysValidity($key); |
|
52 | 3 | if ($ttl instanceof DateInterval) { |
|
53 | 1 | $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
|
54 | } |
||
55 | 3 | $setTtl = (int) $ttl; |
|
56 | 3 | if ($setTtl === 0) { |
|
57 | 1 | return $this->handler->set($key, $value); |
|
58 | } |
||
59 | 2 | return $this->handler->setex($key, $ttl, $value); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | 2 | public function delete($key) |
|
66 | { |
||
67 | 2 | return (bool) $this->handler->delete($key); |
|
68 | } |
||
69 | /** |
||
70 | * {@inheritDoc} |
||
71 | */ |
||
72 | 8 | public function clear() |
|
73 | { |
||
74 | 8 | return $this->handler->flushDB(); |
|
75 | } |
||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | 1 | public function getMultiple($keys, $default = null) |
|
80 | { |
||
81 | 1 | $defaults = array_fill(0, count($keys), $default); |
|
82 | 1 | foreach ($keys as $key) { |
|
83 | 1 | $this->checkKeysValidity($key); |
|
84 | } |
||
85 | 1 | return array_merge(array_combine($keys, $this->handler->mget($keys)), $defaults); |
|
0 ignored issues
–
show
|
|||
86 | } |
||
87 | /** |
||
88 | * {@inheritDoc} |
||
89 | */ |
||
90 | 1 | public function setMultiple($values, $ttl = null) |
|
91 | { |
||
92 | 1 | foreach ($values as $key => $value) { |
|
93 | 1 | $this->checkKeysValidity($key); |
|
94 | } |
||
95 | 1 | if ($ttl instanceof DateInterval) { |
|
96 | 1 | $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
|
97 | } |
||
98 | 1 | $setTtl = (int) $ttl; |
|
99 | 1 | if ($setTtl === 0) { |
|
100 | 1 | return $this->handler->mset($values); |
|
101 | } |
||
102 | 1 | $return = true; |
|
103 | 1 | foreach ($values as $key => $value) { |
|
104 | 1 | $return = $return && $this->set($key, $value, $setTtl); |
|
105 | |||
106 | } |
||
107 | 1 | return $return; |
|
108 | } |
||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | 1 | public function deleteMultiple($keys) |
|
113 | { |
||
114 | 1 | foreach ($keys as $key) { |
|
115 | 1 | $this->checkKeysValidity($key); |
|
116 | } |
||
117 | 1 | $return =[]; |
|
118 | 1 | foreach ($keys as $key) { |
|
119 | 1 | $return[$key] = (bool) $this->delete($key); |
|
120 | } |
||
121 | 1 | return $return; |
|
0 ignored issues
–
show
The return type of
return $return; (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 ![]() |
|||
122 | } |
||
123 | /** |
||
124 | * Increment a value atomically in the cache by its step value, which defaults to 1 |
||
125 | * |
||
126 | * @param string $key The cache item key |
||
127 | * @param integer $step The value to increment by, defaulting to 1 |
||
128 | * |
||
129 | * @return int|bool The new value on success and false on failure |
||
130 | */ |
||
131 | 1 | public function increment($key, $step = 1) |
|
132 | { |
||
133 | 1 | return $this->handler->incr($key, $step); |
|
134 | } |
||
135 | /** |
||
136 | * Decrement a value atomically in the cache by its step value, which defaults to 1 |
||
137 | * |
||
138 | * @param string $key The cache item key |
||
139 | * @param integer $step The value to decrement by, defaulting to 1 |
||
140 | * |
||
141 | * @return int|bool The new value on success and false on failure |
||
142 | */ |
||
143 | 1 | public function decrement($key, $step = 1) |
|
144 | { |
||
145 | 1 | return $this->handler->decr($key, $step); |
|
146 | } |
||
147 | |||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * {@inheritDoc} |
||
153 | */ |
||
154 | 1 | public function has($key) |
|
155 | { |
||
156 | 1 | $this->checkKeysValidity($key); |
|
157 | 1 | return $this->handler->exists($key); |
|
158 | } |
||
159 | |||
160 | 5 | private function checkKeysValidity($key) |
|
161 | { |
||
162 | 5 | if (!is_string($key)) { |
|
163 | 1 | $message = sprintf('Key %s is not a string.', $key); |
|
164 | 1 | throw new InvalidArgumentException($message); |
|
165 | } |
||
166 | 4 | foreach (self::PSR16_RESERVED_CHARACTERS as $needle) { |
|
167 | 4 | if (strpos($key, $needle) !== false) { |
|
168 | 1 | $message = sprintf('Key %s has not a legal value.', $key); |
|
169 | 1 | throw new InvalidArgumentException($message); |
|
170 | } |
||
171 | } |
||
172 | 3 | } |
|
173 | } |
||
174 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.