Completed
Push — master ( e19b31...4da4bd )
by Scott
02:04
created

ArgParser::letterArg()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
class ArgParser
5
{
6
    protected $args;
7
8
    public function __construct(array $args)
9
    {
10
        $this->args = $args;
11
    }
12
13
    public function getArg($name)
14
    {
15
        if (is_numeric($name)) {
16
            return $this->numericArg($name);
17
        }
18
19
        return $this->letterArg($name);
20
    }
21
22
    protected function numericArg($position)
23
    {
24
        foreach ($this->args as $arg) {
25
            if ($arg{0} != '-' && $position-- == 0) {
26
                return $arg;
27
            }
28
        }
29
30
        return null;
31
    }
32
33
    protected function letterArg($name)
34
    {
35
        $name = $this->getAdjustedArg($name);
36
        foreach ($this->args as $arg) {
37
            list($value, $arg) = $this->splitArg($arg);
38
39
            if ($arg{0} == '-' && $name == $arg) {
40
                return $value;
41
            }
42
        }
43
44
        return false;
45
    }
46
47
    protected function splitArg($arg)
48
    {
49
        $value = true;
50
        if (strpos($arg, '=')) {
51
            list($arg, $value) = explode('=', $arg, 2);
52
        }
53
54
        return array($value, $arg);
55
    }
56
57
    /**
58
     * @param $name
59
     * @return string
60
     */
61
    protected function getAdjustedArg($name)
62
    {
63
        $name = strlen($name) == 1 ?
64
            '-' . $name :
65
            '--' . $name;
66
        return $name;
67
    }
68
}
69