Completed
Pull Request — master (#6)
by Michal
02:10
created

SortedSetBehavior   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 25.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 16
loc 62
rs 10
c 1
b 0
f 0
ccs 19
cts 19
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
init() 0 1 ?
actualDriver() 0 1 ?
A zadd() 0 13 3
A zrange() 8 8 2
A zrevrange() 8 8 2

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 RedisProxy;
4
5
/**
6
 * @method int zcard(string $key) Get the number of members in a sorted set
7
 */
8
trait SortedSetBehavior
9
{
10
    private $driver;
11
12
    abstract protected function init();
13 20
14
    abstract public function actualDriver();
15 20
16 20
    /**
17 12
     * Add one or more members to a sorted set, or update its score if it already exists
18 12
     * @param string $key
19 12
     * @param array $dictionary (score1, member1[, score2, member2]) or associative array: [member1 => score1, member2 => score2]
20 12
     * @return int
21
     */
22 12
    public function zadd($key, ...$dictionary)
23
    {
24 20
        $this->init();
25
        if (is_array($dictionary[0])) {
26
            $return = 0;
27
            foreach ($dictionary[0] as $member => $score) {
28
                $res = $this->zadd($key, $score, $member);
29
                $return += $res;
30
            }
31
            return $return;
32
        }
33
        return $this->driver->zadd($key, ...$dictionary);
34
    }
35 4
36
    /**
37 4
     * Return a range of members in a sorted set, by index
38 4
     * @param string $key
39 2
     * @param int $start
40
     * @param int $stop
41 2
     * @param boolean $withscores
42
     * @return array
43
     */
44 View Code Duplication
    public function zrange($key, $start, $stop, $withscores = false)
45
    {
46
        $this->init();
47
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
48
            return $this->driver->zrange($key, $start, $stop, ['WITHSCORES' => $withscores]);
49
        }
50
        return $this->driver->zrange($key, $start, $stop, $withscores);
51
    }
52 4
53
    /**
54 4
     * Return a range of members in a sorted set, by index, with scores ordered from high to low
55 4
     * @param string $key
56 2
     * @param int $start
57
     * @param int $stop
58 2
     * @param boolean $withscores
59
     * @return array
60
     */
61 View Code Duplication
    public function zrevrange($key, $start, $stop, $withscores = false)
62
    {
63
        $this->init();
64
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
65
            return $this->driver->zrevrange($key, $start, $stop, ['WITHSCORES' => $withscores]);
66
        }
67
        return $this->driver->zrevrange($key, $start, $stop, $withscores);
68
    }
69
}
70