Completed
Pull Request — master (#303)
by Atlas
02:57
created

Parser::parse()   D

Complexity

Conditions 13
Paths 109

Size

Total Lines 47
Code Lines 25

Duplication

Lines 21
Ratio 44.68 %

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 109
nop 1
dl 21
loc 47
rs 4.952
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
use Hal\Application\Config\File\ConfigFileReaderFactory;
5
6
class Parser
7
{
8
    public function parse($argv)
9
    {
10
        $config = new Config;
11
12
        if (sizeof($argv) === 0) {
13
            return $config;
14
        }
15
16
        if (preg_match('!\.php$!', $argv[0]) || preg_match('!phpmetrics$!', $argv[0]) || preg_match('!phpmetrics.phar$!', $argv[0])) {
17
            array_shift($argv);
18
        }
19
20
        // Checking for a configuration file option key and importing options
21 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...
22
            if (preg_match('!\-\-config=(.*)!', $arg, $matches)) {
23
                $fileReader = ConfigFileReaderFactory::createFromFileName($matches[1]);
24
                $fileReader->read($config);
25
                unset($argv[$k]);
26
            }
27
        }
28
29
        // arguments with options
30 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...
31
            if (preg_match('!\-\-([\w\-]+)=(.*)!', $arg, $matches)) {
32
                list(, $parameter, $value) = $matches;
33
                $config->set($parameter, trim($value, ' "\''));
34
                unset($argv[$k]);
35
            }
36
        }
37
38
        // arguments without options
39 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...
40
            if (preg_match('!\-\-([\w\-]+)$!', $arg, $matches)) {
41
                list(, $parameter) = $matches;
42
                $config->set($parameter, true);
43
                unset($argv[$k]);
44
            }
45
        }
46
47
        // last argument
48
        $files = array_pop($argv);
49
        if ($files && !preg_match('!^\-\-!', $files)) {
50
            $config->set('files', explode(',', $files));
51
        }
52
53
        return $config;
54
    }
55
}
56