Passed
Push — master ( ea2da2...e241fd )
by Alec
01:49
created

Circular   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 113
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 3 1
A convert() 0 18 4
A getElement() 0 3 1
A value() 0 8 2
A __construct() 0 3 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
use function AlecRabbit\typeOf;
8
9
/**
10
 * Class Circular
11
 */
12
class Circular implements \Iterator
13
{
14
    /** @var Rewindable */
15
    protected $data;
16
17
    /**
18
     * Circular constructor.
19
     * @param array|callable|Rewindable $data
20
     */
21 7
    public function __construct($data)
22
    {
23 7
        $this->data = $this->convert($data);
24 5
    }
25
26
    /**
27
     * @param mixed $data
28
     * @return Rewindable
29
     */
30 7
    private function convert(&$data): Rewindable
31
    {
32 7
        if (\is_array($data)) {
33
            return
34 4
                new Rewindable(
35
                    function () use (&$data): \Generator {
36 4
                        yield from $data;
37 4
                    }
38
                );
39
        }
40 3
        if (\is_callable($data)) {
41
            return
42 2
                new Rewindable($data);
43
        }
44 1
        if ($data instanceof Rewindable) {
45
            return $data;
46
        }
47 1
        throw new \InvalidArgumentException('Unexpected argument type: ' . typeOf($data) . ' for ' . Caller::get());
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53 1
    public function __invoke()
54
    {
55 1
        return $this->value();
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61 2
    public function value()
62
    {
63 2
        if (null === $value = $this->current()) {
64 2
            $this->rewind();
65 2
            $value = $this->current();
66
        }
67 2
        $this->next();
68 2
        return $value;
69
//        if (!$this->valid()) {
70
//            $this->rewind();
71
//        } else {
72
//            $this->next();
73
//        }
74
//        return $this->current();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 5
    public function current()
81
    {
82 5
        return $this->data->current();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 5
    public function rewind(): void
89
    {
90 5
        $this->data->rewind();
91 5
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 5
    public function next(): void
97
    {
98 5
        $this->data->next();
99 5
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 3
    public function valid(): bool
105
    {
106
        return
107 3
            $this->data->valid();
108
    }
109
110
    /**
111
     * @deprecated
112
     * @return mixed
113
     */
114
    public function getElement()
115
    {
116
        return $this->value();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 3
    public function key()
123
    {
124 3
        return $this->data->key();
125
    }
126
}
127