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

Circular::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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