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() |
|
| 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() |
|
| 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: