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 |
||
37 | class UserController extends AbstractCRUDController |
||
38 | { |
||
39 | /** |
||
40 | * @var UserManager |
||
41 | */ |
||
42 | protected $userManager; |
||
43 | |||
44 | /** |
||
45 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
46 | * @param TokenStorageInterface $tokenStorage |
||
47 | * @param ListingFactory $listingFactory |
||
48 | * @param ManagerRegistry $doctrine |
||
49 | * @param FormFactory $formFactory |
||
50 | * @param Paginator $paginator |
||
51 | * @param UrlGeneratorInterface $urlGenerator |
||
52 | * @param \Twig_Environment $twig |
||
53 | * @param UserManager $userManager |
||
54 | */ |
||
55 | View Code Duplication | public function __construct( |
|
|
|||
56 | AuthorizationCheckerInterface $authorizationChecker, |
||
57 | TokenStorageInterface $tokenStorage, |
||
58 | ListingFactory $listingFactory, |
||
59 | ManagerRegistry $doctrine, |
||
60 | FormFactory $formFactory, |
||
61 | Paginator $paginator, |
||
62 | UrlGeneratorInterface $urlGenerator, |
||
63 | \Twig_Environment $twig, |
||
64 | UserManager $userManager |
||
65 | ) { |
||
66 | $this->authorizationChecker = $authorizationChecker; |
||
67 | $this->tokenStorage = $tokenStorage; |
||
68 | $this->listingFactory = $listingFactory; |
||
69 | $this->doctrine = $doctrine; |
||
70 | $this->formFactory = $formFactory; |
||
71 | $this->paginator = $paginator; |
||
72 | $this->urlGenerator = $urlGenerator; |
||
73 | $this->twig = $twig; |
||
74 | $this->userManager = $userManager; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @Route("/", bind="user_list", method="GET") |
||
79 | * @param Request $request |
||
80 | * @return Response |
||
81 | */ |
||
82 | public function listAction(Request $request) |
||
86 | |||
87 | /** |
||
88 | * @Route("/create", bind="user_create") |
||
89 | * @param Request $request |
||
90 | * @return Response|RedirectResponse |
||
91 | */ |
||
92 | public function createAction(Request $request) |
||
96 | |||
97 | /** |
||
98 | * @Route("/edit/{id}", bind="user_edit", asserts={"id"="[0-9a-f]{1,24}"}) |
||
99 | * @param Request $request |
||
100 | * @param $id |
||
101 | * @return Response|RedirectResponse |
||
102 | */ |
||
103 | public function editAction(Request $request, $id) |
||
107 | |||
108 | /** |
||
109 | * @Route("/view/{id}", bind="user_view", asserts={"id"="[0-9a-f]{1,24}"}) |
||
110 | * @param Request $request |
||
111 | * @param $id |
||
112 | * @return Response|RedirectResponse |
||
113 | */ |
||
114 | public function viewAction(Request $request, $id) |
||
118 | |||
119 | /** |
||
120 | * @Route("/delete/{id}", bind="user_delete", asserts={"id"="[0-9a-f]{1,24}"}, method="GET") |
||
121 | * @param Request $request |
||
122 | * @param $id |
||
123 | * @return RedirectResponse |
||
124 | */ |
||
125 | public function deleteAction(Request $request, $id) |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | protected function crudListRole() |
||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function crudCreateRole() |
||
145 | |||
146 | /** |
||
147 | * @param Request $request |
||
148 | * @param object $object |
||
149 | * |
||
150 | * @return FormTypeInterface |
||
151 | */ |
||
152 | protected function crudCreateFormType(Request $request, $object) |
||
156 | |||
157 | /** |
||
158 | * @param User $object |
||
159 | * @param FormInterface $form |
||
160 | * @param Request $request |
||
161 | * @return void |
||
162 | */ |
||
163 | protected function crudCreatePrePersist($object, FormInterface $form, Request $request) |
||
167 | |||
168 | /** |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function crudEditRole() |
||
175 | |||
176 | /** |
||
177 | * @param Request $request |
||
178 | * @param object $object |
||
179 | * |
||
180 | * @return FormTypeInterface |
||
181 | */ |
||
182 | protected function crudEditFormType(Request $request, $object) |
||
186 | |||
187 | /** |
||
188 | * @param User $object |
||
189 | * @param FormInterface $form |
||
190 | * @param Request $request |
||
191 | * @return void |
||
192 | */ |
||
193 | protected function crudEditPrePersist($object, FormInterface $form, Request $request) |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | protected function crudViewRole() |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | protected function crudDeleteRole() |
||
213 | |||
214 | /** |
||
215 | * @return string |
||
216 | */ |
||
217 | protected function crudName() |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | protected function crudObjectClass() |
||
229 | } |
||
230 |
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.