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 |
||
| 7 | class BaseApiController extends Controller |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The model implementation. |
||
| 11 | * |
||
| 12 | * @var model |
||
| 13 | */ |
||
| 14 | protected $model; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The config implementation. |
||
| 18 | * |
||
| 19 | * @var config |
||
| 20 | */ |
||
| 21 | protected $config; |
||
| 22 | |||
| 23 | public function __construct() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Fetch all records with relations from model repository. |
||
| 39 | * |
||
| 40 | * @return \Illuminate\Http\Response |
||
| 41 | */ |
||
| 42 | public function index() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Fetch the single object with relations from model repository. |
||
| 53 | * |
||
| 54 | * @param integer $id |
||
| 55 | * @return \Illuminate\Http\Response |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function find($id) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Paginate all records with relations from model repository |
||
| 68 | * that matche the given query. |
||
| 69 | * |
||
| 70 | * @param string $query |
||
| 71 | * @param integer $perPage |
||
| 72 | * @param string $sortBy |
||
| 73 | * @param boolean $desc |
||
| 74 | * @return \Illuminate\Http\Response |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Fetch records from the storage based on the given |
||
| 87 | * condition. |
||
| 88 | * |
||
| 89 | * @param \Illuminate\Http\Request $request |
||
| 90 | * @param string $sortBy |
||
| 91 | * @param boolean $desc |
||
| 92 | * @return \Illuminate\Http\Response |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Fetch the first record from the storage based on the given |
||
| 105 | * condition. |
||
| 106 | * |
||
| 107 | * @param \Illuminate\Http\Request $request |
||
| 108 | * @return \Illuminate\Http\Response |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function first(Request $request) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Paginate all records with relations from model repository. |
||
| 121 | * |
||
| 122 | * @param integer $perPage |
||
| 123 | * @param string $sortBy |
||
| 124 | * @param boolean $desc |
||
| 125 | * @return \Illuminate\Http\Response |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Fetch all records with relations based on |
||
| 138 | * the given condition from storage in pages. |
||
| 139 | * |
||
| 140 | * @param \Illuminate\Http\Request $request |
||
| 141 | * @param integer $perPage |
||
| 142 | * @param string $sortBy |
||
| 143 | * @param boolean $desc |
||
| 144 | * @return \Illuminate\Http\Response |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Save the given model to repository. |
||
| 157 | * |
||
| 158 | * @param \Illuminate\Http\Request $request |
||
| 159 | * @return \Illuminate\Http\Response |
||
| 160 | */ |
||
| 161 | public function save(Request $request) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Delete by the given id from model repository. |
||
| 190 | * |
||
| 191 | * @param integer $id |
||
| 192 | * @return \Illuminate\Http\Response |
||
| 193 | */ |
||
| 194 | public function delete($id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check if the logged in user can do the given permission. |
||
| 204 | * |
||
| 205 | * @param string $permission |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | private function checkPermission($permission) |
||
| 220 | } |
||
| 221 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.