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 | |||
| 44 | 48 | public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 9 | public function getType() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 21 | public function getKey() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @return boolean |
||
| 71 | */ |
||
| 72 | 6 | public function isRequired() |
|
| 76 | |||
| 77 | 6 | public function getAvailableValues() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | 21 | 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 | */ |
||
| 97 | 18 | 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 | */ |
||
| 128 | 39 | public function getValue() |
|
| 176 | |||
| 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: