Completed
Pull Request — master (#23)
by Michal
08:35
created

RedisDataManager::tables()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 28
cp 0
rs 9.0168
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
3
namespace UniMan\Drivers\Redis;
4
5
use RedisProxy\RedisProxy;
6
use UniMan\Core\DataManager\AbstractDataManager;
7
use UniMan\Core\Utils\Filter;
8
use UniMan\Core\Utils\Multisort;
9
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage;
10
11
class RedisDataManager extends AbstractDataManager
12
{
13
    private $connection;
14
15
    private $databaseAliasStorage;
16
17
    private $itemsCountCache = false;
18
19 2
    public function __construct(RedisProxy $connection, RedisDatabaseAliasStorage $databaseAliasStorage)
20
    {
21 2
        $this->connection = $connection;
22 2
        $this->databaseAliasStorage = $databaseAliasStorage;
23 2
    }
24
25
    public function databases(array $sorting = [])
26
    {
27
        $keyspace = $this->connection->info('keyspace');
28
        $aliases = $this->databaseAliasStorage->loadAll();
29
        $databases = [];
30
        foreach ($keyspace as $db => $info) {
31
            $db = str_replace('db', '', $db);
32
            $alias = isset($aliases[$db]) ? ' (' . $aliases[$db] . ')' : '';
33
            $info['database'] = $db . $alias;
34
            $databases[$db] = $info;
35
        }
36
        return Multisort::sort($databases, $sorting);
37
    }
38
39
    protected function getDatabaseNameColumn()
40
    {
41
        return 'database';
42
    }
43
44
    public function tables(array $sorting = [])
45
    {
46
        $tables = [
47
            RedisDriver::TYPE_KEY => [
48
                'list_of_all_keys' => [
49
                    'key' => 'Show all keys',
50
                    'number_of_keys' => 0,
51
                ]
52
            ],
53
            RedisDriver::TYPE_HASH => [],
54
            RedisDriver::TYPE_SET => [],
55
        ];
56
        foreach ($this->connection->keys('*') as $key) {
57
            $type = $this->connection->type($key);
58
            if ($type === RedisProxy::TYPE_STRING) {
59
                $tables[RedisDriver::TYPE_KEY]['list_of_all_keys']['number_of_keys']++;
60
            } elseif ($type === RedisProxy::TYPE_HASH) {
61
                $result = $this->connection->hlen($key);
62
                $tables[RedisDriver::TYPE_HASH][$key] = [
63
                    'key' => $key,
64
                    'number_of_fields' => $result,
65
                ];
66
            } elseif ($type === RedisProxy::TYPE_SET) {
67
                $result = $this->connection->scard($key);
68
                $tables[RedisDriver::TYPE_SET][$key] = [
69
                    'key' => $key,
70
                    'number_of_members' => $result,
71
                ];
72
            }
73
            // TODO list and sorted set
74
        }
75
        return [
76
            RedisDriver::TYPE_KEY => Multisort::sort($tables[RedisDriver::TYPE_KEY], $sorting),
77
            RedisDriver::TYPE_HASH => Multisort::sort($tables[RedisDriver::TYPE_HASH], $sorting),
78
            RedisDriver::TYPE_SET => Multisort::sort($tables[RedisDriver::TYPE_SET], $sorting),
79
        ];
80
    }
81
82
    public function itemsCount($type, $table, array $filter = [])
83
    {
84
        if ($this->itemsCountCache !== false) {
85
            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...
86
        }
87
        if ($type == RedisDriver::TYPE_HASH) {
88
            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...
89
                $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...
90
                return $this->itemsCountCache;
91
            } else {
92
                $totalItems = 0;
93
                foreach ($filter as $filterParts) {
94
                    if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) {
95
                        $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]);
96
                        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...
97
                            $item = [
98
                                'key' => $filterParts['key'][Filter::OPERATOR_EQUAL],
99
                                'length' => strlen($res),
100
                                'value' => $res,
101
                            ];
102
                            if (Filter::apply($item, $filter)) {
103
                                $totalItems++;
104
                            }
105
                        }
106
                        $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...
107
                        return $this->itemsCountCache;
108
                    }
109
                }
110
                $iterator = '';
111
                do {
112
                    $pattern = null;
113
                    $res = $this->connection->hscan($table, $iterator, $pattern, 1000);
114
                    $res = $res ?: [];
115 View Code Duplication
                    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...
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...
116
                        $item = [
117
                            'key' => $key,
118
                            'length' => strlen($value),
119
                            'value' => $value,
120
                        ];
121
                        if (Filter::apply($item, $filter)) {
122
                            $totalItems++;
123
                        }
124
                    }
125
                } while ($iterator !== 0);
126
                $this->itemsCountCache = $totalItems;
127
                return $this->itemsCountCache;
128
            }
129
        }
130
        if ($type == RedisDriver::TYPE_KEY) {
131
            $totalItems = 0;
132
            foreach ($this->connection->keys('*') as $key) {
133
                var_dump($key);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($key); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
134
                var_dump($this->connection->type($key));
135
                if (!$this->connection->type($key) !== RedisProxy::TYPE_STRING) {
136
                    continue;
137
                }
138
                $result = $this->connection->get($key);
139
                $item = [
140
                    'key' => $key,
141
                    'value' => $result,
142
                    'length' => strlen($result),
143
                ];
144
145
                if (Filter::apply($item, $filter)) {
146
                    $totalItems++;
147
                }
148
            }
149
            return $totalItems;
150
        }
151
        if ($type == RedisDriver::TYPE_SET) {
152
            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...
153
                return $this->connection->scard($table);
154
            }
155
            $iterator = '';
156
            $totalItems = 0;
157
            do {
158
                $res = $this->connection->sscan($table, $iterator, null, 1000);
159
                $res = $res ?: [];
160 View Code Duplication
                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...
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...
161
                    $item = [
162
                        'member' => $member,
163
                        'length' => strlen($member),
164
                    ];
165
                    if (Filter::apply($item, $filter)) {
166
                        $totalItems++;
167
                    }
168
                }
169
            } while ($iterator !== 0);
170
            return $totalItems;
171
        }
172
        return 0;
173
    }
174
175
    public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = [])
176
    {
177
        $items = [];
178
        $offset = ($page - 1) * $onPage;
179
        $skipped = 0;
180
        if ($type == RedisDriver::TYPE_HASH) {
181
            foreach ($filter as $filterParts) {
182
                if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) {
183
                    $items = [];
184
                    $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]);
185
                    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...
186
                        $item = [
187
                            'key' => $filterParts['key'][Filter::OPERATOR_EQUAL],
188
                            'length' => strlen($res),
189
                            'value' => $res,
190
                        ];
191
                        if (Filter::apply($item, $filter)) {
192
                            $items[$item['key']] = $item;
193
                        }
194
                    }
195
                    return $items;
196
                }
197
            }
198
199
200
            $iterator = '';
201
            do {
202
                $pattern = null;
203
                $res = $this->connection->hscan($table, $iterator, $pattern, $onPage * 10);
204
                $res = $res ?: [];
205
                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...
206
                    $item = [
207
                        'key' => $key,
208
                        'length' => strlen($value),
209
                        'value' => $value,
210
                    ];
211 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...
212
                        if ($skipped < $offset) {
213
                            $skipped++;
214
                        } else {
215
                            $items[$key] = $item;
216
                            if (count($items) === $onPage) {
217
                                break;
218
                            }
219
                        }
220
                    }
221
                }
222
            } while ($iterator !== 0 && count($items) < $onPage);
223
        } elseif ($type == RedisDriver::TYPE_KEY) {
224
            foreach ($this->connection->keys('*') as $key) {
225
                $result = $this->connection->get($key);
226
                if ($this->connection->getLastError() !== null) {
227
                    $this->connection->clearLastError();
228
                    continue;
229
                }
230
231
                $item = [
232
                    'key' => $key,
233
                    'value' => $result,
234
                    'length' => strlen($result),
235
                ];
236
237 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...
238
                    if ($skipped < $offset) {
239
                        $skipped++;
240
                    } else {
241
                        $items[$key] = $item;
242
                        if (count($items) === $onPage) {
243
                            break;
244
                        }
245
                    }
246
                }
247
            }
248
        } elseif ($type == RedisDriver::TYPE_SET) {
249
            $iterator = '';
250
            do {
251
                $pattern = null;
252
                $res = $this->connection->sscan($table, $iterator, $pattern, $onPage * 10);
253
                $res = $res ?: [];
254
                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...
255
                    $item = [
256
                        'member' => $member,
257
                        'length' => strlen($member),
258
                    ];
259
                    if (Filter::apply($item, $filter)) {
260
                        $items[$member] = $item;
261
                        if (count($items) === $onPage) {
262
                            break;
263
                        }
264
                    }
265
                }
266
            } while ($iterator !== 0 && count($items) < $onPage);
267
        }
268
269
        if ($this->itemsCount($type, $table, $filter) <= $onPage) {
270
            $items = Multisort::sort($items, $sorting);
271
        } 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...
272
            $this->addMessage('Sorting has not been applied because the number of items is greater then the limit. Increase the limit or modify the filter.');
273
        }
274
275
        return $items;
276
    }
277
278
    public function deleteItem($type, $table, $item)
279
    {
280
        if ($type == RedisDriver::TYPE_HASH) {
281
            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...
282
        }
283
        if ($type == RedisDriver::TYPE_KEY) {
284
            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...
285
        }
286
        if ($type == RedisDriver::TYPE_SET) {
287
            return $this->connection->srem($table, $item);
288
        }
289
        return parent::deleteItem($type, $table, $item);
290
    }
291
292
    public function deleteTable($type, $table)
293
    {
294
        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...
295
    }
296
297
    public function selectDatabase($database)
298
    {
299
        $this->connection->select($database);
300
    }
301
302
    public function execute($commands)
303
    {
304
        $listOfCommands = array_filter(array_map('trim', explode("\n", $commands)), function ($command) {
305
            return $command;
306
        });
307
308
        $results = [];
309
        foreach ($listOfCommands as $command) {
310
            $commandParts = explode(' ', $command);
311
            $function = array_shift($commandParts);
312
            $function = strtolower($function);
313
            $results[$command]['headers'] = $this->headers($function);
314
            $rows = call_user_func_array([$this->connection, $function], $commandParts);
315
            $items = $this->getItems($function, $rows);
316
            $results[$command]['items'] = $items;
317
            $results[$command]['count'] = count($items);
318
        }
319
        return $results;
320
    }
321
322
    private function headers($function)
323
    {
324
        if ($function === 'get' || $function === 'hget') {
325
            return ['value'];
326
        }
327
        if ($function === 'keys') {
328
            return ['key'];
329
        }
330
        if ($function === 'hgetall') {
331
            return ['key', 'value'];
332
        }
333
        if ($function === 'hlen') {
334
            return ['items_count'];
335
        }
336
        return [];
337
    }
338
339
    private function getItems($function, $rows)
340
    {
341
        $items = [];
342
        if ($function === 'keys') {
343
            foreach ($rows as $key) {
344
                $items[] = [$key];
345
            }
346
        } elseif ($function === 'hgetall') {
347
            foreach ($rows as $key => $value) {
348
                $items[] = [$key, $value];
349
            }
350
        } else {
351
            return [[$rows]];
352
        }
353
        return $items;
354
    }
355
}
356