| Conditions | 12 |
| Paths | 4 |
| Total Lines | 66 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 80 | public function cloneView(View $view, $templateName = null) |
||
| 81 | { |
||
| 82 | $clonedView = clone $view; |
||
| 83 | $this->em->refresh($view); |
||
| 84 | $clonedView->setSlug(null); |
||
| 85 | $widgetMapClone = $clonedView->getWidgetMaps(); |
||
| 86 | $arrayMapOfWidgetMap = []; |
||
| 87 | if (null !== $templateName) { |
||
| 88 | $clonedView->setName($templateName); |
||
| 89 | } |
||
| 90 | |||
| 91 | $clonedView->setId(null); |
||
| 92 | $this->em->persist($clonedView); |
||
| 93 | |||
| 94 | if (!$clonedView instanceof BusinessTemplate) { |
||
| 95 | $widgetLayoutSlots = []; |
||
| 96 | $newWidgets = []; |
||
| 97 | foreach ($clonedView->getWidgets() as $widgetKey => $widgetVal) { |
||
| 98 | $clonedWidget = clone $widgetVal; |
||
| 99 | $clonedWidget->setId(null); |
||
| 100 | $clonedWidget->setView($clonedView); |
||
| 101 | $this->em->persist($clonedWidget); |
||
| 102 | $newWidgets[] = $clonedWidget; |
||
| 103 | $arrayMapOfWidgetMap[$widgetVal->getId()] = $clonedWidget; |
||
| 104 | if ($widgetVal instanceof WidgetLayout) { |
||
| 105 | $id = $widgetVal->getId(); |
||
| 106 | $widgetLayoutSlots[$id] = $clonedWidget; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | $clonedView->setWidgets($newWidgets); |
||
| 110 | $this->em->persist($clonedView); |
||
| 111 | $this->em->flush(); |
||
| 112 | $widgetSlotMap = []; |
||
| 113 | foreach ($widgetLayoutSlots as $_id => $_widget) { |
||
| 114 | foreach ($clonedView->getWidgets() as $_clonedWidget) { |
||
| 115 | if (preg_match('/^'.$_id.'_(.)/', $_clonedWidget->getSlot(), $matches)) { |
||
| 116 | $newSlot = $_widget->getId().'_'.$matches[1]; |
||
| 117 | $oldSlot = $_clonedWidget->getSlot(); |
||
| 118 | $_clonedWidget->setSlot($newSlot); |
||
| 119 | $widgetSlotMap[$oldSlot] = $newSlot; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->em->flush(); |
||
| 125 | foreach ($widgetMapClone as $wigetSlotCloneKey => $widgetSlotCloneVal) { |
||
| 126 | foreach ($widgetSlotCloneVal as $widgetMapItemKey => $widgetMapItemVal) { |
||
| 127 | if (isset($arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']])) { |
||
| 128 | $widgetId = $arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']]->getId(); |
||
| 129 | $widgetMapItemVal['widgetId'] = $widgetId; |
||
| 130 | if (array_key_exists($wigetSlotCloneKey, $widgetSlotMap)) { |
||
| 131 | $wigetSlotCloneKey = $widgetSlotMap[$wigetSlotCloneKey]; |
||
| 132 | } |
||
| 133 | $widgetMapClone[$wigetSlotCloneKey][$widgetMapItemKey] = $widgetMapItemVal; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $clonedView->setSlots([]); |
||
| 139 | $clonedView->setWidgetMap($widgetMapClone); |
||
| 140 | $this->em->persist($clonedView); |
||
| 141 | $this->em->flush(); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $clonedView; |
||
| 145 | } |
||
| 146 | } |
||
| 147 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.