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 RoleController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * @param Request $request |
||
24 | * @param $page |
||
25 | * @Method({"GET", "POST"}) |
||
26 | * @Route("/roles/{pager}/{page}", name="rolesAdmin", |
||
27 | * defaults={"pager": "page", "page": 1}, |
||
28 | * requirements={ |
||
29 | * "pager": "page", |
||
30 | * "page": "[1-9]\d*", |
||
31 | * }) |
||
32 | * @Template("AppBundle:admin:roles.html.twig") |
||
33 | * |
||
34 | * @return Response |
||
35 | */ |
||
36 | 1 | public function roleAction(Request $request, $page = 1) |
|
46 | |||
47 | /** |
||
48 | * @param $id |
||
49 | * @param $action |
||
50 | * @param Request $request |
||
51 | * @Route("/role/{action}/{id}", name="roleEdit", |
||
52 | * defaults={"id": 0}, |
||
53 | * requirements={ |
||
54 | * "action": "new|edit", |
||
55 | * "id": "\d+" |
||
56 | * }) |
||
57 | * @Method({"GET", "POST"}) |
||
58 | * @Template("AppBundle:admin/form:role.html.twig") |
||
59 | * |
||
60 | * @return Response |
||
61 | */ |
||
62 | View Code Duplication | public function editRoleAction($id, $action, Request $request) |
|
63 | { |
||
64 | $em = $this->getDoctrine()->getManager(); |
||
65 | if ($action == "edit") { |
||
66 | $role = $em->getRepository('AppBundle:Role') |
||
67 | ->find($id); |
||
68 | $title = 'Edit role id: '.$id; |
||
69 | } |
||
70 | else { |
||
71 | $role = new Role(); |
||
72 | $title = 'Create new role'; |
||
73 | } |
||
74 | |||
75 | |||
76 | $form = $this->createForm(RoleType::class, $role, [ |
||
77 | 'em' => $em, |
||
78 | 'action' => $this->generateUrl('roleEdit', ['action' => $action, 'id' => $id]), |
||
79 | 'method' => Request::METHOD_POST, |
||
80 | ]) |
||
81 | ->add('save', SubmitType::class, array('label' => 'Save')); |
||
82 | |||
83 | if ($request->getMethod() == 'POST') { |
||
84 | $form->handleRequest($request); |
||
85 | if ($form->isValid()) { |
||
86 | $em->persist($role); |
||
87 | $em->flush(); |
||
88 | |||
89 | return $this->redirectToRoute('rolesAdmin'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return [ |
||
94 | 'title' => $title, |
||
95 | 'form' => $form->createView(), |
||
96 | ]; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param $id |
||
101 | * @param Request $request |
||
102 | * @Route("/role/delete/{id}", name="roleDelete", |
||
103 | * requirements={ |
||
104 | * "id": "\d+" |
||
105 | * }) |
||
106 | * @Method({"GET", "POST"}) |
||
107 | * @Template("AppBundle:admin/form:delete.html.twig") |
||
108 | * |
||
109 | * @return Response |
||
110 | */ |
||
111 | 1 | public function deleteRoleAction($id, Request $request) |
|
159 | } |
||
160 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.