Completed
Push — master ( 9d133c...2cfb2f )
by Tomas
03:26
created

InputParam::getValue()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 28
Code Lines 17

Duplication

Lines 18
Ratio 64.29 %

Code Coverage

Tests 13
CRAP Score 13.5941

Importance

Changes 6
Bugs 2 Features 3
Metric Value
c 6
b 2
f 3
dl 18
loc 28
ccs 13
cts 18
cp 0.7222
rs 5.2653
cc 11
eloc 17
nc 9
nop 0
crap 13.5941

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
namespace Tomaj\NetteApi\Params;
4
5
use Exception;
6
7
class InputParam implements ParamInterface
8
{
9
    const TYPE_POST = 'POST';
10
    const TYPE_GET  = 'GET';
11
    const TYPE_FILE = 'FILE';
12
13
    const OPTIONAL = false;
14
    const REQUIRED = true;
15
16
    /**
17
     * @var string
18
     */
19
    private $type;
20
21
    /**
22
     * @var string
23
     */
24
    private $key;
25
26
    /**
27
     * @var bool
28
     */
29
    private $required;
30
31
    /**
32
     * @var array|null
33
     */
34
    private $availableValues;
35
36
    /**
37
     * @var bool
38
     */
39
    private $multi;
40
41 18
    public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false)
42
    {
43 18
        $this->type = $type;
44 18
        $this->key = $key;
45 18
        $this->required = $required;
46 18
        $this->availableValues = $availableValues;
47 18
        $this->multi = $multi;
48 18
    }
49
50
    /**
51
     * @return string
52
     */
53 3
    public function getType()
54
    {
55 3
        return $this->type;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 12
    public function getKey()
62
    {
63 12
        return $this->key;
64
    }
65
66
    /**
67
     * @return boolean
68
     */
69 3
    public function isRequired()
70
    {
71 3
        return $this->required;
72
    }
73
74 3
    public function getAvailableValues()
75
    {
76 3
        return $this->availableValues;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82 9
    public function isMulti()
83
    {
84 9
        return $this->multi;
85
    }
86
87
    /**
88
     * Check if actual value from environment is valid
89
     *
90
     * @return bool
91
     *
92
     * @throws Exception if actual InputParam has unsupported type
93
     */
94 9
    public function isValid()
95
    {
96 9
        $value = $this->getValue();
97 9
        if ($this->availableValues != null) {
98 3
            if (is_array($this->availableValues)) {
99 3
                return in_array($value, $this->availableValues);
100
            }
101
        }
102
103 9
        if ($this->required) {
104 9
            if ($value == null || $value = '') {
105 3
                return false;
106
            }
107 6
            if (is_string($this->availableValues)) {
108
                return $value == $this->availableValues;
109
            }
110 6
        }
111 6
        return true;
112
    }
113
114
    /**
115
     * Process environment variables like POST|GET|etc.. and return actual value
116
     *
117
     * @return mixed
118
     *
119
     * @throws Exception if actual InputParam has unsupported type
120
     */
121 15
    public function getValue()
0 ignored issues
show
Coding Style introduced by
getValue uses the super-global variable $_GET 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...
Coding Style introduced by
getValue uses the super-global variable $_POST 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...
Coding Style introduced by
getValue uses the super-global variable $_FILES 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...
122
    {
123 15 View Code Duplication
        if ($this->type == self::TYPE_GET) {
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...
124 6
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
125 3
                return $_GET[$this->key];
126
            }
127 3
            if ($this->isMulti()) {
128
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
129
            }
130 3
            return filter_input(INPUT_GET, $this->key);
131
        }
132 15 View Code Duplication
        if ($this->type == self::TYPE_POST) {
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...
133 12
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
134 6
                return $_POST[$this->key];
135
            }
136 9
            if ($this->isMulti()) {
137
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
138
            }
139 9
            return filter_input(INPUT_POST, $this->key);
140
        }
141 3
        if ($this->type == self::TYPE_FILE) {
142
            if (isset($_FILES[$this->key])) {
143
                return $_FILES[$this->key];
144
            }
145
        }
146
147 3
        throw new Exception("Invalid type");
148
    }
149
}
150