Completed
Push — master ( 85ddb2...d5b935 )
by Michal
11:09
created

RedisSetDataManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 50.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 35
loc 69
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B itemsCount() 9 24 6
B items() 26 34 8

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace UniMan\Drivers\Redis\DataManager;
4
5
use RedisProxy\RedisProxy;
6
use UniMan\Core\Utils\Filter;
7
8
class RedisSetDataManager
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->scard($table);
21
        }
22
        $iterator = '';
23
        $totalItems = 0;
24
        do {
25
            $res = $this->connection->sscan($table, $iterator, null, 1000);
26
            if (!$res) {
27
                return $totalItems;
28
            }
29 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...
30
                $item = [
31
                    'member' => $member,
32
                    'length' => strlen($member),
33
                ];
34
                if (Filter::apply($item, $filter)) {
35
                    $totalItems++;
36
                }
37
            }
38
        } while ($iterator !== 0);
39
        return $totalItems;
40
    }
41
42
    public function items(string $table, int $page, int $onPage, array $filter = []): array
43
    {
44
        $skipped = 0;
45
        $offset = ($page - 1) * $onPage;
46
        $items = [];
47
        $iterator = '';
48 View Code Duplication
        do {
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...
49
            $pattern = null;
50
            $res = $this->connection->sscan($table, $iterator, $pattern, $onPage * 10);
51
            if (!$res) {
52
                return $items;
53
            }
54
            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...
55
                $item = [
56
                    'member' => $member,
57
                    'length' => strlen($member),
58
                ];
59
                if (!Filter::apply($item, $filter)) {
60
                    continue;
61
                }
62
63
                if ($skipped < $offset) {
64
                    $skipped++;
65
                    continue;
66
                }
67
68
                $items[$member] = $item;
69
                if (count($items) === $onPage) {
70
                    break;
71
                }
72
            }
73
        } while ($iterator !== 0 && count($items) < $onPage);
74
        return $items;
75
    }
76
}
77