Completed
Pull Request — master (#21)
by Michal
02:30
created

RedisDataManager   C

Complexity

Total Complexity 78

Size/Duplication

Total Lines 337
Duplicated Lines 11.57 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 1.21%

Importance

Changes 0
Metric Value
wmc 78
lcom 1
cbo 4
dl 39
loc 337
ccs 3
cts 247
cp 0.0121
rs 5.4563
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A databases() 0 11 2
B tables() 0 39 6
D itemsCount() 19 92 22
D items() 20 102 28
A deleteItem() 0 13 4
A deleteTable() 0 4 1
A selectDatabase() 0 4 1
A execute() 0 19 2
B headers() 0 16 6
B getItems() 0 16 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like RedisDataManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RedisDataManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace UniMan\Drivers\Redis;
4
5
use UniMan\Core\DataManager\AbstractDataManager;
6
use UniMan\Core\Utils\Multisort;
7
use UniMan\Core\Utils\Filter;
8
use RedisProxy\RedisProxy;
9
10
class RedisDataManager extends AbstractDataManager
11
{
12
    private $connection;
13
14
    private $itemsCountCache = false;
15
16 2
    public function __construct(RedisProxy $connection)
17
    {
18 2
        $this->connection = $connection;
19 2
    }
20
21
    public function databases(array $sorting = [])
22
    {
23
        $keyspace = $this->connection->info('keyspace');
24
        $databases = [];
25
        foreach ($keyspace as $db => $info) {
26
            $db = str_replace('db', '', $db);
27
            $info['database'] = $db;
28
            $databases[$db] = $info;
29
        }
30
        return Multisort::sort($databases, $sorting);
31
    }
32
33
    public function tables(array $sorting = [])
34
    {
35
        $tables = [
36
            RedisDriver::TYPE_KEY => [
37
                'all' => [
38
                    'key' => 'Show all keys',
39
                ]
40
            ],
41
            RedisDriver::TYPE_HASH => [],
42
            RedisDriver::TYPE_SET => [],
43
        ];
44
        $commands = [
45
            'hLen' => RedisDriver::TYPE_HASH,
46
            'sCard' => RedisDriver::TYPE_SET,
47
        ];
48
        foreach ($this->connection->keys('*') as $key) {
49
            foreach ($commands as $command => $label) {
50
                $result = $this->connection->$command($key);
51
                if ($this->connection->getLastError() !== null) {
52
                    $this->connection->clearLastError();
53
                    continue;
54
                }
55
                $tables[$label][$key] = [
56
                    'key' => $key
57
                ];
58
                if ($label == RedisDriver::TYPE_HASH) {
59
                    $tables[$label][$key]['number_of_fields'] = $result;
60
                } elseif ($label == RedisDriver::TYPE_SET) {
61
                    $tables[$label][$key]['number_of_members'] = $result;
62
                }
63
                break;
64
            }
65
        }
66
        return [
67
            RedisDriver::TYPE_KEY => Multisort::sort($tables[RedisDriver::TYPE_KEY], $sorting),
68
            RedisDriver::TYPE_HASH => Multisort::sort($tables[RedisDriver::TYPE_HASH], $sorting),
69
            RedisDriver::TYPE_SET => Multisort::sort($tables[RedisDriver::TYPE_SET], $sorting),
70
        ];
71
    }
72
73
    public function itemsCount($type, $table, array $filter = [])
74
    {
75
        if ($this->itemsCountCache !== false) {
76
            return $this->itemsCountCache;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->itemsCountCache; (boolean) is incompatible with the return type declared by the interface UniMan\Core\DataManager\...erInterface::itemsCount of type integer.

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...
77
        }
78
        if ($type == RedisDriver::TYPE_HASH) {
79
            if (!$filter) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filter of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
80
                $this->itemsCountCache = $this->connection->hLen($table);
0 ignored issues
show
Documentation Bug introduced by
The property $itemsCountCache was declared of type boolean, but $this->connection->hLen($table) is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
81
                return $this->itemsCountCache;
82
            } else {
83
                $totalItems = 0;
84
                foreach ($filter as $filterParts) {
85
                    if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) {
86
                        $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]);
87
                        if ($res) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
                            $item = [
89
                                'key' => $filterParts['key'][Filter::OPERATOR_EQUAL],
90
                                'length' => strlen($res),
91
                                'value' => $res,
92
                            ];
93
                            if (Filter::apply($item, $filter)) {
94
                                $totalItems++;
95
                            }
96
                        }
97
                        $this->itemsCountCache = $totalItems;
0 ignored issues
show
Documentation Bug introduced by
The property $itemsCountCache was declared of type boolean, but $totalItems is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
98
                        return $this->itemsCountCache;
99
                    }
100
                }
