Completed
Pull Request — master (#61)
by Michal
08:40
created

InputParam   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 197
Duplicated Lines 9.14 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.42%

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 0
dl 18
loc 197
ccs 65
cts 77
cp 0.8442
rs 9.2
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getType() 0 4 1
A getKey() 0 4 1
A isRequired() 0 4 1
A getAvailableValues() 0 4 1
A isMulti() 0 4 1
C isValid() 0 31 12
D getValue() 18 55 19
A processMultiFileUploads() 0 10 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like InputParam often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use InputParam, and based on these observations, apply Extract Interface, too.

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