Argument   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 31

Size/Duplication

Total Lines 380
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 0
dl 0
loc 380
ccs 84
cts 84
cp 1
rs 9.8
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromArray() 0 16 3
A getSettableArgumentParams() 0 14 1
A name() 0 4 1
A setName() 0 4 1
A prefix() 0 4 1
A setPrefix() 0 4 1
A longPrefix() 0 4 1
A setLongPrefix() 0 4 1
A hasPrefix() 0 4 2
A description() 0 4 1
A setDescription() 0 4 1
A isRequired() 0 4 1
A setRequired() 0 4 1
A noValue() 0 4 1
A setNoValue() 0 5 1
A castTo() 0 4 1
A setCastTo() 0 11 3
A defaultValue() 0 4 1
A setDefaultValue() 0 4 1
A value() 0 4 1
A setValue() 0 5 1
A castToString() 0 4 1
A castToInt() 0 4 1
A castToFloat() 0 4 1
A castToBool() 0 4 1
1
<?php
2
3
namespace League\CLImate\Argument;
4
5
class Argument
6
{
7
    /**
8
     * An argument's name.
9
     *
10
     * Use this name when internally referring to the argument.
11
     *
12
     * @var string
13
     */
14
    protected $name;
15
16
    /**
17
     * An argument's short representation.
18
     *
19
     * @var string
20
     */
21
    protected $prefix;
22
23
    /**
24
     * An argument's long representation.
25
     *
26
     * @var string
27
     */
28
    protected $longPrefix;
29
30
    /**
31
     * An argument's description.
32
     *
33
     * @var string
34
     */
35
    protected $description;
36
37
    /**
38
     * Whether or not an argument is required.
39
     *
40
     * @var bool
41
     */
42
    protected $required = false;
43
44
    /**
45
     * Whether or not an argument only needs to be defined to have a value.
46
     *
47
     * These arguments have the value true when they are defined on the command
48
     * line.
49
     *
50
     * @var bool
51
     */
52
    protected $noValue = false;
53
54
    /**
55
     * Which data type to cast an argument's value to.
56
     *
57
     * Valid data types are "string", "int", "float", and "bool".
58
     *
59
     * @var string
60
     */
61
    protected $castTo = 'string';
62
63
    /**
64
     * An argument's default value.
65
     *
66
     * @var string
67
     */
68
    protected $defaultValue;
69
70
    /**
71
     * An argument's value, after type casting.
72
     *
73
     * @var string|int|float|bool
74
     */
75
    protected $value;
76
77
    /**
78
     * Build a new command argument.
79
     *
80
     * @param string $name
81
     */
82 48
    public function __construct($name)
83
    {
84 48
        $this->setName($name);
85 48
    }
86
87
    /**
88
     * Build a new command argument from an array.
89
     *
90
     * @param string $name
91
     * @param array $params
92
     *
93
     * @return Argument
94
     */
95 48
    public static function createFromArray($name, array $params)
96
    {
97 48
        $argument = new Argument($name);
98 48
        $params   = self::getSettableArgumentParams($params);
99
100 48
        foreach ($params as $key => $value) {
101 48
            $method = 'set' . ucwords($key);
102 48
            $argument->{$method}($value);
103 44
        }
104
105 44
        if ($argument->defaultValue()) {
106 8
            $argument->setValue($argument->defaultValue());
107 8
        }
108
109 44
        return $argument;
110
    }
111
112
    /**
113
     * Get argument params based on settable properties
114
     *
115
     * @param array $params
116
     *
117
     * @return array
118
     */
119 48
    protected static function getSettableArgumentParams(array $params)
120
    {
121
        $allowed = [
122 48
                    'prefix',
123 48
                    'longPrefix',
124 48
                    'description',
125 48
                    'required',
126 48
                    'noValue',
127 48
                    'castTo',
128 48
                    'defaultValue',
129 48
                ];
130
131 48
        return array_intersect_key($params, array_flip($allowed));
132
    }
133
134
    /**
135
     * Retrieve an argument's name.
136
     *
137
     * Use this name when internally referring to the argument.
138
     *
139
     * @return string
140
     */
141 24
    public function name()
142
    {
143 24
        return $this->name;
144
    }
145
146
    /**
147
     * Set an argument's name.
148
     *
149
     * Use this name when internally referring to the argument.
150
     *
151
     * @param string $name
152
     */
153 48
    protected function setName($name)
154
    {
155 48
        $this->name = trim($name);
156 48
    }
157
158
    /**
159
     * Retrieve an argument's short form.
160
     *
161
     * @return string
162
     */
163 24
    public function prefix()
164
    {
165 24
        return $this->prefix;
166
    }
167
168
    /**
169
     * Set an argument's short form.
170
     *
171
     * @param string $prefix
172
     */
173 20
    protected function setPrefix($prefix)
174
    {
175 20
        $this->prefix = trim($prefix);
176 20
    }
177
178
    /**
179
     * Retrieve an argument's long form.
180
     *
181
     * @return string
182
     */
183 24
    public function longPrefix()
184
    {
185 24
        return $this->longPrefix;
186
    }
187
188
    /**
189
     * Set an argument's short form.
190
     *
191
     * @param string $longPrefix
192
     */
193 20
    protected function setLongPrefix($longPrefix)
194
    {
195 20
        $this->longPrefix = trim($longPrefix);
196 20
    }
197
198
    /**
199
     * Determine if an argument has a prefix.
200
     *
201
     * @return bool
202
     */
203 20
    public function hasPrefix()
204
    {
205 20
        return $this->prefix() || $this->longPrefix();
206
    }
207
208
    /**
209
     * Retrieve an argument's description.
210
     *
211
     * @return string
212
     */
213 4
    public function description()
214
    {
215 4
        return $this->description;
216
    }
217
218
    /**
219
     * Set an argument's description.
220
     *
221
     * @param string $description
222
     */
223 4
    protected function setDescription($description)
224
    {
225 4
        $this->description = trim($description);
226 4
    }
227
228
    /**
229
     * Determine whether or not an argument is required.
230
     *
231
     * @return bool
232
     */
233 20
    public function isRequired()
234
    {
235 20
        return $this->required;
236
    }
237
238
    /**
239
     * Set whether an argument is required or not.
240
     *
241
     * @param bool $required
242
     */
243 8
    protected function setRequired($required)
244
    {
245 8
        $this->required = (bool) $required;
246 8
    }
247
248
    /**
249
     * Determine whether or not an argument only needs to be defined to have a
250
     * value.
251
     *
252
     * @return bool
253
     */
254 40
    public function noValue()
255
    {
256 40
        return $this->noValue;
257
    }
258
259
    /**
260
     * Set whether or not an argument only needs to be defined to have a value.
261
     *
262
     * @param bool $noValue
263
     */
264 12
    protected function setNoValue($noValue)
265
    {
266 12
        $this->setCastTo('bool');
267 12
        $this->noValue = (bool) $noValue;
268 12
    }
269
270
    /**
271
     * Retrieve the data type to cast an argument's value to.
272
     *
273
     * @return string
274
     */
275 4
    public function castTo()
276
    {
277 4
        return $this->castTo;
278
    }
279
280
    /**
281
     * Set the data type to cast an argument's value to.
282
     *
283
     * Valid data types are "string", "int", "float", and "bool".
284
     *
285
     * @throws \Exception if $castTo is not a valid data type.
286
     * @param string $castTo
287
     */
288 32
    protected function setCastTo($castTo)
289
    {
290 32
        if (!in_array($castTo, ['string', 'int', 'float', 'bool'])) {
291 4
            throw new \Exception(
292
                "An argument may only be cast to the data type "
293
                . "'string', 'int', 'float', or 'bool'."
294 4
            );
295
        }
296
297 28
        $this->castTo = $this->noValue() ? 'bool' : $castTo;
298 28
    }
299
300
    /**
301
     * Retrieve an argument's default value.
302
     *
303
     * @return string
304
     */
305 44
    public function defaultValue()
306
    {
307 44
        return $this->defaultValue;
308
    }
309
310
    /**
311
     * Set an argument's default value.
312
     *
313
     * @param string $defaultValue
314
     */
315 8
    public function setDefaultValue($defaultValue)
316
    {
317 8
        $this->defaultValue = $defaultValue;
318 8
    }
319
320
    /**
321
     * Retrieve an argument's value.
322
     *
323
     * Argument values are type cast based on the value of $castTo.
324
     *
325
     * @return string|int|float|bool
326
     */
327 28
    public function value()
328
    {
329 28
        return $this->value;
330
    }
331
332
    /**
333
     * Set an argument's value based on its command line entry.
334
     *
335
     * Argument values are type cast based on the value of $castTo.
336
     *
337
     * @param string|bool $value
338
     */
339 36
    public function setValue($value)
340
    {
341 36
        $cast_method = 'castTo' . ucwords($this->castTo);
342 36
        $this->value = $this->{$cast_method}($value);
343 36
    }
344
345
    /**
346
     * @param string $value
347
     *
348
     * @return string
349
     */
350 24
    protected function castToString($value)
351
    {
352 24
        return (string) $value;
353
    }
354
355
    /**
356
     * @param string $value
357
     *
358
     * @return int
359
     */
360 4
    protected function castToInt($value)
361
    {
362 4
        return (int) $value;
363
    }
364
365
    /**
366
     * @param string $value
367
     *
368
     * @return float
369
     */
370 4
    protected function castToFloat($value)
371
    {
372 4
        return (float) $value;
373
    }
374
375
    /**
376
     * @param string $value
377
     *
378
     * @return bool
379
     */
380 8
    protected function castToBool($value)
381
    {
382 8
        return (bool) $value;
383
    }
384
}
385