Passed
Push — test ( 63bffd...9ffc6c )
by Tom
03:04
created

OptionIterator::currentMatchesOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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
use IteratorIterator;
8
use Ktomk\Pipelines\Cli\ArgsException;
9
10
class OptionIterator extends IteratorIterator
11
{
12
    /**
13
     * @var Iterator
14
     */
15
    private $iterator;
16
17
    /**
18
     * Iterator constructor.
19
     *
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
    /**
30
     * @return bool
31
     */
32 5
    public function hasArgument()
33
    {
34 5
        $next = $this->iterator->getNext();
35
36 5
        if (null === $next) {
37 1
            return false;
38
        }
39
40 4
        if ('--' === $next) {
41 1
            return false;
42
        }
43
44 3
        return true;
45
    }
46
47
    /**
48
     * @throws ArgsException
49
     *
50
     * @return string
51
     */
52 5
    public function getArgument()
53
    {
54 5
        if (!$this->hasArgument()) {
55 2
            throw new ArgsException(
56 2
                sprintf('option %s requires an argument', (string)$this->current())
57
            );
58
        }
59
60 3
        return (string)$this->iterator->getNext();
61
    }
62
63
    /* Iterator */
64
65
    /**
66
     * @return null|string
67
     */
68 8
    public function current()
69
    {
70 8
        return $this->iterator->current();
71
    }
72
73
    /**
74
     * Move forward to next option
75
     */
76 9
    public function next()
77
    {
78 9
        parent::next();
79 9
        $this->forwardToOption();
80 9
    }
81
82
    /**
83
     * Checks if current position is valid
84
     *
85
     * @return bool
86
     */
87 6
    public function valid()
88
    {
89 6
        if (!$this->iterator->valid()) {
90 4
            return false;
91
        }
92
93 5
        $current = parent::current();
94
95 5
        return  !('--' === $current);
96
    }
97
98
    /**
99
     * @return void Any returned value is ignored.
100
     */
101 18
    public function rewind()
102
    {
103 18
        parent::rewind();
104 18
        $this->forwardToOption();
105 18
    }
106
107
    /* seeks */
108
109
    /**
110
     * seek option
111
     *
112
     * seek single option (technically only long options are supported)
113
     * which allows --long-option[=argument] optional arguments.
114
     *
115
     * @param string|string[] $option
116
     */
117 1
    public function seekOption($option)
118
    {
119
        while(
120 1
            ($this->forwardToOption() || $this->valid())
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->forwardToOption() targeting Ktomk\Pipelines\Cli\Args...ator::forwardToOption() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121 1
            && !$this->currentMatchesOption($option)
122
        ) {
123 1
            parent::next();
124
        }
125 1
    }
126
127
    /**
128
     * @param string|string[] $option
129
     *
130
     * @return bool
131
     */
132 1
    public function currentMatchesOption($option)
133
    {
134 1
        return $this->valid() ? OptionMatcher::create($option)->match($this->current()) : false;
135
    }
136
137
    /**
138
     * Forward the iterator to the current option
139
     *
140
     * @return void
141
     */
142 18
    private function forwardToOption()
143
    {
144
        while (
145 18
            parent::valid()
146 18
            && (null !== $current = parent::current())
147
            && (
148 18
                (strlen($current) < 2)
149 18
                || (0 !== strpos($current, '-'))
150
            )
151
        ) {
152 8
            parent::next();
153
        }
154 18
    }
155
}
156