Conditions | 37 |
Paths | 4032 |
Total Lines | 136 |
Code Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | 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 |
||
81 | public function upgrade(string $oldVersion): bool |
||
82 | { |
||
83 | $blockRepository = $this->entityManager->getRepository(BlockEntity::class); |
||
84 | // Upgrade dependent on old version number |
||
85 | switch ($oldVersion) { |
||
86 | case '3.8.1': |
||
87 | case '3.8.2': |
||
88 | case '3.9.0': |
||
89 | $sql = 'SELECT * FROM blocks'; |
||
90 | $blocks = $this->entityManager->getConnection()->fetchAll($sql); |
||
|
|||
91 | foreach ($blocks as $block) { |
||
92 | $content = $block['content']; |
||
93 | if ($this->isSerialized($content)) { |
||
94 | $content = unserialize($content); |
||
95 | foreach ($content as $k => $item) { |
||
96 | if (is_string($item)) { |
||
97 | if (false !== mb_strpos($item, 'blocks_block_extmenu_topnav.tpl')) { |
||
98 | $content[$k] = str_replace('blocks_block_extmenu_topnav.tpl', 'Block/Extmenu/topnav.tpl', $item); |
||
99 | } elseif (false !== mb_strpos($item, 'blocks_block_extmenu.tpl')) { |
||
100 | $content[$k] = str_replace('blocks_block_extmenu.tpl', 'Block/Extmenu/extmenu.tpl', $item); |
||
101 | } elseif (false !== mb_strpos($item, 'menutree/blocks_block_menutree_')) { |
||
102 | $content[$k] = str_replace('menutree/blocks_block_menutree_', 'Block/Menutree/', $item); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | $this->entityManager->getConnection()->executeUpdate('UPDATE blocks SET content=? WHERE bid=?', [serialize($content), $block['bid']]); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // check if request is available (#2073) |
||
111 | $templateWarning = $this->trans('Warning: Block template locations modified, you may need to fix your template overrides if you have any.'); |
||
112 | $request = $this->requestStack->getMasterRequest(); |
||
113 | if ( |
||
114 | is_object($request) |
||
115 | && method_exists($request, 'getSession') |
||
116 | && is_object($request->getSession()) |
||
117 | ) { |
||
118 | $this->addFlash('warning', $templateWarning); |
||
119 | } |
||
120 | case '3.9.1': |
||
121 | // make all content fields of blocks serialized. |
||
122 | $sql = 'SELECT * FROM blocks'; |
||
123 | $blocks = $this->entityManager->getConnection()->fetchAll($sql); |
||
124 | $oldContent = []; |
||
125 | foreach ($blocks as $block) { |
||
126 | $block['content'] = !empty($block['content']) ? $block['content'] : ''; |
||
127 | $oldContent[$block['bid']] = $this->isSerialized($block['content']) ? unserialize($block['content']) : ['content' => $block['content']]; |
||
128 | } |
||
129 | $this->schemaTool->update($this->entities); |
||
130 | $this->entityManager->getConnection()->executeQuery("UPDATE blocks SET properties='a:0:{}'"); |
||
131 | |||
132 | $blocks = $blockRepository->findAll(); |
||
133 | $installerHelper = new InstallerHelper(); |
||
134 | /** @var ZikulaHttpKernelInterface $kernel */ |
||
135 | /** @var BlockEntity $block */ |
||
136 | foreach ($blocks as $block) { |
||
137 | $block->setProperties($oldContent[$block->getBid()]); |
||
138 | $block->setFilters($installerHelper->upgradeFilterArray($block->getFilters())); |
||
139 | $block->setBlocktype(preg_match('/Block$/', $block->getBkey()) ? mb_substr($block->getBkey(), 0, -5) : $block->getBkey()); |
||
140 | $block->setBkey($installerHelper->upgradeBkeyToFqClassname($this->kernel, $block)); |
||
141 | } |
||
142 | $this->entityManager->flush(); |
||
143 | |||
144 | $collapseable = $this->getVar('collapseable'); |
||
145 | $this->setVar('collapseable', (bool)$collapseable); |
||
146 | |||
147 | case '3.9.2': |
||
148 | // convert Text and Html block types so properties is proper array |
||
149 | $blocks = $blockRepository->findBy(['blocktype' => ['Html', 'Text']]); |
||
150 | foreach ($blocks as $block) { |
||
151 | $properties = $block->getProperties(); |
||
152 | if (!is_array($properties)) { |
||
153 | $block->setProperties(['content' => $properties]); |
||
154 | } |
||
155 | } |
||
156 | $this->entityManager->flush(); |
||
157 | case '3.9.3': |
||
158 | $this->schemaTool->drop([ |
||
159 | 'Zikula\BlocksModule\Entity\UserBlockEntity' |
||
160 | ]); |
||
161 | case '3.9.4': |
||
162 | // convert integer values to boolean for search block settings |
||
163 | $searchBlocks = $blockRepository->findBy(['blocktype' => 'Search']); |
||
164 | foreach ($searchBlocks as $searchBlock) { |
||
165 | $properties = $searchBlock->getProperties(); |
||
166 | $properties['displaySearchBtn'] = (bool)$properties['displaySearchBtn']; |
||
167 | if (isset($properties['active'])) { |
||
168 | foreach ($properties['active'] as $module => $active) { |
||
169 | $properties['active'][$module] = (bool)$active; |
||
170 | } |
||
171 | } |
||
172 | $searchBlock->setProperties($properties); |
||
173 | } |
||
174 | $this->entityManager->flush(); |
||
175 | case '3.9.5': |
||
176 | $loginBlocks = $blockRepository->findBy(['blocktype' => 'Login']); |
||
177 | foreach ($loginBlocks as $loginBlock) { |
||
178 | $filters = $loginBlock->getFilters(); |
||
179 | $filters[] = [ |
||
180 | 'attribute' => '_route', |
||
181 | 'queryParameter' => null, |
||
182 | 'comparator' => '!=', |
||
183 | 'value' => 'zikulausersmodule_access_login' |
||
184 | ]; |
||
185 | $loginBlock->setFilters($filters); |
||
186 | } |
||
187 | $this->entityManager->flush(); |
||
188 | case '3.9.6': |
||
189 | $statement = $this->entityManager->getConnection()->executeQuery("SELECT * FROM blocks WHERE blocktype = 'Lang'"); |
||
190 | $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
191 | if (count($blocks) > 0) { |
||
192 | $this->entityManager->getConnection()->executeQuery("UPDATE blocks set bkey=?, blocktype=?, properties=? WHERE blocktype = 'Lang'", [ |
||
193 | 'ZikulaSettingsModule:Zikula\SettingsModule\Block\LocaleBlock', |
||
194 | 'Locale', |
||
195 | 'a:0:{}' |
||
196 | ]); |
||
197 | $this->addFlash('success', 'All instances of LangBlock have been converted to LocaleBlock.'); |
||
198 | } |
||
199 | $this->entityManager->getConnection()->executeQuery("UPDATE group_perms SET component = REPLACE(component, 'Languageblock', 'LocaleBlock') WHERE component LIKE 'Languageblock%'"); |
||
200 | case '3.9.7': |
||
201 | case '3.9.8': |
||
202 | $statement = $this->entityManager->getConnection()->executeQuery("SELECT * FROM blocks"); |
||
203 | $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
204 | foreach ($blocks as $block) { |
||
205 | $bKey = $block['bkey']; |
||
206 | if (mb_strpos($bKey, ':')) { |
||
207 | [/*$moduleName*/, $bKey] = explode(':', $bKey); |
||
208 | } |
||
209 | $this->entityManager->getConnection()->executeUpdate('UPDATE blocks SET bKey=? WHERE bid=?', [trim($bKey, '\\'), $block['bid']]); |
||
210 | } |
||
211 | case '3.9.9': |
||
212 | // future upgrade routines |
||
213 | } |
||
214 | |||
215 | // Update successful |
||
216 | return true; |
||
217 | } |
||
327 |