Passed
Push — master ( 0cfc7e...b88fc1 )
by Tom
03:33 queued 42s
created

Iterator::getByIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Cli\Args;
6
7
8
use Iterator as PhpIterator;
9
10
class Iterator extends Args implements PhpIterator
11
{
12
    /**
13
     * @var Args
14
     */
15
    private $args;
16
17
    /**
18
     * @var int
19
     */
20
    private $index;
21
22
    /**
23
     * @var array
24
     */
25
    private $indexes;
26
27
    /**
28
     * Iterator constructor.
29
     * @param Args $args
30
     */
31 6
    public function __construct(Args $args)
32
    {
33 6
        $this->args = $args;
34 6
        $this->rewind();
35 6
    }
36
37
    /**
38
     * get next argument (look ahead)
39
     *
40
     * @return null|string next argument, null if there is no next argument
41
     */
42 2
    public function getNext()
43
    {
44 2
        return $this->getByIndex($this->index + 1);
45
    }
46
47
    /**
48
     * @param int $index
49
     * @return string|null argument string or null if not found
50
     */
51 5
    private function getByIndex($index)
52
    {
53
54 5
        $argsIndex = $this->getArgsIndex($index);
55 5
        if (null === $argsIndex) {
56 4
            return null;
57
        }
58
59 4
        if (!isset($this->args->arguments[$argsIndex])) {
60 1
            return null;
61
        }
62
63 4
        return $this->args->arguments[$argsIndex];
64
    }
65
66
    /**
67
     * @param int $index to retrieve index for
68
     *
69
     * @return int|null integer index of the argument, null if there is none
70
     */
71 5
    private function getArgsIndex($index)
72
    {
73 5
        if (!isset($this->indexes[$index])) {
74 4
            return null;
75
        }
76
77 4
        return $this->indexes[$index];
78
    }
79
80
    /* Iterator */
81
82
    /**
83
     * Return the current element
84
     *
85
     * @return string
86
     */
87 3
    public function current()
88
    {
89 3
        if (!$this->valid()) {
90 1
            return null;
91
        }
92
93 3
        return $this->getByIndex($this->index);
94
    }
95
96
    /**
97
     * Move forward to next option
98
     */
99 4
    public function next()
100
    {
101 4
        if (!$this->valid()) {
102 1
            return;
103
        }
104
105 3
        $this->index++;
106 3
    }
107
108
    /**
109
     * Return the key of the current option
110
     *
111
     * @return int index, zero based
112
     */
113 2
    public function key()
114
    {
115 2
        return $this->getArgsIndex($this->index);
116
    }
117
118
    /**
119
     * Checks if current position is valid
120
     *
121
     * @return bool
122
     */
123 5
    public function valid()
124
    {
125 5
        $current = $this->getByIndex($this->index);
126
127 5
        if (null === $current) {
128 4
            return false;
129
        }
130
131 4
        return true;
132
    }
133
134
    /**
135
     * @return void Any returned value is ignored.
136
     */
137 6
    public function rewind()
138
    {
139 6
        $this->index = 0;
140 6
        $this->indexes = array_keys($this->args->arguments);
141 6
    }
142
}
143