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 | View Code Duplication | class CompanyController extends AbstractController |
|
|
1 ignored issue
–
show
|
|||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Lists all Company entities. |
||
| 36 | * |
||
| 37 | * @Route("/", name="admin_company") |
||
| 38 | * @Method("GET") |
||
| 39 | * @Template() |
||
| 40 | */ |
||
| 41 | public function indexAction() |
||
| 42 | { |
||
| 43 | $em = $this->getDoctrine()->getManager(); |
||
| 44 | $entities = $em->getRepository('AppBundle:Company')->findAll(); |
||
| 45 | |||
| 46 | return array( |
||
| 47 | 'entities' => $entities, |
||
| 48 | 'ctEntity' => count($entities), |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Finds and displays a Company entity. |
||
| 54 | * |
||
| 55 | * @Route("/{id}/show", name="admin_company_show", requirements={"id"="\d+"}) |
||
| 56 | * @Method("GET") |
||
| 57 | * @Template() |
||
| 58 | */ |
||
| 59 | public function showAction(Company $company) |
||
| 60 | { |
||
| 61 | $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete'); |
||
| 62 | |||
| 63 | return array( |
||
| 64 | 'company' => $company, |
||
| 65 | 'delete_form' => $deleteForm->createView(), |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Displays a form to create a new Company entity. |
||
| 71 | * |
||
| 72 | * @Route("/new", name="admin_company_new") |
||
| 73 | * @Method("GET") |
||
| 74 | * @Template() |
||
| 75 | */ |
||
| 76 | public function newAction() |
||
| 77 | { |
||
| 78 | $company = new Company(); |
||
| 79 | $form = $this->createForm(new CompanyType(), $company); |
||
| 80 | |||
| 81 | return array( |
||
| 82 | 'company' => $company, |
||
| 83 | 'form' => $form->createView(), |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Creates a new Company entity. |
||
| 89 | * |
||
| 90 | * @Route("/create", name="admin_company_create") |
||
| 91 | * @Method("POST") |
||
| 92 | * @Template("AppBundle:Settings/Company:new.html.twig") |
||
| 93 | */ |
||
| 94 | public function createAction(Request $request) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Displays a form to edit an existing Company entity. |
||
| 114 | * |
||
| 115 | * @Route("/{id}/edit", name="admin_company_edit", requirements={"id"="\d+"}) |
||
| 116 | * @Method("GET") |
||
| 117 | * @Template() |
||
| 118 | */ |
||
| 119 | public function editAction(Company $company) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Edits an existing Company entity. |
||
| 136 | * |
||
| 137 | * @Route("/{id}/update", name="admin_company_update", requirements={"id"="\d+"}) |
||
| 138 | * @Method("PUT") |
||
| 139 | * @Template("AppBundle:Company:edit.html.twig") |
||
| 140 | */ |
||
| 141 | public function updateAction(Company $company, Request $request) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Deletes a Company entity. |
||
| 164 | * |
||
| 165 | * @Route("/{id}/delete", name="admin_company_delete", requirements={"id"="\d+"}) |
||
| 166 | * @Method("DELETE") |
||
| 167 | */ |
||
| 168 | public function deleteAction(Company $company, Request $request) |
||
| 179 | } |
||
| 180 |
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.