1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console\Input; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
|
7
|
|
|
class Argument extends Input |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Default arguement value. |
11
|
|
|
* |
12
|
|
|
* @var null|string |
13
|
|
|
*/ |
14
|
|
|
protected $default = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Parse the set argument string. |
18
|
|
|
* |
19
|
|
|
* @return $this |
20
|
|
|
*/ |
21
|
|
|
public function parse() |
22
|
|
|
{ |
23
|
|
|
return $this->setDescription() |
24
|
|
|
->setArray('IS_ARRAY') |
25
|
|
|
->setOptional() |
26
|
|
|
->setDefault() |
27
|
|
|
->setRequired() |
28
|
|
|
->calculateMode(InputArgument::class) |
29
|
|
|
->setName(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set optional argument mode. |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
protected function setOptional() |
38
|
|
|
{ |
39
|
|
|
if (substr($this->input, -1) === '?') { |
40
|
|
|
$this->modeArray[] = 'OPTIONAL'; |
41
|
|
|
|
42
|
|
|
$this->input = str_replace('?', '', $this->input); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set default option value. |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
protected function setDefault() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (strpos($this->input, '=')) { |
56
|
|
|
list($this->input, $this->default) = explode('=', $this->input); |
57
|
|
|
|
58
|
|
|
$this->modeArray[] = 'OPTIONAL'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set required mode if optional not already set. |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
protected function setRequired() |
70
|
|
|
{ |
71
|
|
|
if (!in_array('OPTIONAL', $this->modeArray)) { |
72
|
|
|
$this->modeArray[] = 'REQUIRED'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the argument attributes. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getAttributes() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
'name' => $this->name, |
87
|
|
|
'mode' => $this->mode, |
88
|
|
|
'description' => $this->description, |
89
|
|
|
'default' => $this->default, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.