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 |
||
39 | class ComestibleController extends AbstractCRUDController |
||
40 | { |
||
41 | /** |
||
42 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
43 | * @param TokenStorageInterface $tokenStorage |
||
44 | * @param ListingFactory $listingFactory |
||
45 | * @param ManagerRegistry $doctrine |
||
46 | * @param FormFactory $formFactory |
||
47 | * @param Paginator $paginator |
||
48 | * @param UrlGeneratorInterface $urlGenerator |
||
49 | * @param \Twig_Environment $twig |
||
50 | */ |
||
51 | View Code Duplication | public function __construct( |
|
|
|||
52 | AuthorizationCheckerInterface $authorizationChecker, |
||
53 | TokenStorageInterface $tokenStorage, |
||
54 | ListingFactory $listingFactory, |
||
55 | ManagerRegistry $doctrine, |
||
56 | FormFactory $formFactory, |
||
57 | Paginator $paginator, |
||
58 | UrlGeneratorInterface $urlGenerator, |
||
59 | \Twig_Environment $twig |
||
60 | ) { |
||
61 | $this->authorizationChecker = $authorizationChecker; |
||
62 | $this->tokenStorage = $tokenStorage; |
||
63 | $this->listingFactory = $listingFactory; |
||
64 | $this->doctrine = $doctrine; |
||
65 | $this->formFactory = $formFactory; |
||
66 | $this->paginator = $paginator; |
||
67 | $this->urlGenerator = $urlGenerator; |
||
68 | $this->twig = $twig; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @Route("/", bind="comestible_list", method="GET") |
||
73 | * @param Request $request |
||
74 | * @return Response |
||
75 | */ |
||
76 | public function listAction(Request $request) |
||
80 | |||
81 | /** |
||
82 | * @Route("/create", bind="comestible_create") |
||
83 | * @param Request $request |
||
84 | * @return Response|RedirectResponse |
||
85 | */ |
||
86 | public function createAction(Request $request) |
||
90 | |||
91 | /** |
||
92 | * @Route("/edit/{id}", bind="comestible_edit", asserts={"id"="[0-9a-f]{1,24}"}) |
||
93 | * @param Request $request |
||
94 | * @param $id |
||
95 | * @return Response|RedirectResponse |
||
96 | */ |
||
97 | public function editAction(Request $request, $id) |
||
101 | |||
102 | /** |
||
103 | * @Route("/delete/{id}", bind="comestible_delete", asserts={"id"="[0-9a-f]{1,24}"}, method="GET") |
||
104 | * @param Request $request |
||
105 | * @param $id |
||
106 | * @return RedirectResponse |
||
107 | */ |
||
108 | public function deleteAction(Request $request, $id) |
||
112 | |||
113 | /** |
||
114 | * @Route("/choice", bind="comestible_choice", method="GET") |
||
115 | * @param Request $request |
||
116 | * @return RedirectResponse |
||
117 | */ |
||
118 | public function choiceAction(Request $request) |
||
119 | { |
||
120 | $choiceLabel = $request->query->get('choice_label', 'name'); |
||
121 | $search = urldecode($request->query->get('q', '')); |
||
122 | |||
123 | /** @var ComestibleRepository $repo */ |
||
124 | $repo = $this->crudRepositoryForClass($this->crudObjectClass()); |
||
125 | $propertyAccessor = new PropertyAccessor(); |
||
126 | $data = array(); |
||
127 | foreach ($repo->searchComestibleOfUser($this->getUser(), $search) as $comestible) { |
||
128 | $data[] = array( |
||
129 | 'id' => $comestible->getId(), |
||
130 | 'text' => $propertyAccessor->getValue($comestible, $choiceLabel), |
||
131 | 'default' => $comestible->getDefaultValue(), |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | return new JsonResponse($data); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return int |
||
140 | */ |
||
141 | protected function crudListPerPage() |
||
142 | { |
||
143 | return 20; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param Request $request |
||
148 | * |
||
149 | * @return FormTypeInterface |
||
150 | */ |
||
151 | protected function crudListFormType(Request $request) |
||
155 | |||
156 | /** |
||
157 | * @param Request $request |
||
158 | * @param array $formData |
||
159 | * @return array |
||
160 | */ |
||
161 | protected function crudListFormDataEnrich(Request $request, array $formData) |
||
162 | { |
||
163 | return array_replace_recursive($formData, array( |
||
164 | 'user' => $this->getUser()->getId(), |
||
165 | )); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param Request $request |
||
170 | * @return Comestible |
||
171 | */ |
||
172 | protected function crudCreateFactory(Request $request) |
||
173 | { |
||
174 | $objectClass = $this->crudObjectClass(); |
||
175 | |||
176 | /** @var Comestible $object */ |
||
177 | $object = new $objectClass(); |
||
178 | $object->setUser($this->getUser()); |
||
179 | |||
180 | return $object; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param Request $request |
||
185 | * @param object $object |
||
186 | * |
||
187 | * @return FormTypeInterface |
||
188 | */ |
||
189 | protected function crudCreateFormType(Request $request, $object) |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function crudEditRole() |
||
201 | |||
202 | /** |
||
203 | * @param Request $request |
||
204 | * @param object $object |
||
205 | * |
||
206 | * @return FormTypeInterface |
||
207 | */ |
||
208 | protected function crudEditFormType(Request $request, $object) |
||
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | protected function crudViewRoute() |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | protected function crudDeleteRole() |
||
228 | |||
229 | /** |
||
230 | * @return string |
||
231 | */ |
||
232 | protected function crudName() |
||
236 | |||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | protected function crudObjectClass() |
||
244 | } |
||
245 |
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.