| Conditions | 9 |
| Paths | 18 |
| Total Lines | 53 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 |
||
| 54 | public function actionIndex() |
||
| 55 | { |
||
| 56 | $result = []; |
||
| 57 | $variablesList = []; |
||
| 58 | |||
| 59 | // Get the global set to use |
||
| 60 | $settings = RichVariables::$plugin->getSettings(); |
||
| 61 | $globalsSet = Craft::$app->getGlobals()->getSetByHandle($settings['globalSetHandle']); |
||
| 62 | // Grab the first global set if they haven't specified one yet |
||
| 63 | if (!$globalsSet) { |
||
| 64 | $allGlobalsSetIds = Craft::$app->getGlobals()->getAllSetIds(); |
||
| 65 | if (!empty($allGlobalsSetIds)) { |
||
| 66 | $globalsSet = Craft::$app->globals->getSetById($allGlobalsSetIds[0]); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | if ($globalsSet) { |
||
| 70 | // Get the field layout fields used for this global set |
||
| 71 | $layout = $globalsSet->getFieldLayout(); |
||
| 72 | if ($layout) { |
||
| 73 | $fieldLayoutFields = $layout->getFields(); |
||
| 74 | /** @var Field $field */ |
||
| 75 | foreach ($fieldLayoutFields as $field) { |
||
| 76 | foreach (self::VALID_FIELD_CLASSES as $fieldClass) { |
||
| 77 | if ($field instanceof $fieldClass) { |
||
| 78 | // Add the field title and Reference Tag as per https://craftcms.com/docs/reference-tags |
||
| 79 | $thisVar = [ |
||
| 80 | 'title' => $field->name, |
||
| 81 | 'text' => '{globalset:' . $globalsSet->attributes['id'] . ':' . $field->handle . '}', |
||
| 82 | ]; |
||
| 83 | $variablesList[] = $thisVar; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | // Get the URL to our menu icon from our resource bundle |
||
| 91 | try { |
||
| 92 | Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class); |
||
| 93 | } catch (InvalidConfigException $e) { |
||
| 94 | Craft::error($e->getMessage(), __METHOD__); |
||
| 95 | } |
||
| 96 | $menuIconUrl = Craft::$app->assetManager->getPublishedUrl( |
||
| 97 | '@nystudio107/richvariables/web/assets/dist', |
||
| 98 | true |
||
| 99 | ) . '/img/RichVariables-menu-icon.svg'; |
||
| 100 | |||
| 101 | // Return everything to our JavaScript encoded as JSON |
||
| 102 | $result['variablesList'] = $variablesList; |
||
| 103 | $result['menuIconUrl'] = $menuIconUrl; |
||
| 104 | $result['useIconForMenu'] = $settings['useIconForMenu']; |
||
| 105 | |||
| 106 | return Json::encode($result); |
||
| 107 | } |
||
| 109 |