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 |
||
34 | class OrdersController extends AbstractOrdersController |
||
35 | { |
||
36 | /** |
||
37 | * Lists all Orders entities. |
||
38 | * |
||
39 | * @Route("/", name="orders") |
||
40 | * @Method("GET") |
||
41 | * @Template() |
||
42 | */ |
||
43 | View Code Duplication | public function indexAction(Request $request) |
|
52 | |||
53 | /** |
||
54 | * Finds and displays a Orders entity. |
||
55 | * |
||
56 | * @Route("/{id}/show", name="orders_show", requirements={"id"="\d+"}) |
||
57 | * @Method("GET") |
||
58 | * @Template() |
||
59 | */ |
||
60 | public function showAction(Orders $orders) |
||
69 | |||
70 | /** |
||
71 | * Displays a form to create a new Orders entity. |
||
72 | * |
||
73 | * @Route("/new/{supplier}", name="orders_new") |
||
74 | * @Method("GET") |
||
75 | * @Template() |
||
76 | */ |
||
77 | public function newAction(Supplier $supplier) |
||
99 | |||
100 | /** |
||
101 | * Creates a new Orders entity. |
||
102 | * |
||
103 | * @Route("/admin/create", name="orders_create") |
||
104 | * @Method("POST") |
||
105 | * @Template("orders/orders/new.html.twig") |
||
106 | */ |
||
107 | public function createAction(Request $request) |
||
140 | |||
141 | /** |
||
142 | * Displays a form to edit an existing Orders entity. |
||
143 | * |
||
144 | * @Route("/admin/{id}/edit", name="orders_edit", requirements={"id"="\d+"}) |
||
145 | * @Method("GET") |
||
146 | * @Template() |
||
147 | */ |
||
148 | public function editAction(Orders $orders) |
||
157 | |||
158 | /** |
||
159 | * Edits an existing Orders entity. |
||
160 | * |
||
161 | * @Route("/admin/{id}/update", name="orders_update", requirements={"id"="\d+"}) |
||
162 | * @Method("PUT") |
||
163 | * @Template("orders/orders/edit.html.twig") |
||
164 | */ |
||
165 | public function updateAction(Orders $orders, Request $request) |
||
184 | |||
185 | /** |
||
186 | * Deletes a Orders entity. |
||
187 | * |
||
188 | * @Route("/admin/{id}/delete", name="orders_delete", requirements={"id"="\d+"}) |
||
189 | * @Method("DELETE") |
||
190 | */ |
||
191 | public function deleteAction(Orders $orders, Request $request) |
||
197 | |||
198 | /** |
||
199 | * Set order Dates. |
||
200 | * |
||
201 | * @param integer $date Order day |
||
202 | * @param \App\Entity\Orders\Orders $orders The order to process |
||
203 | * @param \App\Entity\Settings\Supplier $supplier The supplier concerned |
||
204 | * @return \App\Entity\Orders\Orders The amended order |
||
205 | */ |
||
206 | private function setDates($date, Orders $orders, Supplier $supplier) |
||
223 | |||
224 | /** |
||
225 | * Save OrdersArticles. |
||
226 | * |
||
227 | * @param array $articles List of articles |
||
228 | * @param \App\Entity\Orders\Orders $orders The order to process |
||
229 | * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager |
||
230 | */ |
||
231 | View Code Duplication | private function saveOrdersArticles($articles, $orders, $etm) |
|
246 | |||
247 | /** |
||
248 | * Print the current order.<br />Creating a `PDF` file for viewing on paper |
||
249 | * |
||
250 | * @Route("/{id}/print/", name="orders_print", requirements={"id"="\d+"}) |
||
251 | * @Method("GET") |
||
252 | * @Template() |
||
253 | * |
||
254 | * @param \App\Entity\Orders\Orders $orders Order item to print |
||
255 | * @return \Symfony\Component\HttpFoundation\Response |
||
256 | */ |
||
257 | public function printAction(Orders $orders) |
||
263 | } |
||
264 |
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.