Completed
Push — master ( bdaaad...4f4372 )
by personal
06:57 queued 04:36
created

Parser::parse()   C

Complexity

Conditions 11
Paths 37

Size

Total Lines 38
Code Lines 20

Duplication

Lines 14
Ratio 36.84 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 37
nop 1
dl 14
loc 38
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Hal\Application\Config;
3
4
class Parser
5
{
6
7
    public function parse($argv)
8
    {
9
        $config = new Config;
10
11
        if (sizeof($argv) === 0) {
12
            return $config;
13
        }
14
15
        if (preg_match('!\.php$!', $argv[0]) || preg_match('!phpmetrics$!', $argv[0]) || preg_match('!phpmetrics.phar$!', $argv[0])) {
16
            array_shift($argv);
17
        }
18
19
        // arguments with options
20 View Code Duplication
        foreach ($argv as $k => $arg) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
            if (preg_match('!\-\-([\w\-]+)=(.*)!', $arg, $matches)) {
22
                list(, $parameter, $value) = $matches;
23
                $config->set($parameter, trim($value, ' "\''));
24
                unset($argv[$k]);
25
            }
26
        }
27
28
        // arguments without options
29 View Code Duplication
        foreach ($argv as $k => $arg) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
            if (preg_match('!\-\-([\w\-]+)$!', $arg, $matches)) {
31
                list(, $parameter) = $matches;
32
                $config->set($parameter, true);
33
                unset($argv[$k]);
34
            }
35
        }
36
37
        // last argument
38
        $files = array_pop($argv);
39
        if ($files && !preg_match('!^\-\-!', $files)) {
40
            $config->set('files', explode(',', $files));
41
        }
42
43
        return $config;
44
    }
45
}