Completed
Push — master ( 4006e3...35fa08 )
by Flo
07:46
created

ArgumentParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 89
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B parseInput() 0 36 6
A set() 0 4 1
A get() 0 8 2
1
<?php
2
/**
3
 * Class ArgumentParser | ArgumentParser.php
4
 * @package ORM\Console
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\Console;
8
9
use Faulancer\Service\Config;
10
11
/**
12
 * Class ArgumentParser
13
 */
14
class ArgumentParser
15
{
16
17
    /** @var array */
18
    protected $arguments = [];
19
20
    /** @var array */
21
    protected $config;
22
23
    /**
24
     * ArgumentParser constructor.
25
     * @param array $argv
26
     * @param Config $config
27
     * @codeCoverageIgnore
28
     */
29
    public function __construct($argv, $config)
30
    {
31
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Faulancer\Service\Config> is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->parseInput($argv);
33
    }
34
35
    /**
36
     * @param array $argv
37
     * @return ConsoleInterface
38
     * @throws \Exception
39
     * @codeCoverageIgnore
40
     */
41
    protected function parseInput(array $argv)
42
    {
43
        $args = array_splice($argv, 1, count($argv));
44
45
        if (empty($args)) {
46
            throw new \Exception('Not enough parameters given.');
47
        }
48
49
        for ($i = 0; $i < count($args); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
50
51
            if (strpos($args[$i], '-') === false) {
52
                continue;
53
            } else if (empty($args[$i+1])) {
54
                break;
55
            }
56
57
            $this->set(str_replace('-', '', $args[$i]), $args[$i+1]);
58
59
        }
60
61
        if (strpos($args[0], ':') !== false) {
62
63
            $parts  = explode(':', $args[0]);
64
            $class  = $parts[0];
65
            $method = $parts[1] . 'Action';
66
            $ns     = '\Faulancer\Console\Handler\\' . ucfirst($class);
67
68
            $class = new $ns($this->config, $this);
69
70
            return call_user_func([$class, $method], $this);
71
72
        }
73
74
        throw new \Exception('No matching handler found');
75
76
    }
77
78
    /**
79
     * @param string $arg
80
     * @param string $value
81
     * @codeCoverageIgnore
82
     */
83
    public function set($arg, $value)
84
    {
85
        $this->arguments[$arg] = $value;
86
    }
87
88
    /**
89
     * @param string $arg
90
     * @return string
91
     * @codeCoverageIgnore
92
     */
93
    public function get($arg)
94
    {
95
        if (empty($this->arguments[$arg])) {
96
            return '';
97
        }
98
99
        return $this->arguments[$arg];
100
    }
101
102
}