SignatureParser::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Artisanize;
4
5
use Artisanize\Input\Option;
6
use Artisanize\Input\Argument;
7
8
class SignatureParser
9
{
10
    /**
11
     * The command to build.
12
     *
13
     * @var Command
14
     */
15
    protected $command;
16
17
    /**
18
     * Construct.
19
     *
20
     * @param Command $command
21
     */
22
    public function __construct(Command $command)
23
    {
24
        $this->command = $command;
25
    }
26
27
    /**
28
     * Parse the command signature.
29
     *
30
     * @param string $signature
31
     */
32
    public function parse($signature)
33
    {
34
        $this->setName($signature);
35
36
        $argumentsOptions = $this->extractArgumentsOptions($signature);
37
38
        foreach ($argumentsOptions as $value) {
39
            if (substr($value, 0, 2) !== '--') {
40
                $input = new Argument($value);
41
            } else {
42
                $input = new Option(trim($value, '--'));
43
            }
44
45
            $this->command->addInput($input->parse());
46
        }
47
    }
48
49
    /**
50
     * Set the command name.
51
     *
52
     * @param string $signature
53
     */
54
    protected function setName($signature)
55
    {
56
        $this->command->setName(preg_split('/\s+/', $signature)[0]);
57
    }
58
59
    /**
60
     * Extract arguments and options from signature.
61
     *
62
     * @param string $signature
63
     *
64
     * @return array
65
     */
66
    protected function extractArgumentsOptions($signature)
67
    {
68
        preg_match_all('/{(.*?)}/', $signature, $argumentsOption);
69
70
        return array_map(function ($item) {
71
            return trim($item, '{}');
72
        }, $argumentsOption[1]);
73
    }
74
}
75