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 | |||
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 | 45 | ||
44 | public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false) |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | 9 | */ |
|
56 | public function getType() |
||
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | 21 | */ |
|
64 | public function getKey() |
||
68 | |||
69 | /** |
||
70 | * @return boolean |
||
71 | 6 | */ |
|
72 | public function isRequired() |
||
76 | 6 | ||
77 | public function getAvailableValues() |
||
81 | |||
82 | /** |
||
83 | * @return bool |
||
84 | 21 | */ |
|
85 | public function isMulti() |
||
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 | 18 | */ |
|
97 | public function isValid() |
||
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 | 36 | */ |
|
128 | public function getValue() |
||
129 | 36 | { |
|
130 | 15 | View Code Duplication | if ($this->type == self::TYPE_GET) { |
131 | 6 | if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) { |
|
132 | return $_GET[$this->key]; |
||
133 | 9 | } |
|
134 | if ($this->isMulti()) { |
||
135 | return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
||
136 | 9 | } |
|
137 | return filter_input(INPUT_GET, $this->key); |
||
138 | 30 | } |
|
139 | 12 | View Code Duplication | if ($this->type == self::TYPE_POST) { |
140 | 6 | if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) { |
|
141 | return $_POST[$this->key]; |
||
142 | 9 | } |
|
143 | if ($this->isMulti()) { |
||
144 | return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
||
145 | 9 | } |
|
146 | return filter_input(INPUT_POST, $this->key); |
||
147 | 18 | } |
|
148 | 9 | if ($this->type == self::TYPE_FILE) { |
|
149 | 6 | if (isset($_FILES[$this->key])) { |
|
150 | 3 | if ($this->isMulti()) { |
|
151 | return $this->processMultiFileUploads($_FILES[$this->key]); |
||
152 | 3 | } else { |
|
153 | return $_FILES[$this->key]; |
||
154 | } |
||
155 | 3 | } |
|
156 | return null; |
||
157 | 9 | } |
|
158 | 3 | if ($this->type == self::TYPE_COOKIE) { |
|
159 | 3 | if (isset($_COOKIE[$this->key])) { |
|
160 | return $_COOKIE[$this->key]; |
||
161 | } |
||
162 | 6 | } |
|
163 | 3 | if ($this->type == self::TYPE_POST_RAW) { |
|
164 | return file_get_contents("php://input"); |
||
165 | } |
||
166 | 3 | if ($this->type == self::TYPE_PUT) { |
|
167 | parse_str(file_get_contents("php://input"), $params); |
||
168 | if (isset($params[$this->key])) { |
||
169 | 3 | return $params[$this->key]; |
|
170 | } |
||
171 | 3 | return ''; |
|
172 | 3 | } |
|
173 | 3 | ||
174 | 3 | throw new Exception('Invalid type'); |
|
175 | 2 | } |
|
176 | 2 | ||
177 | 3 | private function processMultiFileUploads($files) |
|
187 | } |
||
188 |
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: