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 EntryController 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 EntryController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class EntryController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * @param Request $request |
||
24 | * @param int $page |
||
25 | * |
||
26 | * @Route("/search/{page}", name="search", defaults={"page" = 1}) |
||
27 | * |
||
28 | * Default parameter for page is hardcoded (in duplication of the defaults from the Route) |
||
29 | * because this controller is also called inside the layout template without any page as argument |
||
30 | * |
||
31 | * @return \Symfony\Component\HttpFoundation\Response |
||
32 | */ |
||
33 | public function searchFormAction(Request $request, $page = 1, $currentRoute = null) |
||
53 | |||
54 | /** |
||
55 | * @param Request $request |
||
56 | * |
||
57 | * @Route("/new-entry", name="new_entry") |
||
58 | * |
||
59 | * @return \Symfony\Component\HttpFoundation\Response |
||
60 | */ |
||
61 | public function addEntryFormAction(Request $request) |
||
97 | |||
98 | /** |
||
99 | * @param Request $request |
||
100 | * |
||
101 | * @Route("/bookmarklet", name="bookmarklet") |
||
102 | * |
||
103 | * @return \Symfony\Component\HttpFoundation\Response |
||
104 | */ |
||
105 | public function addEntryViaBookmarkletAction(Request $request) |
||
123 | |||
124 | /** |
||
125 | * @Route("/new", name="new") |
||
126 | * |
||
127 | * @return \Symfony\Component\HttpFoundation\Response |
||
128 | */ |
||
129 | public function addEntryAction() |
||
133 | |||
134 | /** |
||
135 | * Edit an entry content. |
||
136 | * |
||
137 | * @param Request $request |
||
138 | * @param Entry $entry |
||
139 | * |
||
140 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") |
||
141 | * |
||
142 | * @return \Symfony\Component\HttpFoundation\Response |
||
143 | */ |
||
144 | View Code Duplication | public function editEntryAction(Request $request, Entry $entry) |
|
145 | { |
||
146 | $this->checkUserAction($entry); |
||
147 | |||
148 | $form = $this->createForm(EditEntryType::class, $entry); |
||
149 | |||
150 | $form->handleRequest($request); |
||
151 | |||
152 | if ($form->isSubmitted() && $form->isValid()) { |
||
153 | $em = $this->getDoctrine()->getManager(); |
||
154 | $em->persist($entry); |
||
155 | $em->flush(); |
||
156 | |||
157 | $this->get('session')->getFlashBag()->add( |
||
158 | 'notice', |
||
159 | 'flashes.entry.notice.entry_updated' |
||
160 | ); |
||
161 | |||
162 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
||
163 | } |
||
164 | |||
165 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ |
||
166 | 'form' => $form->createView(), |
||
167 | ]); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Shows all entries for current user. |
||
172 | * |
||
173 | * @param Request $request |
||
174 | * @param int $page |
||
175 | * |
||
176 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) |
||
177 | * |
||
178 | * @return \Symfony\Component\HttpFoundation\Response |
||
179 | */ |
||
180 | public function showAllAction(Request $request, $page) |
||
184 | |||
185 | /** |
||
186 | * Shows unread entries for current user. |
||
187 | * |
||
188 | * @param Request $request |
||
189 | * @param int $page |
||
190 | * |
||
191 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
||
192 | * |
||
193 | * @return \Symfony\Component\HttpFoundation\Response |
||
194 | */ |
||
195 | public function showUnreadAction(Request $request, $page) |
||
204 | |||
205 | /** |
||
206 | * Shows read entries for current user. |
||
207 | * |
||
208 | * @param Request $request |
||
209 | * @param int $page |
||
210 | * |
||
211 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
||
212 | * |
||
213 | * @return \Symfony\Component\HttpFoundation\Response |
||
214 | */ |
||
215 | public function showArchiveAction(Request $request, $page) |
||
219 | |||
220 | /** |
||
221 | * Shows starred entries for current user. |
||
222 | * |
||
223 | * @param Request $request |
||
224 | * @param int $page |
||
225 | * |
||
226 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) |
||
227 | * |
||
228 | * @return \Symfony\Component\HttpFoundation\Response |
||
229 | */ |
||
230 | public function showStarredAction(Request $request, $page) |
||
234 | |||
235 | /** |
||
236 | * Shows entry content. |
||
237 | * |
||
238 | * @param Entry $entry |
||
239 | * |
||
240 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
||
241 | * |
||
242 | * @return \Symfony\Component\HttpFoundation\Response |
||
243 | */ |
||
244 | public function viewAction(Entry $entry) |
||
253 | |||
254 | /** |
||
255 | * Reload an entry. |
||
256 | * Refetch content from the website and make it readable again. |
||
257 | * |
||
258 | * @param Entry $entry |
||
259 | * |
||
260 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") |
||
261 | * |
||
262 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
263 | */ |
||
264 | public function reloadAction(Entry $entry) |
||
288 | |||
289 | /** |
||
290 | * Changes read status for an entry. |
||
291 | * |
||
292 | * @param Request $request |
||
293 | * @param Entry $entry |
||
294 | * |
||
295 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
||
296 | * |
||
297 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
298 | */ |
||
299 | View Code Duplication | public function toggleArchiveAction(Request $request, Entry $entry) |
|
320 | |||
321 | /** |
||
322 | * Changes starred status for an entry. |
||
323 | * |
||
324 | * @param Request $request |
||
325 | * @param Entry $entry |
||
326 | * |
||
327 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
||
328 | * |
||
329 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
330 | */ |
||
331 | View Code Duplication | public function toggleStarAction(Request $request, Entry $entry) |
|
332 | { |
||
333 | $this->checkUserAction($entry); |
||
334 | |||
335 | $entry->toggleStar(); |
||
336 | $entry->updateStar($entry->isStarred()); |
||
337 | $this->getDoctrine()->getManager()->flush(); |
||
338 | |||
339 | $message = 'flashes.entry.notice.entry_unstarred'; |
||
340 | if ($entry->isStarred()) { |
||
341 | $message = 'flashes.entry.notice.entry_starred'; |
||
342 | } |
||
343 | |||
344 | $this->get('session')->getFlashBag()->add( |
||
345 | 'notice', |
||
346 | $message |
||
347 | ); |
||
348 | |||
349 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
||
350 | |||
351 | return $this->redirect($redirectUrl); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Deletes entry and redirect to the homepage or the last viewed page. |
||
356 | * |
||
357 | * @param Entry $entry |
||
358 | * |
||
359 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
||
360 | * |
||
361 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
362 | */ |
||
363 | public function deleteEntryAction(Request $request, Entry $entry) |
||
395 | |||
396 | /** |
||
397 | * Get public URL for entry (and generate it if necessary). |
||
398 | * |
||
399 | * @param Entry $entry |
||
400 | * |
||
401 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") |
||
402 | * |
||
403 | * @return \Symfony\Component\HttpFoundation\Response |
||
404 | */ |
||
405 | public function shareAction(Entry $entry) |
||
421 | |||
422 | /** |
||
423 | * Disable public sharing for an entry. |
||
424 | * |
||
425 | * @param Entry $entry |
||
426 | * |
||
427 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") |
||
428 | * |
||
429 | * @return \Symfony\Component\HttpFoundation\Response |
||
430 | */ |
||
431 | public function deleteShareAction(Entry $entry) |
||
445 | |||
446 | /** |
||
447 | * Ability to view a content publicly. |
||
448 | * |
||
449 | * @param Entry $entry |
||
450 | * |
||
451 | * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry") |
||
452 | * @Cache(maxage="25200", smaxage="25200", public=true) |
||
453 | * |
||
454 | * @return \Symfony\Component\HttpFoundation\Response |
||
455 | */ |
||
456 | public function shareEntryAction(Entry $entry) |
||
467 | |||
468 | /** |
||
469 | * Shows untagged articles for current user. |
||
470 | * |
||
471 | * @param Request $request |
||
472 | * @param int $page |
||
473 | * |
||
474 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) |
||
475 | * |
||
476 | * @return \Symfony\Component\HttpFoundation\Response |
||
477 | */ |
||
478 | public function showUntaggedEntriesAction(Request $request, $page) |
||
482 | |||
483 | /** |
||
484 | * Fetch content and update entry. |
||
485 | * In case it fails, $entry->getContent will return an error message. |
||
486 | * |
||
487 | * @param Entry $entry |
||
488 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded |
||
489 | */ |
||
490 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') |
||
515 | |||
516 | /** |
||
517 | * Global method to retrieve entries depending on the given type |
||
518 | * It returns the response to be send. |
||
519 | * |
||
520 | * @param string $type Entries type: unread, starred or archive |
||
521 | * @param Request $request |
||
522 | * @param int $page |
||
523 | * |
||
524 | * @return \Symfony\Component\HttpFoundation\Response |
||
525 | */ |
||
526 | private function showEntries($type, Request $request, $page) |
||
589 | |||
590 | /** |
||
591 | * Check if the logged user can manage the given entry. |
||
592 | * |
||
593 | * @param Entry $entry |
||
594 | */ |
||
595 | private function checkUserAction(Entry $entry) |
||
601 | |||
602 | /** |
||
603 | * Check for existing entry, if it exists, redirect to it with a message. |
||
604 | * |
||
605 | * @param Entry $entry |
||
606 | * |
||
607 | * @return Entry|bool |
||
608 | */ |
||
609 | private function checkIfEntryAlreadyExists(Entry $entry) |
||
613 | } |
||
614 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: