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 AdminModuleController 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 AdminModuleController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class AdminModuleController extends Controller{ |
||
29 | |||
30 | /** |
||
31 | * Module name |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $moduleName; |
||
36 | |||
37 | /** |
||
38 | * Module basic path |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $moduleBasicRoute; |
||
43 | |||
44 | /** |
||
45 | * View basic path |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $moduleBasicTemplatePath; |
||
50 | |||
51 | /** |
||
52 | * Model Class |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $modelClass; |
||
57 | |||
58 | /** |
||
59 | * Rows to paginate |
||
60 | * |
||
61 | * @var integer |
||
62 | */ |
||
63 | protected $paginateRows = NULL; |
||
64 | |||
65 | /** |
||
66 | * View variables to inject |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $viewVariables = array(); |
||
71 | |||
72 | /** |
||
73 | * Hidden fields for action |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $hiddenFieldsOnAction = array(); |
||
78 | |||
79 | /** |
||
80 | * Binary fields to exclude from update |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $binaryFields = array(); |
||
85 | |||
86 | /** |
||
87 | * Binary fields to exclude from update |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $dateTimeLocalFields = array(); |
||
92 | |||
93 | /** |
||
94 | * Custom view |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $customView = NULL; |
||
99 | |||
100 | /** |
||
101 | * Constructor |
||
102 | */ |
||
103 | public function __construct() { |
||
150 | |||
151 | /** |
||
152 | * Runs after store |
||
153 | * |
||
154 | * @param $request |
||
155 | */ |
||
156 | protected function afterStore($request){ |
||
159 | |||
160 | /** |
||
161 | * Runs after update |
||
162 | * |
||
163 | * @param $request |
||
164 | */ |
||
165 | protected function afterUpdate($request){ |
||
168 | |||
169 | /** |
||
170 | * Save transaction |
||
171 | * |
||
172 | * @param int $typeId |
||
173 | * @param string $text |
||
174 | * @param $userId |
||
175 | * @param $amount |
||
176 | */ |
||
177 | View Code Duplication | protected function _saveTransation($status_id = 1, $user_id, $amount, |
|
199 | |||
200 | /** |
||
201 | * Save media to storage |
||
202 | * |
||
203 | * @param object $object |
||
204 | * @param Request $request |
||
205 | * @param boolean $update |
||
206 | * @return boolean |
||
207 | */ |
||
208 | public function saveMediaToStorage($object, $request, $update = FALSE){ |
||
212 | |||
213 | /** |
||
214 | * Get number of pagination rows |
||
215 | * |
||
216 | * @return integer |
||
217 | */ |
||
218 | public function getRowsToPaginate() { |
||
229 | |||
230 | /** |
||
231 | * Associate relationships to other table |
||
232 | * |
||
233 | * @param object $object |
||
234 | * @param Request $request |
||
235 | */ |
||
236 | public function associateRelationships($object, Request $request){ |
||
239 | |||
240 | /** |
||
241 | * Associate relationships to other table, where ID if object must be present |
||
242 | * |
||
243 | * @param object $object |
||
244 | * @param Request $request |
||
245 | */ |
||
246 | public function associateRelationshipsWithID($object, Request $request){ |
||
249 | |||
250 | /** |
||
251 | * Reset cache |
||
252 | * |
||
253 | * @param object $object |
||
254 | */ |
||
255 | public function resetCache($object){ |
||
258 | |||
259 | /** |
||
260 | * Change ar result if necessary |
||
261 | * |
||
262 | * @param $arResult |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function changeEditResultField($arResult){ |
||
268 | |||
269 | /** |
||
270 | * Display a listing of the resource |
||
271 | * |
||
272 | * @param Request $request |
||
273 | * @return Response |
||
274 | */ |
||
275 | public function index(Request $request) { |
||
323 | |||
324 | /** |
||
325 | * Show the form for creating a new resource. |
||
326 | * |
||
327 | * @return Response |
||
328 | */ |
||
329 | public function create() { |
||
346 | |||
347 | /** |
||
348 | * Store a newly created resource in storage. |
||
349 | * |
||
350 | * @param Request $request |
||
351 | * @return Response |
||
352 | */ |
||
353 | public function store(Request $request) { |
||
438 | |||
439 | /** |
||
440 | * Show the form for editing the specified resource. |
||
441 | * |
||
442 | * @param int $id |
||
443 | * @return Response |
||
444 | */ |
||
445 | public function edit($id) { |
||
485 | |||
486 | /** |
||
487 | * Display the specified resource. |
||
488 | * |
||
489 | * @param int $id |
||
490 | * @return Response |
||
491 | */ |
||
492 | public function show($id) { |
||
495 | |||
496 | /** |
||
497 | * Update the specified resource in storage. |
||
498 | * |
||
499 | * @param Request $request |
||
500 | * @param int $id |
||
501 | * @return Response |
||
502 | */ |
||
503 | public function update(Request $request, $id) { |
||
638 | |||
639 | /** |
||
640 | * Remove the specified resource from storage. |
||
641 | * |
||
642 | * @param int $id |
||
643 | * @return Response |
||
644 | */ |
||
645 | |||
646 | public function destroy($id) { |
||
658 | |||
659 | } |
||
660 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.