Completed
Push — dev ( 122982...5e45c8 )
by Zach
02:18
created

ArgumentParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 25 3
A findOptional() 0 10 2
1
<?php
2
3
namespace Yarak\Console\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
8
class ArgumentParser extends Parser
9
{
10
    /**
11
     * Argument name.
12
     *
13
     * @var string
14
     */
15
    protected $name = '';
16
17
    /**
18
     * Array of argument modes to apply.
19
     *
20
     * @var array
21
     */
22
    protected $modeArray = [];
23
24
    /**
25
     * Argument description.
26
     *
27
     * @var null|string
28
     */
29
    protected $description = null;
30
31
    /**
32
     * Default arguement value.
33
     *
34
     * @var null|string
35
     */
36
    protected $default = null;
37
38
    /**
39
     * Parse the given arguement string.
40
     *
41
     * @param  string $argument
42
     */
43
    public function handle($argument)
44
    {
45
        list($argument, $this->description) = $this->parseDescription($argument);
46
47
        $argument = $this->parseArray($argument, 'IS_ARRAY');
48
49
        $argument = $this->findOptional($argument);
50
51
        if (strpos($argument, '=')) {
52
            list($argument, $this->default) = explode('=', $argument);
53
54
            $this->modeArray[] = 'OPTIONAL';
55
        }
56
57
        if (!in_array('OPTIONAL', $this->modeArray)) {
58
            $this->modeArray[] = 'REQUIRED';
59
        }
60
61
        $this->command->addArgument(
62
            $argument,
63
            $this->calculateMode($this->modeArray, InputArgument::class),
64
            $this->description,
65
            $this->default
66
        );
67
    }
68
69
    /**
70
     * Parse the argument mode.
71
     *
72
     * @param  string $argument
73
     *
74
     * @return string
75
     */
76
    protected function findOptional($argument)
77
    {
78
        if (substr($argument, -1) === '?') {
79
            $this->modeArray[] = 'OPTIONAL';
80
81
            return str_replace('?', '', $argument);
82
        }
83
84
        return $argument;
85
    }
86
}
87