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 MaterialController extends AbstractController |
||
33 | { |
||
34 | /** |
||
35 | * Lists all Material entities. |
||
36 | * |
||
37 | * @Route("/", name="material") |
||
38 | * @Method("GET") |
||
39 | * @Template() |
||
40 | */ |
||
41 | public function indexAction(Request $request) |
||
47 | |||
48 | /** |
||
49 | * Finds and displays a Material entity. |
||
50 | * |
||
51 | * @Route("/{slug}/show", name="material_show") |
||
52 | * @Method("GET") |
||
53 | * @Template() |
||
54 | */ |
||
55 | public function showAction(Material $material) |
||
61 | |||
62 | /** |
||
63 | * Displays a form to create a new Material entity. |
||
64 | * |
||
65 | * @Route("/new", name="material_new") |
||
66 | * @Method("GET") |
||
67 | * @Template() |
||
68 | */ |
||
69 | View Code Duplication | public function newAction() |
|
82 | |||
83 | /** |
||
84 | * Creates a new Material entity. |
||
85 | * |
||
86 | * @Route("/admin/create", name="material_create") |
||
87 | * @Method("POST") |
||
88 | * @Template("AppBundle:Config\Material:new.html.twig") |
||
89 | */ |
||
90 | public function createAction(Request $request) |
||
102 | |||
103 | /** |
||
104 | * Displays a form to edit an existing Material entity. |
||
105 | * |
||
106 | * @Route("/admin/{slug}/edit", name="material_edit") |
||
107 | * @Method("GET") |
||
108 | * @Template() |
||
109 | */ |
||
110 | View Code Duplication | public function editAction(Material $material) |
|
118 | |||
119 | /** |
||
120 | * Edits an existing Material entity. |
||
121 | * |
||
122 | * @Route("/admin/{slug}/update", name="material_update") |
||
123 | * @Method("PUT") |
||
124 | * @Template("AppBundle:Config\Material:edit.html.twig") |
||
125 | */ |
||
126 | public function updateAction(Material $material, Request $request) |
||
132 | |||
133 | /** |
||
134 | * Save order. |
||
135 | * |
||
136 | * @Route("/order/{entity}/{field}/{type}", name="material_sort") |
||
137 | * |
||
138 | * @param string $entity Entity of the field to sort |
||
139 | * @param string $field Field to sort |
||
140 | * @param string $type type of sort |
||
141 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
142 | */ |
||
143 | public function sortAction($entity, $field, $type) |
||
149 | |||
150 | /** |
||
151 | * Deletes a Material entity. |
||
152 | * |
||
153 | * @Route("/{id}/delete", name="material_delete", requirements={"id"="\d+"}) |
||
154 | * @Method("DELETE") |
||
155 | */ |
||
156 | public function deleteAction(Material $material, Request $request) |
||
162 | } |
||
163 |
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.