| Conditions | 6 |
| Paths | 6 |
| Total Lines | 66 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 149 | protected function doIt() |
||
| 150 | { |
||
| 151 | |||
| 152 | $request = Craft::$app->getRequest(); |
||
| 153 | |||
| 154 | if ($request->getIsAjax()) { |
||
| 155 | |||
| 156 | if (!$request->getIsPost()) { |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | $segments = $request->getSegments(); |
||
| 161 | $actionSegment = $segments[count($segments) - 1]; |
||
| 162 | |||
| 163 | if ($actionSegment !== 'get-editor-html') { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.initElementEditor();'); |
||
| 168 | |||
| 169 | } else { |
||
| 170 | |||
| 171 | $redirectUrl = \implode('?', \array_filter([\implode('/', $request->getSegments()), $request->getQueryStringWithoutPath()])); |
||
| 172 | |||
| 173 | $data = [ |
||
| 174 | 'fields' => [], |
||
| 175 | 'entryTypeIds' => [], |
||
| 176 | 'baseEditFieldUrl' => \rtrim(UrlHelper::cpUrl('settings/fields/edit'), '/'), |
||
| 177 | 'baseEditEntryTypeUrl' => \rtrim(UrlHelper::cpUrl('settings/sections/sectionId/entrytypes'), '/'), |
||
| 178 | 'baseEditGlobalSetUrl' => \rtrim(UrlHelper::cpUrl('settings/globals'), '/'), |
||
| 179 | 'baseEditCategoryGroupUrl' => \rtrim(UrlHelper::cpUrl('settings/categories'), '/'), |
||
| 180 | 'baseEditCommerceProductTypeUrl' => \rtrim(UrlHelper::cpUrl('commerce/settings/producttypes'), '/'), |
||
| 181 | 'redirectUrl' => Craft::$app->getSecurity()->hashData($redirectUrl), |
||
| 182 | ]; |
||
| 183 | |||
| 184 | $sectionIds = Craft::$app->getSections()->getAllSectionIds(); |
||
| 185 | foreach ($sectionIds as $sectionId) { |
||
| 186 | $entryTypes = Craft::$app->getSections()->getEntryTypesBySectionId($sectionId); |
||
| 187 | $data['entryTypeIds'][(string)$sectionId] = []; |
||
| 188 | foreach ($entryTypes as $entryType) { |
||
| 189 | $data['entryTypeIds'][(string)$sectionId][] = $entryType->id; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | // this will break the fields and plugins initialization |
||
| 195 | // https://github.com/craftcms/cms/issues/4944 |
||
| 196 | // https://github.com/mmikkel/CpFieldInspect-Craft/issues/11 |
||
| 197 | // $fields = Craft::$app->getFields()->getAllFields(); |
||
| 198 | |||
| 199 | // query for the fields myself because otherwise it will mess up the users field layout |
||
| 200 | $fields = (new Query()) |
||
| 201 | ->select([ |
||
| 202 | 'fields.id', |
||
| 203 | 'fields.handle', |
||
| 204 | ]) |
||
| 205 | ->from(['{{%fields}} fields']) |
||
| 206 | ->where(['context' => 'global']) |
||
| 207 | ->orderBy(['fields.name' => SORT_ASC, 'fields.handle' => SORT_ASC]) |
||
| 208 | ->all(); |
||
| 209 | |||
| 210 | |||
| 211 | $data['fields'] = ArrayHelper::index($fields, 'id'); |
||
| 212 | $view = Craft::$app->getView(); |
||
| 213 | $view->registerAssetBundle(CpFieldInspectBundle::class); |
||
| 214 | $view->registerJs('Craft.CpFieldInspectPlugin.init(' . \json_encode($data) . ');'); |
||
| 215 | } |
||
| 219 |