Passed
Push — v2 ( 9bff46...d4d6f5 )
by Brice
03:06
created

Bag   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 8%

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 111
rs 10
c 0
b 0
f 0
ccs 2
cts 25
cp 0.08

11 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A next() 0 3 1
A unserialize() 0 3 1
A __toArray() 0 3 1
A valid() 0 3 1
A serialize() 0 3 1
A rewind() 0 3 1
A count() 0 3 1
A current() 0 3 1
A seek() 0 7 2
A jsonSerialize() 0 3 1
1
<?php
2
3
namespace JobQueue\Domain\Utils;
4
5
abstract class Bag implements \Countable, \SeekableIterator, \Serializable, \JsonSerializable
6
{
7
    /**
8
     *
9
     * @var array
10
     */
11
    protected $data = [];
12
13
    /**
14
     *
15
     * @return int
16
     */
17
    public function count(): int
18
    {
19
        return count($this->data);
20
    }
21
22
23
    /**
24
     *
25
     * @param int $key
26
     * @return int
27
     */
28
    public function seek($key)
29
    {
30
        if (!isset($this->data[$key])) {
31
            throw new \OutOfBoundsException;
32
        }
33
34
        return $key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $key returns the type integer which is incompatible with the return type mandated by SeekableIterator::seek() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
35
    }
36
37
    /**
38
     *
39
     * @return mixed
40
     */
41
    public function current()
42
    {
43
        return current($this->data);
44
    }
45
46
    /**
47
     *
48
     * @return mixed
49
     */
50
    public function next()
51
    {
52
        return next($this->data);
53
    }
54
55
    /**
56
     *
57
     * @return mixed
58
     */
59
    public function key()
60
    {
61
        return key($this->data);
62
    }
63
64
    /**
65
     *
66
     * @return bool
67
     */
68
    public function valid(): bool
69
    {
70
        return null !== key($this->data);
71
    }
72
73
    /**
74
     *
75
     * @return mixed
76
     */
77
    public function rewind()
78
    {
79
        return reset($this->data);
80
    }
81
82
    /**
83
     *
84
     * @return string
85
     */
86
    public function serialize(): string
87
    {
88
        return serialize($this->data);
89
    }
90
91
    /**
92
     *
93
     * @param string $serialized
94
     */
95
    public function unserialize($serialized)
96
    {
97
        $this->data = unserialize($serialized);
98
    }
99
100
    /**
101
     *
102
     * @return array
103
     */
104
    public function jsonSerialize(): array
105
    {
106
        return $this->data;
107
    }
108
109
    /**
110
     *
111
     * @return array
112
     */
113 17
    public function __toArray(): array
114
    {
115 17
        return $this->data;
116
    }
117
}
118