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 |
||
| 32 | class SupplierController extends AbstractController |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Lists all Supplier entities. |
||
| 36 | * |
||
| 37 | * @Route("/", name="suppliers") |
||
| 38 | * @Method("GET") |
||
| 39 | * @Template() |
||
| 40 | */ |
||
| 41 | View Code Duplication | public function indexAction(Request $request) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Finds and displays a Supplier entity. |
||
| 55 | * |
||
| 56 | * @Route("/{slug}/show", name="suppliers_show") |
||
| 57 | * @Method("GET") |
||
| 58 | * @Template() |
||
| 59 | */ |
||
| 60 | public function showAction(Supplier $supplier) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Displays a form to create a new Supplier entity. |
||
| 77 | * |
||
| 78 | * @Route("/admin/new", name="suppliers_new") |
||
| 79 | * @Method("GET") |
||
| 80 | * @Template() |
||
| 81 | */ |
||
| 82 | public function newAction() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Creates a new Supplier entity. |
||
| 95 | * |
||
| 96 | * @Route("/create", name="suppliers_create") |
||
| 97 | * @Method("POST") |
||
| 98 | * @Template("AppBundle:Supplier:new.html.twig") |
||
| 99 | */ |
||
| 100 | public function createAction(Request $request) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Displays a form to edit an existing Supplier entity. |
||
| 120 | * |
||
| 121 | * @Route("/admin/{slug}/edit", name="suppliers_edit") |
||
| 122 | * @Method("GET") |
||
| 123 | * @Template() |
||
| 124 | */ |
||
| 125 | public function editAction(Supplier $supplier) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Edits an existing Supplier entity. |
||
| 142 | * |
||
| 143 | * @Route("/{slug}/update", name="suppliers_update") |
||
| 144 | * @Method("PUT") |
||
| 145 | * @Template("AppBundle:Supplier:edit.html.twig") |
||
| 146 | */ |
||
| 147 | public function updateAction(Supplier $supplier, Request $request) |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Save order. |
||
| 169 | * |
||
| 170 | * @Route("/order/{field}/{type}", name="suppliers_sort") |
||
| 171 | */ |
||
| 172 | public function sortAction($field, $type) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Deletes a Supplier entity. |
||
| 181 | * |
||
| 182 | * @Route("/admin/{id}/delete", name="suppliers_delete", requirements={"id"="\d+"}) |
||
| 183 | * @Method("DELETE") |
||
| 184 | */ |
||
| 185 | public function deleteAction(Supplier $supplier, Request $request) |
||
| 207 | } |
||
| 208 |
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.