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 |
||
| 9 | class BlogController implements \Anax\DI\IInjectionAware |
||
| 10 | {
|
||
| 11 | use \Anax\DI\TInjectable, |
||
| 12 | \Anax\MVC\TRedirectHelpers; |
||
| 13 | |||
| 14 | public $blog; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Initialize the controller. |
||
| 18 | * |
||
| 19 | * @return void |
||
| 20 | */ |
||
| 21 | public function initialize() |
||
| 26 | |||
| 27 | public function setupAction() |
||
| 33 | |||
| 34 | public function indexAction() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * List all blogposts. |
||
| 47 | * |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function listAction() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * View blogpost with slug. |
||
| 63 | * |
||
| 64 | * @param int $slug of post to display |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function viewAction($slug = null) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Add new blogpost. |
||
| 86 | * |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function addAction() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Edit a blogpost. |
||
| 108 | * |
||
| 109 | * @param string $id of post to edit. |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function updateAction($id = null) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Delete blogpost. |
||
| 137 | * |
||
| 138 | * @param integer $id of post to delete. |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function deleteAction($id = null) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Delete (soft) blogpost. |
||
| 155 | * |
||
| 156 | * @param integer $id of post to delete. |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function softDeleteAction($id = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Restore (soft) deleted blogpost. |
||
| 178 | * |
||
| 179 | * @param integer $id of post to restore. |
||
| 180 | * |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function restoreAction($id = null) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Activate blogpost. |
||
| 199 | * |
||
| 200 | * @param integer $id of upost to activate. |
||
| 201 | * |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | public function activateAction($id = null) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Inactivate blogpost. |
||
| 220 | * |
||
| 221 | * @param integer $id of post to inactivate. |
||
| 222 | * |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function inactivateAction($id = null) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * List all active and not deleted blogposts. |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | View Code Duplication | public function activeAction() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * List all inactive blogposts. |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | View Code Duplication | public function inactiveAction() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * List all deleted blogposts. |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function trashAction() |
|
| 295 | |||
| 296 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.