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

ListBehavior::rpush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 4
nop 2
1
<?php
2
3
namespace RedisProxy;
4
5
/**
6
 * @method int llen(string $key) Get the length of a list
7
 * @method array lrange(string $key, int $start, int $stop) Get a range of elements from a list
8
 */
9
trait ListBehavior
10
{
11
    private $driver;
12
13
    abstract protected function init();
14
15
    abstract public function actualDriver();
16
17
    /**
18
     * Prepend one or multiple values to a list
19
     * @param string $key
20
     * @param array $elements
21
     * @return int the length of the list after the push operations
22
     */
23
    public function lpush($key, ...$elements)
24
    {
25
        $elements = $this->prepareArguments('lpush', ...$elements);
0 ignored issues
show
Bug introduced by
It seems like prepareArguments() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
        $this->init();
27
        return $this->driver->lpush($key, ...$elements);
28
    }
29
30
    /**
31
     * Append one or multiple values to a list
32
     * @param string $key
33
     * @param array $elements
34
     * @return int the length of the list after the push operations
35
     */
36
    public function rpush($key, ...$elements)
37
    {
38
        $elements = $this->prepareArguments('rpush', ...$elements);
0 ignored issues
show
Bug introduced by
It seems like prepareArguments() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
        $this->init();
40
        return $this->driver->rpush($key, ...$elements);
41
    }
42
43
    /**
44
     * Remove and get the first element in a list
45
     * @param string $key
46
     * @return string|null
47
     */
48
    public function lpop($key)
49
    {
50
        $this->init();
51
        $result = $this->driver->lpop($key);
52
        return $this->convertFalseToNull($result);
0 ignored issues
show
Bug introduced by
It seems like convertFalseToNull() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
    }
54
55
    /**
56
     * Remove and get the last element in a list
57
     * @param string $key
58
     * @return string|null
59
     */
60
    public function rpop($key)
61
    {
62
        $this->init();
63
        $result = $this->driver->rpop($key);
64
        return $this->convertFalseToNull($result);
0 ignored issues
show
Bug introduced by
It seems like convertFalseToNull() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65
    }
66
67
    /**
68
     * Get an element from a list by its index
69
     * @param string $key
70
     * @param int $index zero-based, so 0 means the first element, 1 the second element and so on. -1 means the last element, -2 means the penultimate and so forth
71
     * @return string|null
72
     */
73
    public function lindex($key, $index)
74
    {
75
        $this->init();
76
        $result = $this->driver->lindex($key, $index);
77
        return $this->convertFalseToNull($result);
0 ignored issues
show
Bug introduced by
It seems like convertFalseToNull() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
78
    }
79
}
80