Manager   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 4
dl 0
loc 243
ccs 69
cts 69
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 17 4
A addMany() 0 6 2
A exists() 0 4 1
A get() 0 4 2
A getArray() 0 4 2
A all() 0 4 1
A defined() 0 18 4
A isArgument() 0 15 4
A toArray() 0 10 2
A description() 0 4 1
A usage() 0 9 1
A parse() 0 6 1
A trailing() 0 4 1
1
<?php
2
3
namespace League\CLImate\Argument;
4
5
use League\CLImate\CLImate;
6
7
class Manager
8
{
9
    /**
10
     * An array of arguments passed to the program.
11
     *
12
     * @var Argument[] $arguments
13
     */
14
    protected $arguments = [];
15
16
    /**
17
     * A program's description.
18
     *
19
     * @var string $description
20
     */
21
    protected $description;
22
23
    /**
24
     * Filter class to find various types of arguments
25
     *
26
     * @var \League\CLImate\Argument\Filter $filter
27
     */
28
    protected $filter;
29
30
    /**
31
     * Summary builder class
32
     *
33
     * @var \League\CLImate\Argument\Summary $summary
34
     */
35
    protected $summary;
36
37
    /**
38
     * Argument parser class
39
     *
40
     * @var \League\CLImate\Argument\Parser $parser
41
     */
42
    protected $parser;
43
44 948
    public function __construct()
45
    {
46 948
        $this->filter  = new Filter();
47 948
        $this->summary = new Summary();
48 948
        $this->parser  = new Parser();
49 948
    }
50
51
    /**
52
     * Add an argument.
53
     *
54
     * @throws \Exception if $argument isn't an array or Argument object.
55
     * @param Argument|string|array $argument
56
     * @param $options
57
     */
58 28
    public function add($argument, array $options = [])
59
    {
60 28
        if (is_array($argument)) {
61 24
            $this->addMany($argument);
62 24
            return;
63
        }
64
65 28
        if (is_string($argument)) {
66 24
            $argument = Argument::createFromArray($argument, $options);
67 24
        }
68
69 28
        if (!($argument instanceof Argument)) {
70 4
            throw new \Exception('Please provide an argument name or object.');
71
        }
72
73 24
        $this->arguments[$argument->name()] = $argument;
74 24
    }
75
76
    /**
77
     * Add multiple arguments to a CLImate script.
78
     *
79
     * @param array $arguments
80
     */
81 24
    protected function addMany(array $arguments = [])
82
    {
83 24
        foreach ($arguments as $name => $options) {
84 24
            $this->add($name, $options);
85 24
        }
86 24
    }
87
88
    /**
89
     * Determine if an argument exists.
90
     *
91
     * @param string $name
92
     * @return bool
93
     */
94 8
    public function exists($name)
95
    {
96 8
        return isset($this->arguments[$name]);
97
    }
98
99
    /**
100
     * Retrieve an argument's value.
101
     *
102
     * @param string $name
103
     * @return string|int|float|bool|null
104
     */
105 8
    public function get($name)
106
    {
107 8
        return isset($this->arguments[$name]) ? $this->arguments[$name]->value() : null;
108
    }
109
110
    /**
111
     * Retrieve an argument's all values as an array.
112
     *
113
     * @param string $name
114
     * @return string[]|int[]|float[]|bool[]
115 20
     */
116
    public function getArray($name)
117 20
    {
118
        return isset($this->arguments[$name]) ? $this->arguments[$name]->values() : [];
119
    }
120
121
    /**
122
     * Retrieve all arguments.
123
     *
124
     * @return Argument[]
125
     */
126
    public function all()
127
    {
128
        return $this->arguments;
129
    }
130
131 8
    /**
132
     * Determine if an argument has been defined on the command line.
133
     *
134 8
     * This can be useful for making sure an argument is present on the command
135 8
     * line before parse()'ing them into argument objects.
136
     *
137
     * @param string $name
138 8
     * @param array $argv
139 8
     *
140
     * @return bool
141 8
     */
142 8
    public function defined($name, array $argv = null)
143 8
    {
144
        // The argument isn't defined if it's not defined by the calling code.
145 8
        if (!$this->exists($name)) {
146
            return false;
147 8
        }
148
149
        $argument = $this->arguments[$name];
150
        $command_arguments = $this->parser->arguments($argv);
151
152
        foreach ($command_arguments as $command_argument) {
153
            if ($this->isArgument($argument, $command_argument)) {
154
                return true;
155
            }
156
        }
157
158 8
        return false;
159
    }
160
161 8
    /**
162 8
     * Check if the defined argument matches the command argument.
163 8
     *
164
     * @param Argument $argument
165 8
     * @param string $command_argument
166 8
     *
167 8
     * @return bool
168
     */
169 8
    protected function isArgument($argument, $command_argument)
170
    {
171 8
        $possibilities = [
172
            $argument->prefix()     => "-{$argument->prefix()}",
173
            $argument->longPrefix() => "--{$argument->longPrefix()}",
174
        ];
175
176
        foreach ($possibilities as $key => $search) {
177
            if ($key && strpos($command_argument, $search) === 0) {
178
                return true;
179 8
            }
180
        }
181 8
182
        return false;
183 8
    }
184 8
185 8
    /**
186
     * Retrieve all arguments as key/value pairs.
187 8
     *
188
     * @return array
189
     */
190
    public function toArray()
191
    {
192
        $return = [];
193
194
        foreach ($this->all() as $name => $argument) {
195 4
            $return[$name] = $argument->value();
196
        }
197 4
198 4
        return $return;
199
    }
200
201
    /**
202
     * Set a program's description.
203
     *
204
     * @param string $description
205
     */
206 4
    public function description($description)
207
    {
208 4
        $this->description = trim($description);
209 4
    }
210 4
211 4
    /**
212 4
     * Output a script's usage statement.
213 4
     *
214 4
     * @param CLImate $climate
215
     * @param array $argv
216
     */
217
    public function usage(CLImate $climate, array $argv = null)
218
    {
219
        $this->summary
220
                ->setClimate($climate)
221
                ->setDescription($this->description)
222 16
                ->setCommand($this->parser->command($argv))
223
                ->setFilter($this->filter, $this->all())
224 16
                ->output();
225
    }
226 16
227 12
    /**
228
     * Parse command line arguments into CLImate arguments.
229
     *
230
     * @throws \Exception if required arguments aren't defined.
231
     * @param array $argv
232
     */
233
    public function parse(array $argv = null)
234 4
    {
235
        $this->parser->setFilter($this->filter, $this->all());
236 4
237
        $this->parser->parse($argv);
238
    }
239
240
    /**
241
     * Get the trailing arguments
242
     *
243
     * @return string|null
244
     */
245
    public function trailing()
246
    {
247
        return $this->parser->trailing();
248
    }
249
}
250