Completed
Push — dev ( a8234e...9312cf )
by Zach
04:40
created

Argument::setOptional()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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