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 |
||
| 13 | class SessionManager |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Start session handler |
||
| 17 | * @return void |
||
| 18 | * @codeCoverageIgnore |
||
| 19 | */ |
||
| 20 | public function startSession() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Destroy session at all |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | public function destroySession() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check if session exists |
||
| 40 | * @return boolean |
||
| 41 | */ |
||
| 42 | public function hasSession() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set session key with value |
||
| 53 | * @param string $key |
||
| 54 | * @param string|array $value |
||
| 55 | */ |
||
| 56 | public function set(string $key, $value) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get session value by key |
||
| 63 | * @param string $key |
||
| 64 | * @return null|string|array |
||
| 65 | */ |
||
| 66 | public function get(string $key) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Delete session value by key |
||
| 77 | * @param string $key |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | public function delete(string $key) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Check if flashbag key exists |
||
| 93 | * @param string $key |
||
| 94 | * @return boolean |
||
| 95 | */ |
||
| 96 | public function hasFlashbagKey(string $key) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Check if flashbag error key exists |
||
| 103 | * @param string $key |
||
| 104 | * @return boolean |
||
| 105 | */ |
||
| 106 | public function hasFlashbagErrorsKey($key) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set flashbag value by key |
||
| 113 | * @param string|array $key |
||
| 114 | * @param null|string|array $value |
||
| 115 | * @return boolean |
||
| 116 | */ |
||
| 117 | public function setFlashbag($key, $value = null) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get flashbag value by key |
||
| 136 | * |
||
| 137 | * @param string $key |
||
| 138 | * @return null|string|array |
||
| 139 | */ |
||
| 140 | public function getFlashbag(string $key) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get flashbag error by key |
||
| 155 | * |
||
| 156 | * @param string $key |
||
| 157 | * @return null|string|array |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function getFlashbagError(string $key) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Set form data in session flashbag |
||
| 174 | * @param array $formData |
||
| 175 | */ |
||
| 176 | public function setFlashbagFormData(array $formData) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get form data from session flashbag |
||
| 185 | * @param string $key |
||
| 186 | * @return null|array|string |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function getFlashbagFormData(string $key) |
|
| 200 | |||
| 201 | } |
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: