1 | <?php |
||||
2 | /** |
||||
3 | * PHP Redis compatible |
||||
4 | * User: moyo |
||||
5 | * Date: 23/02/2018 |
||||
6 | * Time: 2:30 PM |
||||
7 | */ |
||||
8 | |||||
9 | namespace Carno\Redis\Chips; |
||||
10 | |||||
11 | trait Compatible |
||||
12 | { |
||||
13 | /** |
||||
14 | * @param string $key |
||||
15 | * @param string $val |
||||
16 | * @param mixed $more |
||||
17 | * @return bool |
||||
18 | */ |
||||
19 | public function set(string $key, string $val, $more = null) |
||||
20 | { |
||||
21 | $cmd = 'set'; |
||||
22 | |||||
23 | $args = [$key]; |
||||
24 | |||||
25 | if (is_null($more)) { |
||||
26 | // set(key, val) |
||||
27 | array_push($args, $val); |
||||
28 | } elseif (is_numeric($more)) { |
||||
29 | // set(key, val, timeout) |
||||
30 | $cmd = 'setex'; |
||||
31 | array_push($args, $more, $val); |
||||
32 | } elseif (is_array($more) && $more) { |
||||
0 ignored issues
–
show
|
|||||
33 | // set(key, val, [nx|xx, ex|px => timeout]) |
||||
34 | array_push($args, $val, $more[0] ?? 'nx'); |
||||
35 | |||||
36 | if (isset($more['ex'])) { |
||||
37 | array_push($args, 'ex', $more['ex']); |
||||
38 | } elseif (isset($more['px'])) { |
||||
39 | array_push($args, 'px', $more['px']); |
||||
40 | } |
||||
41 | } |
||||
42 | |||||
43 | return (yield $this->__call($cmd, $args)) === 'OK'; |
||||
0 ignored issues
–
show
It seems like
__call() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * @param string $key |
||||
48 | * @param array $map |
||||
49 | * @return bool |
||||
50 | */ |
||||
51 | public function hMSet(string $key, array $map) |
||||
52 | { |
||||
53 | $args = [$key]; |
||||
54 | |||||
55 | foreach ($map as $k => $v) { |
||||
56 | array_push($args, $k, $v); |
||||
57 | } |
||||
58 | |||||
59 | return (yield $this->__call('hmset', $args)) === 'OK'; |
||||
0 ignored issues
–
show
|
|||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * @param string $key |
||||
64 | * @param array $fds |
||||
65 | * @return array |
||||
66 | */ |
||||
67 | public function hMGet(string $key, array $fds) |
||||
68 | { |
||||
69 | $args = $fds; |
||||
70 | |||||
71 | array_unshift($args, $key); |
||||
72 | |||||
73 | $got = yield $this->__call('hmget', $args); |
||||
0 ignored issues
–
show
|
|||||
74 | |||||
75 | $map = []; |
||||
76 | |||||
77 | foreach ($got as $slot => $val) { |
||||
78 | $map[$fds[$slot]] = $val; |
||||
79 | } |
||||
80 | |||||
81 | return $map; |
||||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * @param string $key |
||||
86 | * @return array |
||||
87 | */ |
||||
88 | public function hGetAll(string $key) |
||||
89 | { |
||||
90 | $got = yield $this->__call('hgetall', [$key]); |
||||
0 ignored issues
–
show
|
|||||
91 | |||||
92 | $map = []; |
||||
93 | |||||
94 | for ($i = 0; $i < count($got); $i += 2) { |
||||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||||
95 | $map[$got[$i]] = $got[$i + 1]; |
||||
96 | } |
||||
97 | |||||
98 | return $map; |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * @param string $lua |
||||
103 | * @param array $args |
||||
104 | * @param int $keys |
||||
105 | * @return mixed |
||||
106 | */ |
||||
107 | public function eval(string $lua, array $args = [], int $keys = 0) |
||||
108 | { |
||||
109 | array_unshift($args, $lua, $keys); |
||||
110 | |||||
111 | return $this->__call('eval', $args); |
||||
112 | } |
||||
113 | } |
||||
114 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.