Completed
Push — master ( 5f180f...861647 )
by arto
02:09
created

Parser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-11-26
5
 */
6
namespace Net\Bazzline\Component\Cli\Arguments;
7
8
class Parser
9
{
10
    /** @var Collection */
11
    private $flags;
12
13
    /** @var Collection */
14
    private $lists;
15
16
    /** @var Collection */
17
    private $values;
18
19
    public function __construct()
20
    {
21
        $this->initiate();
22
    }
23
24
    /**
25
     * @param array $arguments
26
     */
27
    public function parse(array $arguments)
28
    {
29
        $this->initiate();
30
31
        foreach ($arguments as $argument) {
32
            $this->addToFittingCollection($argument);
33
        }
34
    }
35
36
    /**
37
     * @return Collection
38
     */
39
    public function getFlags()
40
    {
41
        return $this->flags;
42
    }
43
44
    /**
45
     * @return Collection
46
     */
47
    public function getLists()
48
    {
49
        return $this->lists;
50
    }
51
52
    /**
53
     * @return Collection
54
     */
55
    public function getValues()
56
    {
57
        return $this->values;
58
    }
59
60
    /**
61
     * @param string $name
62
     * @param mixed $value
63
     */
64
    private function addToList($name, $value)
65
    {
66
        $list   = $this->lists;
67
        $value  = trim($value, '"'); //remove >"< if exists
68
69
        if ($list->containsKey($name)) {
70
            $collection = $list->getOne($name);
71
        } else {
72
            //$collection = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
            $collection = new Collection();
74
        }
75
76
        $collection[]   = $value;
77
        $list[$name]    = $collection;
78
    }
79
80
    /**
81
     * @param string $string
82
     * @param string $search
83
     * @return bool
84
     */
85
    private function contains($string, $search)
86
    {
87
        if (strlen($search) === 0) {
88
            $contains = false;
89
        } else {
90
            $contains = !(strpos($string, $search) === false);
91
        }
92
93
        return $contains;
94
    }
95
96
    private function handleLongNameListOrFlag($argument)
97
    {
98
        if ($this->contains($argument, '=')) {
99
            $position   = strpos($argument, '=');
100
            $name       = substr($argument, 0, $position);
101
            $value      = substr($argument, ($position + 1));
102
            $this->addToList($name, $value);
103
        } else {
104
            $this->flags[] = $argument;
105
        }
106
    }
107
108
    private function handleShortNameListOrFlag($argument)
109
    {
110
        $containsEqualCharacter             = ($this->contains($argument, '='));
111
        $equalCharacterIsOnSecondPosition   = (strpos($argument, '=') === 1);
112
        $isShortNameList                    = ($containsEqualCharacter
113
            && $equalCharacterIsOnSecondPosition);
114
115
        if ($isShortNameList) {
116
            $name   = substr($argument, 0, 1);
117
            $value  = substr($argument, 2);
118
            $this->addToList($name, $value);
119
        } else if (!$containsEqualCharacter) {
120
            $length = strlen($argument);
121
            $iterator = 0;
122
            while ($iterator < $length) {
123
                $this->flags[] = $argument{$iterator};
124
                ++$iterator;
125
            }
126
        }
127
    }
128
129
    private function initiate()
130
    {
131
        $this->flags    = new Collection();
132
        $this->lists    = new Collection();
133
        $this->values   = new Collection();
134
    }
135
136
    /**
137
     * @param $argument
138
     */
139
    private function addToFittingCollection($argument)
140
    {
141
        if ($this->startsWith($argument, '--')) {
142
            $argument = substr($argument, 2);
143
            $this->handleLongNameListOrFlag($argument);
144
        } else if ($this->startsWith($argument, '-')) {
145
            $argument = substr($argument, 1);
146
            $this->handleShortNameListOrFlag($argument);
147
        } else {
148
            $this->values[] = $argument;
149
        }
150
    }
151
152
    /**
153
     * @param string $string
154
     * @param string $start
155
     * @return bool
156
     */
157
    private function startsWith($string, $start)
158
    {
159
        return (strncmp($string, $start, strlen($start)) === 0);
160
    }
161
}