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