101
                $iterator = '';
102
                do {
103
                    $pattern = null;
104
                    $res = $this->connection->hscan($table, $iterator, $pattern, 1000);
105
                    $res = $res ?: [];
106 View Code Duplication
                    foreach ($res as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Bug introduced by
The expression $res of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
107
                        $item = [
108
                            'key' => $key,
109
                            'length' => strlen($value),
110
                            'value' => $value,
111
                        ];
112
                        if (Filter::apply($item, $filter)) {
113
                            $totalItems++;
114
                        }
115
                    }
116
                } while ($iterator !== 0);
117
                $this->itemsCountCache = $totalItems;
118
                return $this->itemsCountCache;
119
            }
120
        }
121
        if ($type == RedisDriver::TYPE_KEY) {
122
            $totalItems = 0;
123
            foreach ($this->connection->keys('*') as $key) {
124
                $result = $this->connection->get($key);
125
                if ($this->connection->getLastError() !== null) {
126
                    $this->connection->clearLastError();
127
                    continue;
128
                }
129
130
                $item = [
131
                    'key' => $key,
132
                    'value' => $result,
133
                    'length' => strlen($result),
134
                ];
135
136
                if (Filter::apply($item, $filter)) {
137
                    $totalItems++;
138
                }
139
            }
140
            return $totalItems;
141
        }
142
        if ($type == RedisDriver::TYPE_SET) {
143
            if (!$filter) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filter of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
144
                return $this->connection->scard($table);
145
            }
146
            $iterator = '';
147
            $totalItems = 0;
148
            do {
149
                $res = $this->connection->sscan($table, $iterator, null, 1000);
150
                $res = $res ?: [];
151 View Code Duplication
                foreach ($res as $member) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Bug introduced by
The expression $res of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
152
                    $item = [
153
                        'member' => $member,
154
                        'length' => strlen($member),
155
                    ];
156
                    if (Filter::apply($item, $filter)) {
157
                        $totalItems++;
158
                    }
159
                }
160
            } while ($iterator !== 0);
161
            return $totalItems;
162
        }
163
        return 0;
164
    }
165
166
    public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = [])
167
    {
168
        $items = [];
169
        $offset = ($page - 1) * $onPage;
170
        $skipped = 0;
171
        if ($type == RedisDriver::TYPE_HASH) {
172
            foreach ($filter as $filterParts) {
173
                if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) {
174
                    $items = [];
175
                    $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]);
176
                    if ($res) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
177
                        $item = [
178
                            'key' => $filterParts['key'][Filter::OPERATOR_EQUAL],
179
                            'length' => strlen($res),
180
                            'value' => $res,
181
                        ];
182
                        if (Filter::apply($item, $filter)) {
183
                            $items[$item['key']] = $item;
184
                        }
185
                    }
186
                    return $items;
187
                }
188
            }
189
190
191
            $iterator = '';
