Completed
Push — develop ( 6abc8a...fc21c0 )
by Tom
04:23
created

RootDir::getArgument()   C

Complexity

Conditions 12
Paths 54

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 37
rs 5.1612
cc 12
eloc 23
nc 54
nop 1

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
/**
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Magento\Application\Option;
9
10
11
/**
12
 * --root-dir option needs a specialized parser b/c despite defined with
13
 * the symfony console API, it's not usable at early stages and
14
 * getopt() fails as well.
15
 */
16
class RootDir
17
{
18
    /**
19
     * @param array $argv (optional)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $argv not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
20
     *
21
     * @return null|string --root-dir option argument if it exists, otherwise null
22
     */
23
    public static function getArgument(array $argv = null)
0 ignored issues
show
Coding Style introduced by
getArgument uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
24
    {
25
        if (null === $argv) {
26
            $argv = isset($GLOBALS['argv']) ? $GLOBALS['argv'] : array();
27
        }
28
29
        $args = $argv;
30
        $utility = array_shift($args);
31
        unset($utility);
32
33
        while ($option = array_shift($args)) {
34
            if ('--' === $option) {
35
                break;
36
            }
37
            $len = strlen($option);
38
            if (!$len) {
39
                continue;
40
            }
41
            if ('-' !== $option[0]) {
42
                continue;
43
            }
44
            if ('--root-dir' === $option) {
45
                if (null !== $argument = array_shift($args)) {
46
                    $path = $argument;
47
                    break;
48
                }
49
            }
50
            if ('--root-dir=' === substr($option, 0, 11)) {
51
                if ($len > 11) {
52
                    $path = substr($option, 11);
53
                }
54
                break;
55
            }
56
        }
57
58
        return isset($path) ? $path : null;
59
    }
60
}
61