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 config implementation. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $config; |
||
15 | |||
16 | /** |
||
17 | * The relations implementation. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $relations; |
||
22 | |||
23 | /** |
||
24 | * The repo implementation. |
||
25 | * |
||
26 | * @var object |
||
27 | */ |
||
28 | protected $repo; |
||
29 | |||
30 | public function __construct() |
||
44 | |||
45 | /** |
||
46 | * Fetch all records with relations from storage. |
||
47 | * |
||
48 | * @param string $sortBy The name of the column to sort by. |
||
49 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
50 | * @return \Illuminate\Http\Response |
||
51 | */ |
||
52 | View Code Duplication | public function index($sortBy = 'created_at', $desc = 1) |
|
59 | |||
60 | /** |
||
61 | * Fetch the single object with relations from storage. |
||
62 | * |
||
63 | * @param integer $id Id of the requested model. |
||
64 | * @return \Illuminate\Http\Response |
||
65 | */ |
||
66 | public function find($id) |
||
73 | |||
74 | /** |
||
75 | * Paginate all records with relations from storage |
||
76 | * that matche the given query. |
||
77 | * |
||
78 | * @param string $query The search text. |
||
79 | * @param integer $perPage Number of rows per page default 15. |
||
80 | * @param string $sortBy The name of the column to sort by. |
||
81 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
82 | * @return \Illuminate\Http\Response |
||
83 | */ |
||
84 | public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
||
91 | |||
92 | /** |
||
93 | * Fetch records from the storage based on the given |
||
94 | * condition. |
||
95 | * |
||
96 | * @param \Illuminate\Http\Request $request |
||
97 | * @param string $sortBy The name of the column to sort by. |
||
98 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
99 | * @return \Illuminate\Http\Response |
||
100 | */ |
||
101 | View Code Duplication | public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
108 | |||
109 | /** |
||
110 | * Fetch the first record from the storage based on the given |
||
111 | * condition. |
||
112 | * |
||
113 | * @param \Illuminate\Http\Request $request |
||
114 | * @return \Illuminate\Http\Response |
||
115 | */ |
||
116 | public function first(Request $request) |
||
123 | |||
124 | /** |
||
125 | * Paginate all records with relations from storage. |
||
126 | * |
||
127 | * @param integer $perPage Number of rows per page default 15. |
||
128 | * @param string $sortBy The name of the column to sort by. |
||
129 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
130 | * @return \Illuminate\Http\Response |
||
131 | */ |
||
132 | View Code Duplication | public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
139 | |||
140 | /** |
||
141 | * Fetch all records with relations based on |
||
142 | * the given condition from storage in pages. |
||
143 | * |
||
144 | * @param \Illuminate\Http\Request $request |
||
145 | * @param integer $perPage Number of rows per page default 15. |
||
146 | * @param string $sortBy The name of the column to sort by. |
||
147 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
148 | * @return \Illuminate\Http\Response |
||
149 | */ |
||
150 | View Code Duplication | public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
157 | |||
158 | /** |
||
159 | * Save the given model to storage. |
||
160 | * |
||
161 | * @param \Illuminate\Http\Request $request |
||
162 | * @return \Illuminate\Http\Response |
||
163 | */ |
||
164 | View Code Duplication | public function save(Request $request) |
|
190 | |||
191 | /** |
||
192 | * Delete by the given id from storage. |
||
193 | * |
||
194 | * @param integer $id Id of the deleted model. |
||
195 | * @return \Illuminate\Http\Response |
||
196 | */ |
||
197 | public function delete($id) |
||
204 | |||
205 | /** |
||
206 | * Return the deleted models in pages based on the given conditions. |
||
207 | * |
||
208 | * @param \Illuminate\Http\Request $request |
||
209 | * @param integer $perPage Number of rows per page default 15. |
||
210 | * @param string $sortBy The name of the column to sort by. |
||
211 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
212 | * @return \Illuminate\Http\Response |
||
213 | */ |
||
214 | public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
||
218 | |||
219 | /** |
||
220 | * Restore the deleted model. |
||
221 | * |
||
222 | * @param integer $id Id of the restored model. |
||
223 | * @return \Illuminate\Http\Response |
||
224 | */ |
||
225 | public function restore($id) |
||
232 | |||
233 | /** |
||
234 | * Check if the logged in user can do the given permission. |
||
235 | * |
||
236 | * @param string $permission |
||
237 | * @return void |
||
238 | */ |
||
239 | private function checkPermission($permission) |
||
265 | |||
266 | /** |
||
267 | * Set sessions based on the given headers in the request. |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | private function setSessions() |
||
299 | |||
300 | /** |
||
301 | * Set relation based on the called api. |
||
302 | * |
||
303 | * @param string $route |
||
304 | * @return void |
||
305 | */ |
||
306 | private function setRelations($route) |
||
312 | } |
||
313 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.