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 |
||
20 | class CommentController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * @param Request $request |
||
24 | * @param $page |
||
25 | * @Method({"GET", "POST"}) |
||
26 | * @Route("/comments/{pager}/{page}", name="commentsAdmin", |
||
27 | * defaults={"pager": "page", "page": 1}, |
||
28 | * requirements={ |
||
29 | * "pager": "page", |
||
30 | * "page": "[1-9]\d*", |
||
31 | * }) |
||
32 | * @Template("AppBundle:admin:comments.html.twig") |
||
33 | * |
||
34 | * @return Response |
||
35 | */ |
||
36 | 1 | public function roleAction(Request $request, $page = 1) |
|
37 | { |
||
38 | 1 | $em = $this->getDoctrine()->getManager(); |
|
39 | 1 | $comments = $em->getRepository("AppBundle:Comment") |
|
40 | 1 | ->getRecentComments($page, 10); |
|
41 | |||
42 | 1 | $formData = $comments->getQuery()->getResult(); |
|
43 | |||
44 | 1 | $form = $this->createFormBuilder($formData) |
|
|
|||
45 | 1 | ->setAction($this->generateUrl('commentsAdmin')) |
|
46 | 1 | ->setMethod('POST') |
|
47 | 1 | ->add('comments', ChoiceType::class, array( |
|
48 | 1 | 'choices' => $formData, |
|
49 | 1 | 'choices_as_values' => true, |
|
50 | 1 | 'expanded' => true, |
|
51 | 1 | 'multiple' => true, |
|
52 | 1 | 'choice_value' => 'id', |
|
53 | 1 | 'label' => false, |
|
54 | 1 | 'choice_label' => 'id', |
|
55 | ) |
||
56 | 1 | ) |
|
57 | 1 | ->add('delete', SubmitType::class, array( |
|
58 | 1 | 'label' => 'Delete', |
|
59 | 'attr' => [ |
||
60 | 'class' => 'btn btn-xs btn-danger' |
||
61 | 1 | ], |
|
62 | ) |
||
63 | 1 | ) |
|
64 | 1 | ->getForm(); |
|
65 | |||
66 | 1 | if ($request->getMethod() == 'POST') { |
|
67 | $form->handleRequest($request); |
||
68 | if ($form->isValid()) { |
||
69 | $data = $form->getData(); |
||
70 | foreach ($data['comments'] as $comment) { |
||
71 | $em->remove($comment); |
||
72 | } |
||
73 | |||
74 | try { |
||
75 | $em->flush(); |
||
76 | } catch (\Exception $e) { |
||
77 | return $this->render( |
||
78 | 'AppBundle:admin:failure.html.twig', |
||
79 | array( |
||
80 | 'message' => 'Deleting record is failed. Because record has relation to other records or another reasons.', |
||
81 | ) |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | return $this->redirectToRoute('commentsAdmin'); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return [ |
||
90 | 1 | 'comments' => $comments, |
|
91 | 1 | 'delete' => $form->createView(), |
|
92 | 1 | ]; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param $id |
||
97 | * @param $action |
||
98 | * @param Request $request |
||
99 | * @Route("/comment/{action}/{id}", name="commentEdit", |
||
100 | * defaults={"id": 0}, |
||
101 | * requirements={ |
||
102 | * "action": "edit", |
||
103 | * "id": "\d+" |
||
104 | * }) |
||
105 | * @Method({"GET", "POST"}) |
||
106 | * @Template("AppBundle:admin/form:comment.html.twig") |
||
107 | * |
||
108 | * @return Response |
||
109 | */ |
||
110 | public function editCommentAction($id, $action, Request $request) |
||
139 | |||
140 | } |
||
141 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: