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 |
||
109 | protected function relationField($block, $field, $key) { |
||
110 | // it’s an array of relations - request them if we’re auto or manual resolving |
||
111 | if (Str::isUuid($field[0])) { |
||
112 | if ($block->_autoResolveRelations || array_key_exists($key, $block->_resolveRelations) || in_array($key, $block->_resolveRelations, true)) { |
||
113 | |||
114 | // they’re passing a custom class |
||
115 | if (array_key_exists($key, $block->_resolveRelations)) { |
||
116 | $relations = collect($field)->transform(fn($relation) => $block->getRelation(new RequestStory(), $relation, $block->_resolveRelations[$key])); |
||
117 | } else { |
||
118 | $relations = collect($field)->transform(fn($relation) => $block->getRelation(new RequestStory(), $relation)); |
||
119 | } |
||
120 | |||
121 | if ($block->_filterRelations) { |
||
122 | $relations = $relations->filter(); |
||
123 | } |
||
124 | |||
125 | return $relations; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // has child items - single option, multi option and Blocks fields |
||
130 | if (is_array($field[0])) { |
||
131 | // resolved relationships - entire story is returned, we just want the content and a few meta items |
||
132 | if (array_key_exists('content', $field[0])) { |
||
133 | return collect($field)->transform(function ($relation) use ($block) { |
||
134 | $class = $block->getChildClassName('Block', $relation['content']['component']); |
||
135 | $relationClass = new $class($relation['content'], $block); |
||
136 | |||
137 | $relationClass->addMeta([ |
||
138 | 'name' => $relation['name'], |
||
139 | 'published_at' => $relation['published_at'], |
||
140 | 'full_slug' => $relation['full_slug'], |
||
141 | ]); |
||
142 | |||
143 | return $relationClass; |
||
144 | }); |
||
145 | } |
||
146 | |||
147 | // this field holds blocks! |
||
148 | if (array_key_exists('component', $field[0])) { |
||
149 | return collect($field)->transform(function ($childBlock) use ($block) { |
||
150 | $class = $block->getChildClassName('Block', $childBlock['component']); |
||
151 | |||
152 | return new $class($childBlock, $block); |
||
153 | }); |
||
154 | } |
||
155 | |||
156 | // multi assets |
||
157 | if (array_key_exists('filename', $field[0])) { |
||
158 | return new MultiAsset($field, $block); |
||
159 | } |
||
175 | } |