Completed
Push — master ( f71e62...3bdf74 )
by Tomas
02:54
created

InputParam::getValue()   D

Complexity

Conditions 17
Paths 18

Size

Total Lines 48
Code Lines 31

Duplication

Lines 18
Ratio 37.5 %

Code Coverage

Tests 27
CRAP Score 17.6203

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 18
loc 48
ccs 27
cts 31
cp 0.871
rs 4.9412
cc 17
eloc 31
nc 18
nop 0
crap 17.6203

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_PUT       = 'PUT';
12
    const TYPE_FILE      = 'FILE';
13
    const TYPE_COOKIE    = 'COOKIE';
14
    const TYPE_POST_RAW  = 'POST_RAW';
15
16
    const OPTIONAL = false;
17
    const REQUIRED = true;
18
19
    /**
20
     * @var string
21
     */
22
    private $type;
23
24
    /**
25
     * @var string
26
     */
27
    private $key;
28
29
    /**
30
     * @var bool
31
     */
32
    private $required;
33
34
    /**
35
     * @var array|null
36
     */
37
    private $availableValues;
38
39
    /**
40
     * @var bool
41
     */
42
    private $multi;
43
44 48
    public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false)
45
    {
46 48
        $this->type = $type;
47 48
        $this->key = $key;
48 48
        $this->required = $required;
49 48
        $this->availableValues = $availableValues;
50 48
        $this->multi = $multi;
51 48
    }
52
53
    /**
54
     * @return string
55
     */
56 9
    public function getType()
57
    {
58 9
        return $this->type;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 21
    public function getKey()
65
    {
66 21
        return $this->key;
67
    }
68
69
    /**
70
     * @return boolean
71
     */
72 6
    public function isRequired()
73
    {
74 6
        return $this->required;
75
    }
76
77 6
    public function getAvailableValues()
78
    {
79 6
        return $this->availableValues;
80
    }
81
82
    /**
83
     * @return bool
84
     */
85 21
    public function isMulti()
86
    {
87 21
        return $this->multi;
88
    }
89
90
    /**
91
     * Check if actual value from environment is valid
92
     *
93
     * @return bool
94
     *
95
     * @throws Exception if actual InputParam has unsupported type
96
     */
97 18
    public function isValid()
98
    {
99 18
        if ($this->required == self::OPTIONAL) {
100 6
            return true;
101
        }
102
103 18
        $value = $this->getValue();
104 18
        if ($this->availableValues !== null) {
105 9
            if (is_array($this->availableValues)) {
106 6
                return empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues));
107
            }
108 1
        }
109
110 15
        if ($this->required) {
111 15
            if ($value === null || $value == '') {
112 6
                return false;
113
            }
114 9
            if (is_string($this->availableValues)) {
115 3
                return $value == $this->availableValues;
116
            }
117 2
        }
118 6
        return true;
119
    }
120
121
    /**
122
     * Process environment variables like POST|GET|etc.. and return actual value
123
     *
124
     * @return mixed
125
     *
126
     * @throws Exception if actual InputParam has unsupported type
127
     */
128 39
    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...
Coding Style introduced by
getValue uses the super-global variable $_COOKIE 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...
129
    {
130 39 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...
131 15
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
132 6
                return $_GET[$this->key];
133
            }
134 9
            if ($this->isMulti()) {
135
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
136
            }
137 9
            return filter_input(INPUT_GET, $this->key);
138
        }
139 33 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...
140 12
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
141 6
                return $_POST[$this->key];
142
            }
143 9
            if ($this->isMulti()) {
144
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
145
            }
146 9
            return filter_input(INPUT_POST, $this->key);
147
        }
148 24
        if ($this->type == self::TYPE_FILE) {
149 9
            if (isset($_FILES[$this->key])) {
150 6
                if ($this->isMulti()) {
151 3
                    return $this->processMultiFileUploads($_FILES[$this->key]);
152
                } else {
153 3
                    return $_FILES[$this->key];
154
                }
155
            }
156 3
            return null;
157
        }
158 15
        if ($this->type == self::TYPE_COOKIE) {
159 3
            if (isset($_COOKIE[$this->key])) {
160 3
                return $_COOKIE[$this->key];
161
            }
162
        }
163 12
        if ($this->type == self::TYPE_POST_RAW) {
164 3
            return file_get_contents("php://input");
165
        }
166 9
        if ($this->type == self::TYPE_PUT) {
167 6
            parse_str(file_get_contents("php://input"), $params);
168 6
            if (isset($params[$this->key])) {
169
                return $params[$this->key];
170
            }
171 6
            return '';
172
        }
173
174 3
        throw new Exception('Invalid type');
175
    }
176
177 3
    private function processMultiFileUploads($files)
178
    {
179 3
        $result = [];
180 3
        foreach ($files as $key => $values) {
181 3
            foreach ($values as $index => $value) {
182 3
                $result[$index][$key] = $value;
183 1
            }
184 1
        }
185 3
        return $result;
186
    }
187
}
188