CliOptionParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parse() 0 14 3
1
<?php
2
namespace Peridot\Console;
3
4
use Symfony\Component\Console\Input\ArgvInput;
5
6
/**
7
 * The CliOptionParser parser searches an array of
8
 * arguments for the specified options and returns their
9
 * values.
10
 *
11
 * @package Peridot\Console
12
 */
13
class CliOptionParser
14
{
15
    /**
16
     * CLI options to search for
17
     *
18
     * @var array
19
     */
20
    protected $search;
21
22
    /**
23
     * The CLI arguments to search against
24
     *
25
     * @var array
26
     */
27
    protected $arguments;
28
29
    /**
30
     * @var array $search
31
     * @var array $arguments
32
     */
33
    public function __construct(array $search, array $arguments)
34
    {
35
        $this->search = $search;
36
        $this->arguments = $arguments;
37
    }
38
39
    /**
40
     * Parse arguments to find any options specified
41
     * in the search array
42
     *
43
     * @return array $parsed
44
     */
45
    public function parse()
46
    {
47
        $input = new ArgvInput($this->arguments);
48
        $parsed = [];
49
50
        foreach ($this->search as $option) {
51
            if ($input->hasParameterOption($option)) {
52
                $name = ltrim($option, '-');
53
                $parsed[$name] = $input->getParameterOption($option);
54
            }
55
        }
56
57
        return $parsed;
58
    }
59
}
60