| Conditions | 12 |
| Paths | 58 |
| Total Lines | 49 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 109 | public function generateCompleteItems(): void |
||
| 110 | { |
||
| 111 | if ($this->sectionId !== null) { |
||
| 112 | // A sectionId of 0 denotes we don't know what this section is, so use |
||
| 113 | // a generic `Entry` and don't generate any complete items for custom fields |
||
| 114 | if ($this->sectionId === 0) { |
||
| 115 | $element = new Entry(); |
||
| 116 | $this->parseObject('', $element, 0); |
||
| 117 | $this->addMagicGetterProperties($element); |
||
| 118 | |||
| 119 | return; |
||
| 120 | } |
||
| 121 | $sections = null; |
||
| 122 | // getSections() is used for Craft 3 & 4 |
||
| 123 | if (method_exists(Craft::$app, 'getSections')) { |
||
| 124 | $sections = Craft::$app->getSections(); |
||
| 125 | } |
||
| 126 | // getEntries() is used for Craft 5 |
||
| 127 | if (method_exists(Craft::$app, 'getEntries')) { |
||
| 128 | $sections = Craft::$app->getEntries(); |
||
| 129 | } |
||
| 130 | // Find the entry types used in the passed in sectionId |
||
| 131 | if ($sections && $section = $sections->getSectionById($this->sectionId)) { |
||
| 132 | $entryTypes = $section->getEntryTypes(); |
||
| 133 | foreach ($entryTypes as $entryType) { |
||
| 134 | // Add the native fields in |
||
| 135 | if ($entryType->elementType) { |
||
| 136 | $element = new $entryType->elementType(); |
||
| 137 | /* @var ElementInterface $element */ |
||
| 138 | $this->parseObject('', $element, 0); |
||
| 139 | $this->addMagicGetterProperties($element); |
||
| 140 | } |
||
| 141 | // Add the custom fields in |
||
| 142 | if (version_compare(Craft::$app->getVersion(), '4.0', '>=')) { |
||
| 143 | $customFields = $entryType->getCustomFields(); |
||
| 144 | } else { |
||
| 145 | $customFields = $entryType->getFields(); |
||
| 146 | } |
||
| 147 | foreach ($customFields as $customField) { |
||
| 148 | $name = $customField->handle; |
||
| 149 | $docs = $customField->instructions ?? ''; |
||
| 150 | if ($name) { |
||
| 151 | CompleteItem::create() |
||
| 152 | ->insertText($name) |
||
| 153 | ->label($name) |
||
| 154 | ->detail(Craft::t('codeeditor', 'Custom Field Shorthand')) |
||
| 155 | ->documentation($docs) |
||
| 156 | ->kind(CompleteItemKind::FieldKind) |
||
| 157 | ->add($this); |
||
| 158 | } |
||
| 188 |