| Conditions | 35 |
| Paths | 10 |
| Total Lines | 202 |
| Code Lines | 144 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 13 | ||
| Bugs | 2 | Features | 2 |
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 |
||
| 51 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 52 | { |
||
| 53 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
| 54 | |||
| 55 | $widgetRepo = $em->getRepository('VictoireWidgetBundle:Widget'); |
||
| 56 | $widgetMapRepo = $em->getRepository('VictoireWidgetMapBundle:WidgetMap'); |
||
|
|
|||
| 57 | |||
| 58 | if ($viewId = $input->getOption('view')) { |
||
| 59 | $views = [$em->getRepository('VictoireCoreBundle:View')->find($viewId)]; |
||
| 60 | } else { |
||
| 61 | $templateRepo = $em->getRepository('VictoireTemplateBundle:Template'); |
||
| 62 | $rootTemplates = $templateRepo->getInstance() |
||
| 63 | ->where('template.template IS NULL') |
||
| 64 | ->getQuery() |
||
| 65 | ->getResult(); |
||
| 66 | $templates = []; |
||
| 67 | $recursiveGetTemplates = function ($template) use (&$recursiveGetTemplates, &$templates) { |
||
| 68 | array_push($templates, $template); |
||
| 69 | foreach ($template->getInheritors() as $template) { |
||
| 70 | if ($template instanceof Template) { |
||
| 71 | $recursiveGetTemplates($template); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | }; |
||
| 75 | |||
| 76 | foreach ($rootTemplates as $rootTemplate) { |
||
| 77 | $recursiveGetTemplates($rootTemplate); |
||
| 78 | } |
||
| 79 | |||
| 80 | $pageRepo = $em->getRepository('VictoirePageBundle:BasePage'); |
||
| 81 | $pages = $pageRepo->findAll(); |
||
| 82 | $errorRepo = $em->getRepository('VictoireTwigBundle:ErrorPage'); |
||
| 83 | $errorPages = $errorRepo->findAll(); |
||
| 84 | |||
| 85 | $views = array_merge($templates, array_merge($pages, $errorPages)); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** @var View $view */ |
||
| 89 | foreach ($views as $view) { |
||
| 90 | $this->getContainer()->get('victoire_widget_map.builder')->build($view); |
||
| 91 | $widgets = []; |
||
| 92 | foreach ($view->getWidgets() as $widget) { |
||
| 93 | $widgets[$widget->getId()] = $widget; |
||
| 94 | } |
||
| 95 | |||
| 96 | $oldWidgetMaps = $view->getWidgetMap(); |
||
| 97 | if (!empty($oldWidgetMaps)) { |
||
| 98 | foreach ($oldWidgetMaps as $slot => $oldWidgetMap) { |
||
| 99 | $widgetMaps = []; |
||
| 100 | usort($oldWidgetMap, function ($a, $b) { |
||
| 101 | if ($b['position'] - $a['position'] == 0) { |
||
| 102 | return 1; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $b['position'] - $a['position']; |
||
| 106 | }); |
||
| 107 | |||
| 108 | foreach ($oldWidgetMap as $key => $item) { |
||
| 109 | if ($item['action'] !== 'create') { |
||
| 110 | unset($oldWidgetMap[$key]); |
||
| 111 | $oldWidgetMap[] = $item; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | foreach ($oldWidgetMap as $key => $item) { |
||
| 116 | if ($item['positionReference'] != null) { |
||
| 117 | foreach ($oldWidgetMap as $_key => $_item) { |
||
| 118 | if ($_item['widgetId'] == $item['positionReference']) { |
||
| 119 | array_splice($oldWidgetMap[$_key], 0, 0, [$item]); |
||
| 120 | unset($oldWidgetMap[$key]); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | foreach ($oldWidgetMap as $position => $_oldWidgetMap) { |
||
| 127 | $output->writeln('=========================='); |
||
| 128 | $output->writeln($slot); |
||
| 129 | |||
| 130 | $widget = $widgetRepo->find($_oldWidgetMap['widgetId']); |
||
| 131 | if (!$widget) { |
||
| 132 | $output->writeln('widget does not exists'); |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | $widgetMap = new WidgetMap(); |
||
| 136 | $referencedWidgetMap = null; |
||
| 137 | if ($_oldWidgetMap['positionReference'] != 0 && $_oldWidgetMap['positionReference'] != null) { |
||
| 138 | $output->writeln('has positionReference'); |
||
| 139 | $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']); |
||
| 140 | $output->writeln($referencedWidget->getId()); |
||
| 141 | $referencedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($referencedWidget, $view); |
||
| 142 | while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) { |
||
| 143 | $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER); |
||
| 144 | } |
||
| 145 | $output->writeln('set parent'.$referencedWidgetMap->getWidget()->getId()); |
||
| 146 | $widgetMap->setParent($referencedWidgetMap); |
||
| 147 | $widgetMap->setPosition(WidgetMap::POSITION_AFTER); |
||
| 148 | } else { |
||
| 149 | $output->writeln('has no positionReference'); |
||
| 150 | if ($position == 0) { |
||
| 151 | if (!isset($view->getBuiltWidgetMap()[$slot])) { |
||
| 152 | $widgetMap->setPosition(null); |
||
| 153 | $output->writeln('set parent'.null); |
||
| 154 | $widgetMap->setParent(null); |
||
| 155 | } else { |
||
| 156 | $widgetMap->setPosition(WidgetMap::POSITION_BEFORE); |
||
| 157 | |||
| 158 | $_rootBuilt = null; |
||
| 159 | |||
| 160 | foreach ($view->getBuiltWidgetMap()[$slot] as $_built) { |
||
| 161 | if (!$_built->getParent()) { |
||
| 162 | $_rootBuilt = $_built; |
||
| 163 | break; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | while (null !== $child = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE)) { |
||
| 167 | $_rootBuilt = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE); |
||
| 168 | } |
||
| 169 | $widgetMap->setParent($_rootBuilt); |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | $widgetMap->setPosition(WidgetMap::POSITION_BEFORE); |
||
| 173 | if (!empty(array_slice($widgetMaps, -1))) { |
||
| 174 | $widgetMap->setParent(array_slice($widgetMaps, -1)[0]); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if (WidgetMap::ACTION_OVERWRITE == $_oldWidgetMap['action']) { |
||
| 180 | $output->writeln('is overwrite'); |
||
| 181 | |||
| 182 | /* @var Widget $replacedWidget */ |
||
| 183 | if ($_oldWidgetMap['replacedWidgetId']) { |
||
| 184 | $output->writeln('has replacedWidgetId'); |
||
| 185 | $replacedWidget = $widgetRepo->find($_oldWidgetMap['replacedWidgetId']); |
||
| 186 | $supplicantWidget = $widgetRepo->find($_oldWidgetMap['widgetId']); |
||
| 187 | $replacedWidgetView = $replacedWidget->getView(); |
||
| 188 | $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView); |
||
| 189 | $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget); |
||
| 190 | // If replaced widgetMap does not exists, this is not an overwrite but a create |
||
| 191 | if ($replacedWidgetMap) { |
||
| 192 | $output->writeln('has replacedWidgetMap'); |
||
| 193 | $widgetMap->setReplaced($replacedWidgetMap); |
||
| 194 | $output->writeln('replace '.$replacedWidget->getId().' by '.$supplicantWidget->getId()); |
||
| 195 | $widgetMap->setWidget($supplicantWidget); |
||
| 196 | $widgetMap->setPosition($replacedWidgetMap->getPosition()); |
||
| 197 | $output->writeln('set parent'.($replacedWidgetMap->getParent() ? $replacedWidgetMap->getParent()->getWidget()->getId() : null)); |
||
| 198 | $widgetMap->setParent($replacedWidgetMap->getParent()); |
||
| 199 | } |
||
| 200 | } elseif ($referencedWidgetMap) { |
||
| 201 | $output->writeln('move'); |
||
| 202 | |||
| 203 | $this->getContainer()->get('victoire_widget_map.manager')->move($view, [ |
||
| 204 | 'position' => WidgetMap::POSITION_AFTER, |
||
| 205 | 'slot' => $slot, |
||
| 206 | 'parentWidgetMap' => $referencedWidgetMap, |
||
| 207 | 'widgetMap' => $_oldWidgetMap['widgetId'], |
||
| 208 | ]); |
||
| 209 | } else { |
||
| 210 | $_oldWidgetMap['action'] = WidgetMap::ACTION_CREATE; |
||
| 211 | } |
||
| 212 | } elseif (WidgetMap::ACTION_DELETE == $_oldWidgetMap['action']) { |
||
| 213 | $output->writeln('is delete'); |
||
| 214 | $replacedWidget = $widgetRepo->find($_oldWidgetMap['widgetId']); |
||
| 215 | $widgetMap->setPosition(null); |
||
| 216 | $output->writeln('set parent'.null); |
||
| 217 | $widgetMap->setParent(null); |
||
| 218 | $deletedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($replacedWidget, $view); |
||
| 219 | if ($deletedWidgetMap) { |
||
| 220 | $replacedWidgetView = $replacedWidget->getView(); |
||
| 221 | $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView); |
||
| 222 | $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget); |
||
| 223 | $widgetMap->setReplaced($replacedWidgetMap); |
||
| 224 | |||
| 225 | $this->getContainer()->get('victoire_widget_map.manager')->moveChildren( |
||
| 226 | $view, |
||
| 227 | $deletedWidgetMap->getChild(WidgetMap::POSITION_BEFORE), |
||
| 228 | $deletedWidgetMap->getChild(WidgetMap::POSITION_AFTER), |
||
| 229 | $deletedWidgetMap->getParent(), |
||
| 230 | $deletedWidgetMap->getPosition() |
||
| 231 | ); |
||
| 232 | } else { |
||
| 233 | continue; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | $widgetMap->setAction($_oldWidgetMap['action']); |
||
| 238 | $widgetMap->setWidget($widget); |
||
| 239 | $widgetMap->setAsynchronous($_oldWidgetMap['asynchronous'] ? true : false); |
||
| 240 | $widgetMap->setSlot($slot); |
||
| 241 | $output->writeln('add widgetMap for widget '.$widgetMap->getWidget()->getId().' to view '.$view->getId()); |
||
| 242 | $view->addWidgetMap($widgetMap); |
||
| 243 | $em->persist($widgetMap); |
||
| 244 | $widgetMaps[] = $widgetMap; |
||
| 245 | |||
| 246 | $this->getContainer()->get('victoire_widget_map.builder')->build($view); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | $em->flush(); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.