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 | trait ModelSoftDeleting |
||
| 14 | { |
||
| 15 | use ModelDeleting; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Overrides the ModelQuerying provided method |
||
| 19 | * with one which searches withTrashed scope. |
||
| 20 | * |
||
| 21 | * @param int $modelitemId |
||
| 22 | * |
||
| 23 | * @return |
||
| 24 | */ |
||
| 25 | public function find($modelitemId) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Finds model and includes withTrashed() scope in query. |
||
| 32 | * |
||
| 33 | * @param int $modelitemId |
||
| 34 | * |
||
| 35 | * @return |
||
| 36 | */ |
||
| 37 | public function findWithTrashed($modelitemId) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Finds model and includes onlyTrashed() scope in query. |
||
| 46 | * |
||
| 47 | * @param int $modelitemId |
||
| 48 | * |
||
| 49 | * @return |
||
| 50 | */ |
||
| 51 | public function findOnlyTrashed($modelitemId) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Method fired before the Delete action is undertaken. |
||
| 60 | * |
||
| 61 | * @return |
||
| 62 | */ |
||
| 63 | protected function beforeDelete() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Delete Action. |
||
| 69 | * |
||
| 70 | * Fires off beforeDelete(), doDelete() and afterDelete() |
||
| 71 | * |
||
| 72 | * @param int $modelitemId |
||
| 73 | * |
||
| 74 | * @return |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function delete($modelitemId) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * The actual delete action. |
||
| 93 | * |
||
| 94 | * @return |
||
| 95 | */ |
||
| 96 | private function doDelete() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Method fired after the Delete action is complete. |
||
| 111 | * |
||
| 112 | * @return |
||
| 113 | */ |
||
| 114 | protected function afterDelete() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Method fired before the Restore action is undertaken. |
||
| 120 | * |
||
| 121 | * @return |
||
| 122 | */ |
||
| 123 | protected function beforeRestore() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Restore Action. |
||
| 129 | * |
||
| 130 | * Fires off beforeRestore(), doRestore() and afterRestore() |
||
| 131 | * |
||
| 132 | * @param int $modelitemId |
||
| 133 | * |
||
| 134 | * @return |
||
| 135 | */ |
||
| 136 | public function restore($modelitemId) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The actual restore action. |
||
| 153 | * |
||
| 154 | * @return |
||
| 155 | */ |
||
| 156 | private function doRestore() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Method fired after the Restore action is complete. |
||
| 165 | * |
||
| 166 | * @return |
||
| 167 | */ |
||
| 168 | protected function afterRestore() |
||
| 171 | } |
||
| 172 |