Passed
Push — master ( 707534...dc95cb )
by Alec
01:50
created

Circular   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 75
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 3 1
A getElement() 0 8 2
A __construct() 0 4 1
A rewind() 0 3 1
A current() 0 3 1
A valid() 0 4 1
A next() 0 3 1
A key() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Accessories;
6
7
/**
8
 * Class Circular
9
 */
10
class Circular implements \Iterator
11
{
12
    /** @var array */
13
    protected $data;
14
15
    /**
16
     * Circular constructor.
17
     * @param mixed $data
18
     */
19 4
    public function __construct($data)
20
    {
21 4
        $this->data = $data;
22 4
        reset($this->data);
23 4
    }
24
25
    /**
26
     * @return mixed
27
     */
28 1
    public function __invoke()
29
    {
30 1
        return $this->getElement();
31
    }
32
33
    /**
34
     * @return mixed
35
     */
36 2
    public function getElement()
37
    {
38 2
        if (false === $result = current($this->data)) {
39 2
            $result = reset($this->data);
40
        }
41 2
        next($this->data);
42
43 2
        return $result;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function current()
50
    {
51 2
        return current($this->data);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function next(): void
58
    {
59 2
        next($this->data);
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function key()
66
    {
67 2
        return key($this->data);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function valid(): bool
74
    {
75
        return
76 2
            false !== current($this->data);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 2
    public function rewind(): void
83
    {
84 2
        reset($this->data);
85 2
    }
86
}
87