Completed
Push — develop ( 9ac21c...a157fb )
by Tom
04:58
created

OptionParser::hasLongOption()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 6
eloc 15
nc 3
nop 1
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;
9
10
/**
11
 * Ad-hoc options parser, used to deal with shortcoming of getopt() with Symfony style command-line options, arguments
12
 * and
13
 */
14
final class OptionParser
15
{
16
    private $argv;
17
18
    public function __construct(array $argv)
19
    {
20
        $this->argv = $argv;
21
    }
22
23
    /**
24
     * @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...
25
     * @return OptionParser
26
     */
27
    public static function init(array $argv = null)
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_SERVER 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...
28
    {
29
        if (null === $argv) {
30
            $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
31
        }
32
33
        return new self($argv);
34
    }
35
36
    /**
37
     * Check for long-opt option existance
38
     *
39
     * @param string $name of long option (e.g. root-dir for --root-dir)
40
     * @return null|true
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
41
     */
42
    public function hasLongOption($name)
43
    {
44
        $long = "--$name";
45
46
        $args = $this->argv;
47
48
        // utility
49
        array_shift($args);
50
51
        while ($option = array_shift($args)) {
52
            if ('--' === $option) {
53
                break;
54
            }
55
            $len = strlen($option);
56
            if (!$len) {
57
                continue;
58
            }
59
            if ('-' !== $option[0]) {
60
                continue;
61
            }
62
            if ($long === $option) {
63
                return true;
64
            }
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * Obtain the non-optional argument of a long-opt
72
     *
73
     * Note: This method does not differ between a non-existent option
74
     *       or an option with no argument or an option whichs
75
     *       argument is missing and then taking the next argument as
76
     *       the options' argument.
77
     *
78
     * @param string $name of long option (e.g. root-dir for --root-dir)
79
     * @return null|string option argument if it exists, otherwise null
80
     */
81
    public function getLongOptionArgument($name)
82
    {
83
        $long = "--$name";
84
85
        $args = $this->argv;
86
87
        // utility
88
        array_shift($args);
89
90
        while ($option = array_shift($args)) {
91
            if ('--' === $option) {
92
                break;
93
            }
94
            $len = strlen($option);
95
            if (!$len) {
96
                continue;
97
            }
98
            if ('-' !== $option[0]) {
99
                continue;
100
            }
101
            if ($long === $option) {
102
                if (null !== $argument = array_shift($args)) {
103
                    $path = $argument;
104
                }
105
                break;
106
            }
107
            if ($long . '=' === substr($option, 0, $optLen = strlen($long) + 1)) {
108
                if ($len > $optLen) {
109
                    $path = substr($option, $optLen);
110
                }
111
                break;
112
            }
113
        }
114
115
        return isset($path) ? $path : null;
116
    }
117
}
118