192
            do {
193
                $pattern = null;
194
                $res = $this->connection->hscan($table, $iterator, $pattern, $onPage * 10);
195
                $res = $res ?: [];
196
                foreach ($res as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $res of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
197
                    $item = [
198
                        'key' => $key,
199
                        'length' => strlen($value),
200
                        'value' => $value,
201
                    ];
202 View Code Duplication
                    if (Filter::apply($item, $filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
                        if ($skipped < $offset) {
204
                            $skipped++;
205
                        } else {
206
                            $items[$key] = $item;
207
                            if (count($items) === $onPage) {
208
                                break;
209
                            }
210
                        }
211
                    }
212
                }
213
            } while ($iterator !== 0 && count($items) < $onPage);
214
        } elseif ($type == RedisDriver::TYPE_KEY) {
215
            foreach ($this->connection->keys('*') as $key) {
216
                $result = $this->connection->get($key);
217
                if ($this->connection->getLastError() !== null) {
218
                    $this->connection->clearLastError();
219
                    continue;
220
                }
221
222
                $item = [
223
                    'key' => $key,
224
                    'value' => $result,
225
                    'length' => strlen($result),
226
                ];
227
228 View Code Duplication
                if (Filter::apply($item, $filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
                    if ($skipped < $offset) {
230
                        $skipped++;
231
                    } else {
232
                        $items[$key] = $item;
233
                        if (count($items) === $onPage) {
234
                            break;
235
                        }
236
                    }
237
                }
238
            }
239
        } elseif ($type == RedisDriver::TYPE_SET) {
240
            $iterator = '';
241
            do {
242
                $pattern = null;
243
                $res = $this->connection->sscan($table, $iterator, $pattern, $onPage * 10);
244
                $res = $res ?: [];
245
                foreach ($res as $member) {
0 ignored issues
show
Bug introduced by
The expression $res of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
246
                    $item = [
247
                        'member' => $member,
248
                        'length' => strlen($member),
249
                    ];
250
                    if (Filter::apply($item, $filter)) {
251
                        $items[$member] = $item;
252
                        if (count($items) === $onPage) {
253
                            break;
254
                        }
255
                    }
256
                }
257
            } while ($iterator !== 0 && count($items) < $onPage);
258
        }
259
260
        if ($this->itemsCount($type, $table, $filter) <= $onPage) {
261
            $items = Multisort::sort($items, $sorting);
262
        } elseif ($sorting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sorting of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
263
            $this->addMessage('Sorting has not been applied because the number of items is greater then the limit. Increase the limit or modify the filter.');
264
        }
265
266
        return $items;
267
    }
268
269
    public function deleteItem($type, $table, $item)
270
    {
271
        if ($type == RedisDriver::TYPE_HASH) {
272
            return $this->connection->hdel($table, $item);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->connection->hdel($table, $item); (integer) is incompatible with the return type declared by the interface UniMan\Core\DataManager\...erInterface::deleteItem of type boolean|null.

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...
273
        }
274
        if ($type == RedisDriver::TYPE_KEY) {
275
            return $this->connection->del($item);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->connection->del($item); (integer) is incompatible with the return type declared by the interface UniMan\Core\DataManager\...erInterface::deleteItem of type boolean|null.

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...
276
        }
277
        if ($type == RedisDriver::TYPE_SET) {
278
            return $this->connection->srem($table, $item);
279
        }
280
        return parent::deleteItem($type, $table, $item);
281
    }
282
283
    public function deleteTable($type, $table)
284
    {
285
        return $this->connection->del($table);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->connection->del($table); (integer) is incompatible with the return type declared by the interface UniMan\Core\DataManager\...rInterface::deleteTable of type boolean|null.

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...
286
    }
287
288
    public function selectDatabase($database)
289
    {
290
        $this->connection->select($database);
291
    }
292
293
    public function execute($commands)
294
    {
295
        $listOfCommands = array_filter(array_map('trim', explode("\n", $commands)), function ($command) {
296
            return $command;
297
        });
298
299
        $results = [];
300
        foreach ($listOfCommands as $command) {
301
            $commandParts = explode(' ', $command);
302
            $function = array_shift($commandParts);
303
            $function = strtolower($function);
304
            $results[$command]['headers'] = $this->headers($function);
305
            $rows = call_user_func_array([$this->connection, $function], $commandParts);
306
            $items = $this->getItems($function, $rows);
307
            $results[$command]['items'] = $items;
308
            $results[$command]['count'] = count($items);
309
        }
310
        return $results;
311
    }
312
313
    private function headers($function)
314
    {
315
        if ($function === 'get' || $function === 'hget') {
316
            return ['value'];
317
        }
318
        if ($function === 'keys') {
319
            return ['key'];
320
        }
321
        if ($function === 'hgetall') {
322
            return ['key', 'value'];
323
        }
324
        if ($function === 'hlen') {
325
            return ['items_count'];
326
        }
327
        return [];
328
    }
329
330
    private function getItems($function, $rows)
331
    {
332
        $items = [];
333
        if ($function === 'keys') {
334
            foreach ($rows as $key) {
335
                $items[] = [$key];
336
            }
337
        } elseif ($function === 'hgetall') {
338
            foreach ($rows as $key => $value) {
339
                $items[] = [$key, $value];
340
            }
341
        } else {
342
            return [[$rows]];
343
        }
344
        return $items;
345
    }
346
}
347