Cli::getDefinedOpt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Request;
3
4
use FMUP\Request;
5
6
class Cli extends Request
7
{
8
    const SHORT = 'SHORT';
9
    const LONG = 'LONG';
10
11
    private $opt = array(self::SHORT => '', self::LONG => array('route:'));
12
13
    /**
14
     * @param string $short
15
     * @param array $long
16
     * @return $this
17
     */
18 1
    public function defineOpt($short = '', array $long = array())
19
    {
20 1
        $long[] = 'route:';
21 1
        $this->opt = array(
22 1
            self::SHORT => (string)$short,
23 1
            self::LONG => (array)$long,
24
        );
25 1
        return $this;
26
    }
27
28
    /**
29
     * Retrieve defined opt
30
     * @return array
31
     */
32 1
    public function getDefinedOpt()
33
    {
34 1
        return $this->opt;
35
    }
36
37
    /**
38
     * @param string $name
39
     * @param mixed $defaultValue
40
     * @return string|mixed
41
     */
42 1
    public function get($name, $defaultValue = null)
43
    {
44 1
        $options = $this->getOpt();
45 1
        return isset($options[$name]) ? $options[$name] : $defaultValue;
46
    }
47
48
    /**
49
     * @return array
50
     */
51 1
    public function getOpt()
52
    {
53 1
        return $this->phpGetOpt($this->opt[self::SHORT], $this->opt[self::LONG]);
54
    }
55
56
    /**
57
     * @param string $short
58
     * @param array $long
59
     * @return array
60
     * @codeCoverageIgnore
61
     */
62
    protected function phpGetOpt($short, array $long = array())
63
    {
64
        return getopt((string)$short, $long);
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return bool
70
     */
71 1
    public function has($name)
72
    {
73 1
        return isset($this->getOpt()[$name]);
74
    }
75
76
    /**
77
     * @param bool|false $withQuerySting
78
     * @return string
79
     */
80 2
    public function getRequestUri($withQuerySting = false)
0 ignored issues
show
Coding Style introduced by
getRequestUri 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...
81
    {
82 2
        $return = $this->get('route');
83 2
        if (isset($_SERVER['argc']) && $_SERVER['argc'] > 2 && $withQuerySting) {
84 1
            $args = $_SERVER['argv'];
85 1
            unset($args[0]);
86 1
            $return = implode(' ', $args);
87
        }
88 2
        return $return;
89
    }
90
}
91