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 |
||
| 19 | class ArticleController extends Controller |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @param Request $request |
||
| 23 | * @param $page |
||
| 24 | * @Method({"GET", "POST"}) |
||
| 25 | * @Route("/articles/{pager}/{page}", name="articlesAdmin", |
||
| 26 | * defaults={"pager": "page", "page": 1}, |
||
| 27 | * requirements={ |
||
| 28 | * "pager": "page", |
||
| 29 | * "page": "[1-9]\d*", |
||
| 30 | * }) |
||
| 31 | * @Template("AppBundle:admin:articles.html.twig") |
||
| 32 | * |
||
| 33 | * @return Response |
||
| 34 | */ |
||
| 35 | 1 | View Code Duplication | public function roleAction(Request $request, $page = 1) |
| 36 | { |
||
| 37 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 38 | 1 | $articles = $em->getRepository("AppBundle:Article") |
|
| 39 | 1 | ->getArticlesWithCountComment($page); |
|
| 40 | |||
| 41 | return [ |
||
| 42 | 1 | 'articles' => $articles, |
|
| 43 | 1 | ]; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $id |
||
| 48 | * @param $action |
||
| 49 | * @param Request $request |
||
| 50 | * @Route("/article/{action}/{id}", name="articleEdit", |
||
| 51 | * defaults={"id": 0}, |
||
| 52 | * requirements={ |
||
| 53 | * "action": "new|edit", |
||
| 54 | * "id": "\d+" |
||
| 55 | * }) |
||
| 56 | * @Method({"GET", "POST"}) |
||
| 57 | * @Template("AppBundle:admin/form:article.html.twig") |
||
| 58 | * |
||
| 59 | * @return Response |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function editArticleAction($id, $action, Request $request) |
|
| 62 | { |
||
| 63 | $em = $this->getDoctrine()->getManager(); |
||
| 64 | if ($action == "edit") { |
||
| 65 | $article = $em->getRepository('AppBundle:Article') |
||
| 66 | ->find($id); |
||
| 67 | $title = 'Edit article id: '.$id; |
||
| 68 | } |
||
| 69 | else { |
||
| 70 | $article = new Article(); |
||
| 71 | $title = 'Create new article'; |
||
| 72 | } |
||
| 73 | |||
| 74 | $form = $this->createForm(ArticleType::class, $article, [ |
||
| 75 | 'em' => $em, |
||
| 76 | 'action' => $this->generateUrl('articleEdit', ['action' => $action, 'id' => $id]), |
||
| 77 | 'method' => Request::METHOD_POST, |
||
| 78 | ]) |
||
| 79 | ->add('save', SubmitType::class, array('label' => 'Save')); |
||
| 80 | |||
| 81 | if ($request->getMethod() == 'POST') { |
||
| 82 | $form->handleRequest($request); |
||
| 83 | if ($form->isValid()) { |
||
| 84 | $em->persist($article); |
||
| 85 | $em->flush(); |
||
| 86 | |||
| 87 | return $this->redirectToRoute('articlesAdmin'); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | return [ |
||
| 92 | 'title' => $title, |
||
| 93 | 'form' => $form->createView(), |
||
| 94 | ]; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param $id |
||
| 99 | * @param Request $request |
||
| 100 | * @Route("/article/delete/{id}", name="articleDelete", |
||
| 101 | * requirements={ |
||
| 102 | * "id": "\d+" |
||
| 103 | * }) |
||
| 104 | * @Method({"GET", "POST"}) |
||
| 105 | * @Template("AppBundle:admin/form:delete.html.twig") |
||
| 106 | * |
||
| 107 | * @return Response |
||
| 108 | */ |
||
| 109 | public function deleteArticleAction($id, Request $request) |
||
| 146 | |||
| 147 | } |
||
| 148 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.