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 | |||
| 14 | const OPTIONAL = false; |
||
| 15 | const REQUIRED = true; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private $type; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $key; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $required; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array|null |
||
| 34 | */ |
||
| 35 | private $availableValues; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | private $multi; |
||
| 41 | |||
| 42 | 39 | public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 9 | public function getType() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 18 | public function getKey() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return boolean |
||
| 69 | */ |
||
| 70 | 6 | public function isRequired() |
|
| 74 | |||
| 75 | 6 | public function getAvailableValues() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | 18 | public function isMulti() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Check if actual value from environment is valid |
||
| 90 | * |
||
| 91 | * @return bool |
||
| 92 | * |
||
| 93 | * @throws Exception if actual InputParam has unsupported type |
||
| 94 | */ |
||
| 95 | 15 | public function isValid() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Process environment variables like POST|GET|etc.. and return actual value |
||
| 117 | * |
||
| 118 | * @return mixed |
||
| 119 | * |
||
| 120 | * @throws Exception if actual InputParam has unsupported type |
||
| 121 | */ |
||
| 122 | 30 | public function getValue() |
|
| 160 | |||
| 161 | 3 | private function processMultiFileUploads($files) |
|
| 171 | } |
||
| 172 |
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: