Completed
Push — master ( 8bde3f...f8d0b5 )
by James Ekow Abaka
02:09
created

Options   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 67
ccs 34
cts 34
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 0 18 6
B fillOption() 0 8 5
A getMap() 0 4 1
A stringOptionToArray() 0 10 2
A getArray() 0 4 1
1
<?php
2
3
namespace clearice;
4
5
class Options //implements \ArrayAccess, \Iterator
6
{
7
    private $options = [];
8
    private $index = 0;
0 ignored issues
show
Unused Code introduced by
The property $index is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
    
10
    /**
11
     * A map of all the options the parser recognises. 
12
     * The map is actually an array which associates short or long options with 
13
     * their appropriate parameters. Options which have both long and short 
14
     * versions would be repeated. This structure is used to quickly find the 
15
     * paramters of an option whether in the short form or long form. This 
16
     * parameter is automatically populated by the library as options are added.
17
     * 
18
     * @var array
19
     */    
20
    private $map;
21
    
22 27
    public function add($options)
23
    {
24 27
        foreach ($options as $option) {
25 27
            if (is_string($option)) {
26 4
                $option = $this->stringOptionToArray($option);
27 4
            }
28 27
            $option = $this->fillOption($option);
29 27
            $this->options[] = $option;
30 27
            $command = isset($option['command']) ? $option['command'] : '__default__';
31 27
            if (isset($option['short'])) {
32 26
                $this->map[$command][$option['short']] = $option;
33 26
            }
34 27
            if (isset($option['long'])) {
35 27
                $this->map[$command][$option['long']] = $option;
36 27
            }
37 27
        }        
38 27
        $this->options += $options;
39 27
    }
40
41 27
    private function fillOption($option)
42
    {
43 27
        $option['has_value'] = isset($option['has_value']) ? $option['has_value'] : false;
44 27
        $option['command'] = isset($option['command']) ? $option['command'] : null;
45 27
        $option['multi'] = isset($option['multi']) ? $option['multi'] : null;
46 27
        $option['group'] = isset($option['group']) ? $option['group'] : null;
47 27
        return $option;
48
    }
49
    
50 24
    public function getMap()
51
    {
52 24
        return $this->map;
53
    }
54
    
55 4
    private function stringOptionToArray($option)
56
    {
57 4
        $newOption = [];
58 4
        if (strlen($option) == 1) {
59 1
            $newOption['short'] = $option;
60 1
        } else {
61 4
            $newOption['long'] = $option;
62
        }
63 4
        return $newOption;
64
    }
65
    
66 8
    public function getArray()
67
    {
68 8
        return $this->options;
69
    }
70
71
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
72
73