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

SignatureParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 18 3
A setName() 0 4 1
A extractArgumentsOptions() 0 8 1
1
<?php
2
3
namespace Yarak\Console\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputArgument;
8
9
class SignatureParser
10
{
11
    /**
12
     * The command to build.
13
     *
14
     * @var Command
15
     */
16
    protected $command;
17
18
    /**
19
     * Construct.
20
     *
21
     * @param Command $command
22
     */
23
    public function __construct(Command $command)
24
    {
25
        $this->command = $command;
26
    }
27
28
    /**
29
     * Parse the command signature.
30
     *
31
     * @param  string $signature
32
     */
33
    public function parse($signature)
34
    {
35
        $this->setName($signature);
36
37
        $argumentsOptions = $this->extractArgumentsOptions($signature);
38
39
        foreach ($argumentsOptions as $value) {
40
            if (substr($value, 0, 2) !== '--') {
41
                $parser = new ArgumentParser($this->command);
42
            } else {
43
                $parser = new OptionParser($this->command);
44
45
                $value = trim($value, '--');
46
            }
47
48
            $parser->handle($value);
49
        }
50
    }
51
52
    /**
53
     * Set the command name.
54
     *
55
     * @param string $signature
56
     */
57
    protected function setName($signature)
58
    {
59
        $this->command->setName(preg_split('/\s+/', $signature)[0]);
60
    }
61
62
    /**
63
     * Extract arguments and options from signature.
64
     *
65
     * @param  string $signature
66
     *
67
     * @return array
68
     */
69
    protected function extractArgumentsOptions($signature)
70
    {
71
        preg_match_all('/{(.*?)}/', $signature, $argumentsOption);
72
73
        return array_map(function ($item) {
74
            return trim($item, '{}');
75
        }, $argumentsOption[1]);
76
    }
77
}
78