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 |
||
49 | class MembersController extends BaseController |
||
50 | { |
||
51 | protected $members; |
||
52 | protected $fees; |
||
53 | |||
54 | public function __construct(MembersRepositoryInterface $members, FeesRepositoryInterface $fees) |
||
59 | |||
60 | /** |
||
61 | * Display a listing of members |
||
62 | * |
||
63 | * @param Request $request |
||
64 | * @param MembersPaginator $paginator |
||
65 | * @return View |
||
66 | */ |
||
67 | public function index(Request $request, MembersPaginator $paginator) |
||
77 | |||
78 | /** |
||
79 | * Returns the list of members to be used for autocompletion |
||
80 | * |
||
81 | * @param UrlGenerator $url |
||
82 | * @return JsonResponse |
||
83 | */ |
||
84 | public function prefetch(UrlGenerator $url) |
||
100 | |||
101 | /** |
||
102 | * Show a page that lists the board members |
||
103 | * |
||
104 | * @return View |
||
105 | */ |
||
106 | public function board() |
||
112 | |||
113 | /** |
||
114 | * Show a page with the unapproved member accounts |
||
115 | * |
||
116 | * @return View |
||
117 | */ |
||
118 | public function unapproved() |
||
124 | |||
125 | /** |
||
126 | * Show the form for creating a new member |
||
127 | * |
||
128 | * @param FacultiesRepositoryInterface $faculties |
||
129 | * @return View |
||
130 | */ |
||
131 | public function create(FacultiesRepositoryInterface $faculties) |
||
137 | |||
138 | /** |
||
139 | * Store a newly created member in storage. |
||
140 | * |
||
141 | * @param StoreMemberRequest $request |
||
142 | * @return RedirectResponse |
||
143 | */ |
||
144 | View Code Duplication | public function store(StoreMemberRequest $request) |
|
154 | |||
155 | /** |
||
156 | * Display the specified member. |
||
157 | * |
||
158 | * @param MeetingsService $meetingsService |
||
159 | * @param int $id |
||
160 | * @return View |
||
161 | * |
||
162 | * @todo Information separated in tabs (in the view) should be separated in few methods |
||
163 | */ |
||
164 | public function show(MeetingsService $meetingsService, $id) |
||
176 | |||
177 | /** |
||
178 | * Returns html component with short member info |
||
179 | * (focused on the membership) |
||
180 | * |
||
181 | * @param int $id |
||
182 | * @return View |
||
183 | */ |
||
184 | public function quickMemberInfo($id) |
||
190 | |||
191 | /** |
||
192 | * Show the form for editing the specified member. |
||
193 | * |
||
194 | * @param int $id |
||
195 | * @param FacultiesRepositoryInterface $faculties |
||
196 | * @return View |
||
197 | */ |
||
198 | public function edit($id, FacultiesRepositoryInterface $faculties) |
||
205 | |||
206 | /** |
||
207 | * Update the specified member in storage. |
||
208 | * |
||
209 | * @param UpdateMemberRequest $request |
||
210 | * @param int $id |
||
211 | * @return RedirectResponse |
||
212 | */ |
||
213 | View Code Duplication | public function update(UpdateMemberRequest $request, $id) |
|
223 | |||
224 | /** |
||
225 | * Remove the specified members from storage. |
||
226 | * Method available only via AJAX requests |
||
227 | * |
||
228 | * @param int $id |
||
229 | * @return JsonResponse |
||
230 | */ |
||
231 | public function destroy($id) |
||
237 | |||
238 | /** |
||
239 | * Approve a pending member account |
||
240 | * Method available only via AJAX requests |
||
241 | * |
||
242 | * @param int $id |
||
243 | * @return JsonResponse |
||
244 | */ |
||
245 | public function approve($id) |
||
251 | |||
252 | /** |
||
253 | * Decline a pending member account |
||
254 | * Method available only via AJAX requests |
||
255 | * |
||
256 | * @param int $id |
||
257 | * @return JsonResponse |
||
258 | */ |
||
259 | public function decline($id) |
||
265 | |||
266 | /** |
||
267 | * The new members can create their profiles on the system |
||
268 | * |
||
269 | * @param FacultiesRepositoryInterface $faculties |
||
270 | * @return View |
||
271 | */ |
||
272 | public function register(FacultiesRepositoryInterface $faculties) |
||
278 | |||
279 | /** |
||
280 | * Proceed the information submitted via the registration form |
||
281 | * |
||
282 | * @param StoreMemberRequest $request |
||
283 | * @return RedirectResponse |
||
284 | */ |
||
285 | View Code Duplication | public function postRegister(StoreMemberRequest $request) |
|
298 | } |
||
299 |
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.