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 InventoryController extends AbstractInventoryController |
||
38 | { |
||
39 | /** |
||
40 | * Lists all Inventory entities. |
||
41 | * |
||
42 | * @Route("/", name="inventory") |
||
43 | * @Method("GET") |
||
44 | * @Template() |
||
45 | * |
||
46 | * @param \Symfony\Component\HttpFoundation\Request $request Paginate request |
||
47 | * @return array |
||
48 | */ |
||
49 | View Code Duplication | public function indexAction(Request $request) |
|
58 | |||
59 | /** |
||
60 | * Finds and displays a Inventory entity. |
||
61 | * |
||
62 | * @Route("/{id}/show", name="inventory_show", requirements={"id"="\d+"}) |
||
63 | * @Method("GET") |
||
64 | * @Template() |
||
65 | * |
||
66 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to display |
||
67 | * @return array |
||
68 | */ |
||
69 | public function showAction(Inventory $inventory) |
||
86 | |||
87 | /** |
||
88 | * Creates a new Inventory entity. |
||
89 | * |
||
90 | * @Route("/admin/create", name="inventory_create") |
||
91 | * @Method("PUT") |
||
92 | * |
||
93 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
94 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
95 | */ |
||
96 | public function createAction(Request $request) |
||
123 | |||
124 | /** |
||
125 | * Displays a form to edit an existing Inventory entity. |
||
126 | * |
||
127 | * @Route("/admin/{id}/edit", name="inventory_edit", requirements={"id"="\d+"}) |
||
128 | * @Method("GET") |
||
129 | * @Template() |
||
130 | * |
||
131 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to edit |
||
132 | * @return array |
||
133 | */ |
||
134 | public function editAction(Inventory $inventory) |
||
140 | |||
141 | /** |
||
142 | * Edits an existing Inventory entity. |
||
143 | * |
||
144 | * @Route("/admin/{id}/update", name="inventory_update", requirements={"id"="\d+"}) |
||
145 | * @Method("PUT") |
||
146 | * @Template("AppBundle:Stocks/Inventory:edit.html.twig") |
||
147 | * |
||
148 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to update |
||
149 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
150 | * @return array |
||
151 | */ |
||
152 | public function updateAction(Inventory $inventory, Request $request) |
||
165 | |||
166 | /** |
||
167 | * Displays a form to valid an existing Inventory entity. |
||
168 | * |
||
169 | * @Route("/admin/{id}/valid", name="inventory_valid", requirements={"id"="\d+"}) |
||
170 | * @Method("GET") |
||
171 | * @Template() |
||
172 | * |
||
173 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to validate |
||
174 | * @return array |
||
175 | */ |
||
176 | public function validAction(Inventory $inventory) |
||
187 | |||
188 | /** |
||
189 | * Close an existing Inventory entity. |
||
190 | * |
||
191 | * @Route("/admin/{id}/close", name="inventory_close", requirements={"id"="\d+"}) |
||
192 | * @Method("PUT") |
||
193 | * @Template("AppBundle:Stocks/Inventory:valid.html.twig") |
||
194 | * |
||
195 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to close |
||
196 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
197 | * @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
||
198 | */ |
||
199 | public function closeAction(Inventory $inventory, Request $request) |
||
224 | |||
225 | /** |
||
226 | * Deletes a Inventory entity. |
||
227 | * |
||
228 | * @Route("/admin/{id}/delete", name="inventory_delete", requirements={"id"="\d+"}) |
||
229 | * @Method("DELETE") |
||
230 | * |
||
231 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to delete |
||
232 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
233 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
234 | */ |
||
235 | public function deleteAction(Inventory $inventory, Request $request) |
||
241 | |||
242 | /** |
||
243 | * Print the current inventory.<br />Creating a `PDF` file for viewing on paper |
||
244 | * |
||
245 | * @Route("/{id}/print/", name="inventory_print", requirements={"id"="\d+"}) |
||
246 | * @Method("GET") |
||
247 | * @Template() |
||
248 | * |
||
249 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory item to print |
||
250 | * @return \Symfony\Component\HttpFoundation\Response |
||
251 | */ |
||
252 | public function printAction(Inventory $inventory) |
||
270 | |||
271 | /** |
||
272 | * Print the preparation of inventory.<br />Creating a `PDF` file for viewing on paper |
||
273 | * |
||
274 | * @Route("/{id}/print/{inventoryStyle}/prepare", name="inventory_print_prepare", requirements={"id"="\d+"}) |
||
275 | * @Method("GET") |
||
276 | * @Template() |
||
277 | * |
||
278 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory to print |
||
279 | * @param string $inventoryStyle Style of inventory |
||
280 | * @return \Symfony\Component\HttpFoundation\Response |
||
281 | */ |
||
282 | public function prepareDataAction(Inventory $inventory, $inventoryStyle) |
||
296 | |||
297 | /** |
||
298 | * get Html for PDF file. |
||
299 | * |
||
300 | * @param \AppBundle\Entity\Stocks\Inventory $inventory Inventory entity |
||
301 | * @param string $inventoryStyle Style of inventory |
||
302 | * @return string The rendered view |
||
303 | */ |
||
304 | private function getHtml(Inventory $inventory, $inventoryStyle) |
||
323 | |||
324 | /** |
||
325 | * Save the articles of inventory. |
||
326 | * |
||
327 | * @param array $articles |
||
328 | * @param \AppBundle\Entity\Stocks\Inventory $inventory |
||
329 | * @param \Doctrine\Common\Persistence\ObjectManager $etm |
||
330 | */ |
||
331 | View Code Duplication | private function saveInventoryArticles(array $articles, Inventory $inventory, ObjectManager $etm) |
|
347 | } |
||
348 |
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.