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_FILE = 'FILE'; |
||
12 | const TYPE_COOKIE = 'COOKIE'; |
||
13 | const TYPE_POST_RAW = 'POST_RAW'; |
||
14 | |||
15 | const OPTIONAL = false; |
||
16 | const REQUIRED = true; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $type; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $key; |
||
23 | |||
24 | /** @var bool */ |
||
25 | private $required; |
||
26 | |||
27 | /** @var array|null */ |
||
28 | private $availableValues; |
||
29 | |||
30 | /** @var bool */ |
||
31 | private $multi; |
||
32 | |||
33 | /** @var string */ |
||
34 | private $description; |
||
35 | |||
36 | 45 | public function __construct( |
|
37 | $type, |
||
38 | $key, |
||
39 | $required = self::OPTIONAL, |
||
40 | $availableValues = null, |
||
41 | $multi = false, |
||
42 | $description = '' |
||
43 | ) { |
||
44 | 45 | $this->type = $type; |
|
45 | 45 | $this->key = $key; |
|
46 | 45 | $this->required = $required; |
|
47 | 45 | $this->availableValues = $availableValues; |
|
48 | 45 | $this->multi = $multi; |
|
49 | 45 | $this->description = $description; |
|
50 | 45 | } |
|
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | 9 | public function getType() |
|
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | 21 | public function getKey() |
|
67 | |||
68 | /** |
||
69 | * @return boolean |
||
70 | */ |
||
71 | 6 | public function isRequired() |
|
75 | |||
76 | 6 | public function getAvailableValues() |
|
80 | |||
81 | /** |
||
82 | * @return bool |
||
83 | */ |
||
84 | 21 | public function isMulti() |
|
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getDescription() |
||
96 | |||
97 | /** |
||
98 | * Check if actual value from environment is valid |
||
99 | * |
||
100 | * @return bool |
||
101 | * |
||
102 | * @throws Exception if actual InputParam has unsupported type |
||
103 | */ |
||
104 | 18 | public function isValid() |
|
123 | |||
124 | /** |
||
125 | * Process environment variables like POST|GET|etc.. and return actual value |
||
126 | * |
||
127 | * @return mixed |
||
128 | * |
||
129 | * @throws Exception if actual InputParam has unsupported type |
||
130 | */ |
||
131 | 36 | public function getValue() |
|
172 | |||
173 | 3 | private function processMultiFileUploads($files) |
|
183 | } |
||
184 |
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: