RedisHashDataManager   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 0
loc 105
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B itemsCount() 0 41 10
C items() 0 53 12
1
<?php
2
3
namespace UniMan\Drivers\Redis\DataManager;
4
5
use RedisProxy\RedisProxy;
6
use UniMan\Core\Utils\Filter;
7
8
class RedisHashDataManager
9
{
10
    private $connection;
11
12
    public function __construct(RedisProxy $connection)
13
    {
14
        $this->connection = $connection;
15
    }
16
17
    public function itemsCount(string $table, array $filter): int
18
    {
19
        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...
20
            return $this->connection->hlen($table);
21
        }
22
23
        $totalItems = 0;
24
        foreach ($filter as $filterParts) {
25
            if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) {
26
                $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]);
27
                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...
28
                    $item = [
29
                        'key' => $filterParts['key'][Filter::OPERATOR_EQUAL],
30
                        'length' => strlen($res),
31
                        'value' => $res,
32
                    ];
33
                    if (Filter::apply($item, $filter)) {
34
                        $totalItems++;
35
                    }
36
                }
37
                return $totalItems;
38
            }
39
        }
40
        $iterator = '';
41
        do {
42
            $pattern = null;
43
            $res = $this->connection->hscan($table, $iterator, $pattern, 1000);
44
            $res = $res ?: [];
45
            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...
46
                $item = [
47
                    'key' => $key,
48
                    'length' => strlen($value),
49
                    'value' => $value,
50
                ];
51
                if (Filter::apply($item, $filter)) {
52
                    $totalItems++;
53
                }
54
            }
55
        } while ($iterator !== 0);
56
        return $totalItems;
57
    }
58
59
    public function items(string $table, int $page, int $onPage, array $filter = []): array
60
    {
61
        $items = [];
62
        foreach ($filter as $filterParts) {
63
            if (!isset($filterParts['key'][Filter::OPERATOR_EQUAL])) {
64
                continue;
65
            }
66
            $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]);
67
            if (!$res) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type string|null is loosely compared to false; 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...
68
                return $items;
69
            }
70
            $item = [
71
                'key' => $filterParts['key'][Filter::OPERATOR_EQUAL],
72
                'length' => strlen($res),
73
                'value' => $res,
74
            ];
75
            if (Filter::apply($item, $filter)) {
76
                $items[$item['key']] = $item;
77
            }
78
            return $items;
79
        }
80
81
        $skipped = 0;
82
        $offset = ($page - 1) * $onPage;
83
        $iterator = '';
84
        do {
85
            $pattern = null;
86
            $res = $this->connection->hscan($table, $iterator, $pattern, $onPage * 10);
87
            if (!$res) {
88
                return $items;
89
            }
90
            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...
91
                $item = [
92
                    'key' => $key,
93
                    'length' => strlen($value),
94
                    'value' => $value,
95
                ];
96
                if (!Filter::apply($item, $filter)) {
97
                    continue;
98
                }
99
                if ($skipped < $offset) {
100
                    $skipped++;
101
                    continue;
102
                }
103
104
                $items[$key] = $item;
105
                if (count($items) === $onPage) {
106
                    break;
107
                }
108
            }
109
        } while ($iterator !== 0 && count($items) < $onPage);
110
        return $items;
111
    }
112
}
113