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:
1 | <?php |
||
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) |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | 9 | public function getType() |
|
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | 21 | public function getKey() |
|
69 | |||
70 | /** |
||
71 | * @return boolean |
||
72 | */ |
||
73 | 6 | public function isRequired() |
|
77 | |||
78 | 6 | public function getAvailableValues() |
|
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | */ |
||
86 | 21 | public function isMulti() |
|
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->required == self::OPTIONAL) { |
|
101 | 6 | return true; |
|
102 | } |
||
103 | |||
104 | 18 | $value = $this->getValue(); |
|
105 | 18 | if ($this->availableValues !== null) { |
|
106 | 9 | if (is_array($this->availableValues)) { |
|
107 | 6 | return empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues)); |
|
108 | } |
||
109 | 1 | } |
|
110 | |||
111 | 18 | if ($this->required) { |
|
112 | 18 | if ($value === null || $value == '') { |
|
113 | 9 | return false; |
|
114 | } |
||
115 | 9 | if (is_string($this->availableValues)) { |
|
116 | 3 | return $value == $this->availableValues; |
|
117 | } |
||
118 | 2 | } |
|
119 | 6 | return true; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Process environment variables like POST|GET|etc.. and return actual value |
||
124 | * |
||
125 | * @return mixed |
||
126 | * |
||
127 | * @throws Exception if actual InputParam has unsupported type |
||
128 | */ |
||
129 | 39 | public function getValue() |
|
130 | { |
||
131 | 39 | View Code Duplication | if ($this->type == self::TYPE_GET) { |
132 | 15 | if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) { |
|
133 | 6 | return $_GET[$this->key]; |
|
134 | } |
||
135 | 9 | if ($this->isMulti()) { |
|
136 | return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
||
137 | } |
||
138 | 9 | return filter_input(INPUT_GET, $this->key); |
|
139 | } |
||
140 | 36 | View Code Duplication | if ($this->type == self::TYPE_POST) { |
141 | 12 | if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) { |
|
142 | 6 | return $_POST[$this->key]; |
|
143 | } |
||
144 | 9 | if ($this->isMulti()) { |
|
145 | return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
||
146 | } |
||
147 | 9 | return filter_input(INPUT_POST, $this->key); |
|
148 | } |
||
149 | 27 | if ($this->type == self::TYPE_FILE) { |
|
150 | 9 | if (isset($_FILES[$this->key])) { |
|
151 | 6 | if ($this->isMulti()) { |
|
152 | 3 | return $this->processMultiFileUploads($_FILES[$this->key]); |
|
153 | } else { |
||
154 | 3 | return $_FILES[$this->key]; |
|
155 | } |
||
156 | } |
||
157 | 3 | return null; |
|
158 | } |
||
159 | 18 | if ($this->type == self::TYPE_COOKIE) { |
|
160 | 3 | if (isset($_COOKIE[$this->key])) { |
|
161 | 3 | return $_COOKIE[$this->key]; |
|
162 | } |
||
163 | } |
||
164 | 15 | if ($this->type == self::TYPE_POST_RAW) { |
|
165 | 3 | return file_get_contents("php://input"); |
|
166 | } |
||
167 | 12 | if ($this->type == self::TYPE_PUT) { |
|
168 | 6 | parse_str(file_get_contents("php://input"), $params); |
|
169 | 6 | if (isset($params[$this->key])) { |
|
170 | return $params[$this->key]; |
||
171 | } |
||
172 | 6 | return ''; |
|
173 | } |
||
174 | 6 | if ($this->type == self::TYPE_POST_JSON_KEY) { |
|
175 | 3 | $params = file_get_contents("php://input"); |
|
176 | 3 | $params = @json_decode($params, true); |
|
177 | 3 | if (isset($params[$this->key])) { |
|
178 | return $params[$this->key]; |
||
179 | } |
||
180 | 3 | return ''; |
|
181 | } |
||
182 | 3 | throw new Exception('Invalid type'); |
|
183 | } |
||
184 | |||
185 | 3 | private function processMultiFileUploads($files) |
|
195 | } |
||
196 |
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: