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:
Complex classes like BaseApiController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseApiController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class BaseApiController extends Controller |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The errorHandler implementation. |
||
| 14 | * |
||
| 15 | * @var errorHandler |
||
| 16 | */ |
||
| 17 | protected $errorHandler; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The config implementation. |
||
| 21 | * |
||
| 22 | * @var config |
||
| 23 | */ |
||
| 24 | protected $config; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The model implementation. |
||
| 28 | * |
||
| 29 | * @var model |
||
| 30 | */ |
||
| 31 | protected $model; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create new BaseApiController instance. |
||
| 35 | * |
||
| 36 | * @param errorHandler |
||
| 37 | */ |
||
| 38 | public function __construct(ErrorHandler $errorHandler, CoreConfig $config) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Fetch all records with relations from model repository. |
||
| 57 | * |
||
| 58 | * @param string $sortBy |
||
| 59 | * @param boolean $desc |
||
| 60 | * @return \Illuminate\Http\Response |
||
| 61 | */ |
||
| 62 | public function getIndex() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Fetch the single object with relations from model repository. |
||
| 73 | * |
||
| 74 | * @param integer $id |
||
| 75 | * @return \Illuminate\Http\Response |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function getFind($id) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Paginate all records with relations from model repository |
||
| 88 | * that matche the given query. |
||
| 89 | * |
||
| 90 | * @param string $query |
||
| 91 | * @param integer $perPage |
||
| 92 | * @param string $sortBy |
||
| 93 | * @param boolean $desc |
||
| 94 | * @return \Illuminate\Http\Response |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Fetch records from the storage based on the given |
||
| 107 | * condition. |
||
| 108 | * |
||
| 109 | * @param \Illuminate\Http\Request $request |
||
| 110 | * @param string $sortBy |
||
| 111 | * @param boolean $desc |
||
| 112 | * @return \Illuminate\Http\Response |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Fetch the first record from the storage based on the given |
||
| 125 | * condition. |
||
| 126 | * |
||
| 127 | * @param \Illuminate\Http\Request $request |
||
| 128 | * @return \Illuminate\Http\Response |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function postFirst(Request $request) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Paginate all records with relations from model repository. |
||
| 141 | * |
||
| 142 | * @param integer $perPage |
||
| 143 | * @param string $sortBy |
||
| 144 | * @param boolean $desc |
||
| 145 | * @return \Illuminate\Http\Response |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Fetch all records with relations based on |
||
| 158 | * the given condition from storage in pages. |
||
| 159 | * |
||
| 160 | * @param \Illuminate\Http\Request $request |
||
| 161 | * @param integer $perPage |
||
| 162 | * @param string $sortBy |
||
| 163 | * @param boolean $desc |
||
| 164 | * @return \Illuminate\Http\Response |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Save the given model to repository. |
||
| 177 | * |
||
| 178 | * @param \Illuminate\Http\Request $request |
||
| 179 | * @return \Illuminate\Http\Response |
||
| 180 | */ |
||
| 181 | public function postSave(Request $request) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Delete by the given id from model repository. |
||
| 210 | * |
||
| 211 | * @param integer $id |
||
| 212 | * @return \Illuminate\Http\Response |
||
| 213 | */ |
||
| 214 | public function getDelete($id) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if the logged in user can do the given permission. |
||
| 224 | * |
||
| 225 | * @param string $permission |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | private function checkPermission($permission) |
||
| 246 | } |
||
| 247 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..