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

OptionIterator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 3 1
A hasArgument() 0 13 3
A valid() 0 13 3
A next() 0 4 1
A __construct() 0 5 1
A getArgument() 0 9 2
A rewind() 0 4 1
B forwardToOption() 0 11 5
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Cli\Args;
6
7
8
use IteratorIterator;
9
use Ktomk\Pipelines\Cli\ArgsException;
10
11
class OptionIterator extends IteratorIterator
12
{
13
    /**
14
     * @var Iterator
15
     */
16
    private $iterator;
17
18
    /**
19
     * Iterator constructor.
20
     * @param Args $args
21
     */
22 18
    public function __construct(Args $args)
23
    {
24 18
        $this->iterator = new Iterator($args);
25 18
        parent::__construct($this->iterator);
26 18
        $this->rewind();
27 18
    }
28
29 6
    public function hasArgument()
30
    {
31 6
        $next = $this->iterator->getNext();
32
33 6
        if (null === $next) {
34 1
            return false;
35
        }
36
37 5
        if ($next === '--') {
38 1
            return false;
39
        }
40
41 4
        return true;
42
    }
43
44
    /**
45
     * @throws ArgsException
46
     */
47 6
    public function getArgument()
48
    {
49 6
        if (!$this->hasArgument()) {
50 2
            ArgsException::__(
51 2
                sprintf("error: option '%s' requires an argument", ltrim($this->current(), '-'))
52
            );
53
        }
54
55 4
        return $this->iterator->getNext();
56
    }
57
58
    /**
59
     * Forward the iterator to the current option
60
     */
61 18
    private function forwardToOption()
62
    {
63
        while (
64 18
            parent::valid()
65 18
            && (null !== $current = parent::current())
66
            && (
67 18
                (strlen($current) < 2)
68 18
                || (substr($current, 0, 1) !== '-')
69
            )
70
        ) {
71 8
            parent::next();
72
        }
73 18
    }
74
75
    /* Iterator */
76
77 8
    public function current()
78
    {
79 8
        return $this->iterator->current();
80
    }
81
82
    /**
83
     * Move forward to next option
84
     */
85 10
    public function next()
86
    {
87 10
        parent::next();
88 10
        $this->forwardToOption();
89 10
    }
90
91
    /**
92
     * Checks if current position is valid
93
     *
94
     * @return bool
95
     */
96 6
    public function valid()
97
    {
98 6
        if (!$this->iterator->valid()) {
99 5
            return false;
100
        }
101
102 5
        $current = parent::current();
103
104 5
        if ('--' === $current) {
105 1
            return false;
106
        }
107
108 5
        return true;
109
    }
110
111
    /**
112
     * @return void Any returned value is ignored.
113
     */
114 18
    public function rewind()
115
    {
116 18
        parent::rewind();
117 18
        $this->forwardToOption();
118 18
    }
119
}
120