| Conditions | 11 |
| Paths | 14 |
| Total Lines | 54 |
| Code Lines | 26 |
| 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 |
||
| 131 | protected function relationField($block, $field, $key) { |
||
| 132 | // it’s an array of relations - request them if we’re auto or manual resolving |
||
| 133 | if (Str::isUuid($field[0])) { |
||
| 134 | if ($block->_autoResolveRelations || array_key_exists($key, $block->_resolveRelations) || in_array($key, $block->_resolveRelations, true)) { |
||
| 135 | |||
| 136 | // they’re passing a custom class |
||
| 137 | if (array_key_exists($key, $block->_resolveRelations)) { |
||
| 138 | $relations = collect($field)->transform(fn($relation) => $block->getRelation(new RequestStory(), $relation, $block->_resolveRelations[$key])); |
||
| 139 | } else { |
||
| 140 | $relations = collect($field)->transform(fn($relation) => $block->getRelation(new RequestStory(), $relation)); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($block->_filterRelations) { |
||
| 144 | $relations = $relations->filter(); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $relations; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | // has child items - single option, multi option and Blocks fields |
||
| 152 | if (is_array($field[0])) { |
||
| 153 | // resolved relationships - entire story is returned, we just want the content and a few meta items |
||
| 154 | if (array_key_exists('content', $field[0])) { |
||
| 155 | return collect($field)->transform(function ($relation) use ($block) { |
||
| 156 | $class = $block->getChildClassName('Block', $relation['content']['component']); |
||
| 157 | $relationClass = new $class($relation['content'], $block); |
||
| 158 | |||
| 159 | $relationClass->addMeta([ |
||
| 160 | 'name' => $relation['name'], |
||
| 161 | 'published_at' => $relation['published_at'], |
||
| 162 | 'full_slug' => $relation['full_slug'], |
||
| 163 | ]); |
||
| 164 | |||
| 165 | return $relationClass; |
||
| 166 | }); |
||
| 167 | } |
||
| 168 | |||
| 169 | // this field holds blocks! |
||
| 170 | if (array_key_exists('component', $field[0])) { |
||
| 171 | return collect($field)->transform(function ($childBlock) use ($block) { |
||
| 172 | $class = $block->getChildClassName('Block', $childBlock['component']); |
||
| 173 | |||
| 174 | return new $class($childBlock, $block); |
||
| 175 | }); |
||
| 176 | } |
||
| 177 | |||
| 178 | // multi assets |
||
| 179 | if (array_key_exists('filename', $field[0])) { |
||
| 180 | return new MultiAsset($field, $block); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return $field; |
||
| 185 | } |
||
| 213 |