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 |
||
21 | class TemplateController extends Controller |
||
22 | { |
||
23 | /** |
||
24 | * list of all templates. |
||
25 | * |
||
26 | * @Route("/index", name="victoire_template_index") |
||
27 | * @Configuration\Template() |
||
28 | * |
||
29 | * @return JsonResponse |
||
30 | */ |
||
31 | public function indexAction() |
||
45 | |||
46 | /** |
||
47 | * list of all templates. |
||
48 | * |
||
49 | * @param Template $template The template |
||
50 | * |
||
51 | * @Route("/show/{id}", name="victoire_template_show") |
||
52 | * @ParamConverter("template", class="VictoireTemplateBundle:Template") |
||
53 | * |
||
54 | * @return Response |
||
55 | */ |
||
56 | public function showAction(Template $template) |
||
57 | { |
||
58 | //add the view to twig |
||
59 | $this->get('twig')->addGlobal('view', $template); |
||
60 | $template->setReference(new ViewReference($template->getId())); |
||
61 | $event = new TemplateMenuContextualEvent($template); |
||
62 | |||
63 | //TODO : il serait bon de faire des constantes pour les noms d'évents |
||
64 | $eventName = 'victoire_core.'.Template::TYPE.'_menu.contextual'; |
||
65 | |||
66 | $this->get('event_dispatcher')->dispatch($eventName, $event); |
||
67 | |||
68 | //the victoire templating |
||
69 | $templating = $this->container->get('templating'); |
||
70 | $layout = $template->getLayout().'.html.twig'; |
||
71 | |||
72 | $parameters = [ |
||
73 | 'view' => $template, |
||
74 | 'id' => $template->getId(), |
||
75 | 'locale' => $template->getCurrentLocale(), |
||
76 | ]; |
||
77 | |||
78 | $this->get('victoire_widget_map.builder')->build($template); |
||
79 | $this->get('victoire_widget_map.widget_data_warmer')->warm( |
||
80 | $this->get('doctrine.orm.entity_manager'), |
||
81 | $template |
||
82 | ); |
||
83 | |||
84 | $this->container->get('victoire_core.current_view')->setCurrentView($template); |
||
85 | |||
86 | //create the response |
||
87 | $response = $templating->renderResponse( |
||
88 | 'VictoireCoreBundle:Layout:'.$layout, |
||
89 | $parameters |
||
90 | ); |
||
91 | |||
92 | return $response; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * create a new Template. |
||
97 | * |
||
98 | * @return JsonResponse |
||
99 | * @Route("/new", name="victoire_template_new") |
||
100 | * @Configuration\Template() |
||
101 | */ |
||
102 | public function newAction() |
||
129 | |||
130 | /** |
||
131 | * define settings of the template. |
||
132 | * |
||
133 | * @param Template $template |
||
134 | * |
||
135 | * @return JsonResponse |
||
136 | * @Route("/{id}/parametres", name="victoire_template_settings") |
||
137 | * @ParamConverter("template", class="VictoireTemplateBundle:Template") |
||
138 | */ |
||
139 | public function settingsAction(Request $request, $template) |
||
167 | |||
168 | /** |
||
169 | * edit a Template. |
||
170 | * |
||
171 | * @param Template $template The Template to edit |
||
172 | * |
||
173 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
174 | * @Route("/edit/{slug}", name="victoire_template_edit") |
||
175 | * @Configuration\Template() |
||
176 | * @ParamConverter("template", class="VictoireTemplateBundle:Template") |
||
177 | */ |
||
178 | public function editAction(Template $template) |
||
193 | } |
||
194 |
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.