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 |
||
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 | * Save transaction |
||
153 | * |
||
154 | * @param int $typeId |
||
155 | * @param string $text |
||
156 | * @param $userId |
||
157 | * @param $amount |
||
158 | */ |
||
159 | View Code Duplication | protected function _saveTransation($status_id = 1, $user_id, $amount, |
|
181 | |||
182 | /** |
||
183 | * Save media to storage |
||
184 | * |
||
185 | * @param object $object |
||
186 | * @param Request $request |
||
187 | * @param boolean $update |
||
188 | * @return boolean |
||
189 | */ |
||
190 | public function saveMediaToStorage($object, $request, $update = FALSE){ |
||
194 | |||
195 | /** |
||
196 | * Get number of pagination rows |
||
197 | * |
||
198 | * @return integer |
||
199 | */ |
||
200 | public function getRowsToPaginate() { |
||
211 | |||
212 | /** |
||
213 | * Associate relationships to other table |
||
214 | * |
||
215 | * @param object $object |
||
216 | * @param Request $request |
||
217 | */ |
||
218 | public function associateRelationships($object, Request $request){ |
||
221 | |||
222 | /** |
||
223 | * Associate relationships to other table, where ID if object must be present |
||
224 | * |
||
225 | * @param object $object |
||
226 | * @param Request $request |
||
227 | */ |
||
228 | public function associateRelationshipsWithID($object, Request $request){ |
||
231 | |||
232 | /** |
||
233 | * Reset cache |
||
234 | * |
||
235 | * @param object $object |
||
236 | */ |
||
237 | public function resetCache($object){ |
||
240 | |||
241 | /** |
||
242 | * Change ar result if necessary |
||
243 | * |
||
244 | * @param $arResult |
||
245 | * @return mixed |
||
246 | */ |
||
247 | public function changeEditResultField($arResult){ |
||
250 | |||
251 | /** |
||
252 | * Display a listing of the resource |
||
253 | * |
||
254 | * @param Request $request |
||
255 | * @return Response |
||
256 | */ |
||
257 | public function index(Request $request) { |
||
294 | |||
295 | /** |
||
296 | * Show the form for creating a new resource. |
||
297 | * |
||
298 | * @return Response |
||
299 | */ |
||
300 | public function create() { |
||
317 | |||
318 | /** |
||
319 | * Store a newly created resource in storage. |
||
320 | * |
||
321 | * @param Request $request |
||
322 | * @return Response |
||
323 | */ |
||
324 | public function store(Request $request) { |
||
399 | |||
400 | /** |
||
401 | * Show the form for editing the specified resource. |
||
402 | * |
||
403 | * @param int $id |
||
404 | * @return Response |
||
405 | */ |
||
406 | public function edit($id) { |
||
446 | |||
447 | /** |
||
448 | * Display the specified resource. |
||
449 | * |
||
450 | * @param int $id |
||
451 | * @return Response |
||
452 | */ |
||
453 | public function show($id) { |
||
456 | |||
457 | /** |
||
458 | * Update the specified resource in storage. |
||
459 | * |
||
460 | * @param Request $request |
||
461 | * @param int $id |
||
462 | * @return Response |
||
463 | */ |
||
464 | public function update(Request $request, $id) { |
||
589 | |||
590 | /** |
||
591 | * Remove the specified resource from storage. |
||
592 | * |
||
593 | * @param int $id |
||
594 | * @return Response |
||
595 | */ |
||
596 | |||
597 | public function destroy($id) { |
||
609 | |||
610 | } |
||
611 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.