Conditions | 11 |
Paths | 24 |
Total Lines | 86 |
Code Lines | 48 |
Lines | 8 |
Ratio | 9.3 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
39 | protected function execute(InputInterface $input, OutputInterface $output) |
||
40 | { |
||
41 | //Get options |
||
42 | $outOfDate = $input->getOption('outofdate'); |
||
43 | $limit = $input->getOption('limit'); |
||
44 | |||
45 | //Get services |
||
46 | |||
47 | /** @var EntityManager $entityManager */ |
||
48 | $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
49 | $viewCssBuilder = $this->getContainer()->get('victoire_core.view_css_builder'); |
||
50 | $widgetMapBuilder = $this->getContainer()->get('victoire_widget_map.builder'); |
||
51 | |||
52 | $widgetRepo = $entityManager->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget'); |
||
53 | |||
54 | // //Get View's CSS to generate |
||
55 | $templateRepo = $entityManager->getRepository('VictoireTemplateBundle:Template'); |
||
56 | $rootTemplates = $templateRepo->getInstance() |
||
57 | ->where('template.template IS NULL') |
||
58 | ->getQuery() |
||
59 | ->getResult(); |
||
60 | $templates = []; |
||
61 | View Code Duplication | $recursiveGetTemplates = function ($template) use (&$recursiveGetTemplates, &$templates) { |
|
|
|||
62 | array_push($templates, $template); |
||
63 | foreach ($template->getInheritors() as $template) { |
||
64 | if ($template instanceof Template) { |
||
65 | $recursiveGetTemplates($template); |
||
66 | } |
||
67 | } |
||
68 | }; |
||
69 | |||
70 | foreach ($rootTemplates as $rootTemplate) { |
||
71 | $recursiveGetTemplates($rootTemplate); |
||
72 | } |
||
73 | |||
74 | $pageRepo = $entityManager->getRepository('VictoirePageBundle:BasePage'); |
||
75 | $pages = $pageRepo->findAll(); |
||
76 | $errorRepo = $entityManager->getRepository('VictoireTwigBundle:ErrorPage'); |
||
77 | $errorPages = $errorRepo->findAll(); |
||
78 | |||
79 | /* @var $views View[] */ |
||
80 | $views = array_merge($templates, array_merge($pages, $errorPages)); |
||
81 | |||
82 | //Prepare limit |
||
83 | $limit = ($limit && $limit < count($views)) ? $limit : count($views); |
||
84 | $count = 0; |
||
85 | |||
86 | //Set progress for output |
||
87 | $progress = $this->getHelper('progress'); |
||
88 | $progress->start($output, $limit); |
||
89 | |||
90 | foreach ($views as $view) { |
||
91 | if ($outOfDate && $view->isCssUpToDate()) { |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | //Exit if limit reached |
||
96 | if ($count >= $limit) { |
||
97 | break; |
||
98 | } |
||
99 | |||
100 | //If hash already exist, remove CSS file, else generate a hash |
||
101 | if ($viewHash = $view->getCssHash()) { |
||
102 | $viewCssBuilder->removeCssFile($viewHash); |
||
103 | } else { |
||
104 | $view->changeCssHash(); |
||
105 | } |
||
106 | |||
107 | //Generate CSS file with its widgets style |
||
108 | $widgetMapBuilder->build($view, $entityManager); |
||
109 | $widgets = $widgetRepo->findAllWidgetsForView($view); |
||
110 | $viewCssBuilder->generateViewCss($view, $widgets); |
||
111 | |||
112 | //Set View's CSS as up to date |
||
113 | $view->setCssUpToDate(true); |
||
114 | $entityManager->persist($view); |
||
115 | $entityManager->flush($view); |
||
116 | |||
117 | ++$count; |
||
118 | $progress->advance(); |
||
119 | } |
||
120 | |||
121 | $progress->finish(); |
||
122 | |||
123 | return true; |
||
124 | } |
||
125 | } |
||
126 |
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.