Filter::missing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace League\CLImate\Argument;
4
5
class Filter
6
{
7
    protected $arguments = [];
8
9
    /**
10
     * Set the available arguments
11
     *
12
     * @param array $arguments
13
     */
14 20
    public function setArguments(array $arguments)
15
    {
16 20
        $this->arguments = $arguments;
17 20
    }
18
19
    /**
20
     * Retrieve optional arguments
21
     *
22
     * @return Argument[]
23
     */
24 4
    public function optional()
25
    {
26 4
        return $this->filterArguments(['isOptional']);
27
    }
28
29
    /**
30
     * Retrieve required arguments
31
     *
32
     * @return Argument[]
33
     */
34 4
    public function required()
35
    {
36 4
        return $this->filterArguments(['isRequired']);
37
    }
38
39
    /**
40
     * Retrieve arguments with prefix
41
     *
42
     * @return Argument[]
43
     */
44 20
    public function withPrefix()
45
    {
46 20
        return $this->filterArguments(['hasPrefix']);
47
    }
48
49
    /**
50
     * Retrieve arguments without prefix
51
     *
52
     * @return Argument[]
53
     */
54 20
    public function withoutPrefix()
55
    {
56 20
        return $this->filterArguments(['noPrefix']);
57
    }
58
59
    /**
60
     * Find all required arguments that don't have values after parsing.
61
     *
62
     * These arguments weren't defined on the command line.
63
     *
64
     * @return Argument[]
65
     */
66 16
    public function missing()
67
    {
68 16
        return $this->filterArguments(['isRequired', 'noValue']);
69
    }
70
71
    /**
72
     * Filter defined arguments as to whether they are required or not
73
     *
74
     * @param string[] $filters
75
     *
76
     * @return Argument[]
77
     */
78 20
    protected function filterArguments($filters = [])
79
    {
80 20
        $arguments = $this->arguments;
81
82 20
        foreach ($filters as $filter) {
83 20
            $arguments = array_filter($arguments, [$this, $filter]);
84 20
        }
85
86 20
        if (in_array('hasPrefix', $filters)) {
87 20
            usort($arguments, [$this, 'compareByPrefix']);
88 20
        }
89
90 20
        return array_values($arguments);
91
    }
92
93
    /**
94
     * Determine whether an argument as a prefix
95
     *
96
     * @param Argument $argument
97
     *
98
     * @return bool
99
     */
100 20
    protected function noPrefix($argument)
101
    {
102 20
        return !$argument->hasPrefix();
103
    }
104
105
    /**
106
     * Determine whether an argument as a prefix
107
     *
108
     * @param Argument $argument
109
     *
110
     * @return bool
111
     */
112 20
    protected function hasPrefix($argument)
113
    {
114 20
        return $argument->hasPrefix();
115
    }
116
117
    /**
118
     * Determine whether an argument is required
119
     *
120
     * @param Argument $argument
121
     *
122
     * @return bool
123
     */
124 20
    protected function isRequired($argument)
125
    {
126 20
        return $argument->isRequired();
127
    }
128
129
    /**
130
     * Determine whether an argument is optional
131
     *
132
     * @param Argument $argument
133
     *
134
     * @return bool
135
     */
136 4
    protected function isOptional($argument)
137
    {
138 4
        return !$argument->isRequired();
139
    }
140
141
    /**
142
     * Determine whether an argument is optional
143
     *
144
     * @param Argument $argument
145
     *
146
     * @return bool
147
     */
148 4
    protected function noValue($argument)
149
    {
150 4
        return $argument->values() == [];
151
    }
152
153
    /**
154
     * Compare two arguments by their short and long prefixes.
155
     *
156
     * @see usort()
157
     *
158
     * @param Argument $a
159
     * @param Argument $b
160
     *
161
     * @return int
162
     */
163 16
    public function compareByPrefix(Argument $a, Argument $b)
164
    {
165 16
        if ($this->prefixCompareString($a) < $this->prefixCompareString($b)) {
166 12
            return -1;
167
        }
168
169 16
        return 1;
170
    }
171
172
    /**
173
     * Prep the prefix string for comparison
174
     *
175
     * @param Argument $argument
176
     *
177
     * @return string
178
     */
179 16
    protected function prefixCompareString(Argument $argument)
180
    {
181 16
        return mb_strtolower($argument->longPrefix() ?: $argument->prefix() ?: '');
182
    }
183
}
184