Conditions | 16 |
Paths | 776 |
Total Lines | 93 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
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 | public function getFormFieldsForRepeater($object, $fields, $relation, $model = null, $repeaterName = null) |
||
127 | { |
||
128 | if (!$repeaterName) { |
||
129 | $repeaterName = $relation; |
||
130 | } |
||
131 | |||
132 | $repeaters = []; |
||
133 | $repeatersFields = []; |
||
134 | $repeatersBrowsers = []; |
||
135 | $repeatersMedias = []; |
||
136 | $repeatersFiles = []; |
||
137 | $relationRepository = $this->getModelRepository($relation, $model); |
||
138 | $repeatersConfig = config('twill.block_editor.repeaters'); |
||
139 | |||
140 | foreach ($object->$relation as $relationItem) { |
||
141 | $repeaters[] = [ |
||
142 | 'id' => $relation . '-' . $relationItem->id, |
||
143 | 'type' => $repeatersConfig[$repeaterName]['component'], |
||
144 | 'title' => $repeatersConfig[$repeaterName]['title'], |
||
145 | ]; |
||
146 | |||
147 | $relatedItemFormFields = $relationRepository->getFormFields($relationItem); |
||
148 | $translatedFields = []; |
||
149 | |||
150 | if (isset($relatedItemFormFields['translations'])) { |
||
151 | foreach ($relatedItemFormFields['translations'] as $key => $values) { |
||
152 | $repeatersFields[] = [ |
||
153 | 'name' => "blocks[$relation-$relationItem->id][$key]", |
||
154 | 'value' => $values, |
||
155 | ]; |
||
156 | |||
157 | $translatedFields[] = $key; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | if (isset($relatedItemFormFields['medias'])) { |
||
162 | if (config('twill.media_library.translated_form_fields', false)) { |
||
163 | Collection::make($relatedItemFormFields['medias'])->each(function ($rolesWithMedias, $locale) use (&$repeatersMedias, $relation, $relationItem) { |
||
164 | $repeatersMedias[] = Collection::make($rolesWithMedias)->mapWithKeys(function ($medias, $role) use ($locale, $relation, $relationItem) { |
||
165 | return [ |
||
166 | "blocks[$relation-$relationItem->id][$role][$locale]" => $medias, |
||
167 | ]; |
||
168 | })->toArray(); |
||
169 | }); |
||
170 | } else { |
||
171 | foreach ($relatedItemFormFields['medias'] as $key => $values) { |
||
172 | $repeatersMedias["blocks[$relation-$relationItem->id][$key]"] = $values; |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | if (isset($relatedItemFormFields['files'])) { |
||
178 | Collection::make($relatedItemFormFields['files'])->each(function ($rolesWithFiles, $locale) use (&$repeatersFiles, $relation, $relationItem) { |
||
179 | $repeatersFiles[] = Collection::make($rolesWithFiles)->mapWithKeys(function ($files, $role) use ($locale, $relation, $relationItem) { |
||
180 | return [ |
||
181 | "blocks[$relation-$relationItem->id][$role][$locale]" => $files, |
||
182 | ]; |
||
183 | })->toArray(); |
||
184 | }); |
||
185 | } |
||
186 | |||
187 | if (isset($relatedItemFormFields['browsers'])) { |
||
188 | foreach ($relatedItemFormFields['browsers'] as $key => $values) { |
||
189 | $repeatersBrowsers["blocks[$relation-$relationItem->id][$key]"] = $values; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | $itemFields = method_exists($relationItem, 'toRepeaterArray') ? $relationItem->toRepeaterArray() : Arr::except($relationItem->attributesToArray(), $translatedFields); |
||
194 | |||
195 | foreach ($itemFields as $key => $value) { |
||
196 | $repeatersFields[] = [ |
||
197 | 'name' => "blocks[$relation-$relationItem->id][$key]", |
||
198 | 'value' => $value, |
||
199 | ]; |
||
200 | } |
||
201 | |||
202 | } |
||
203 | |||
204 | if (!empty($repeatersMedias) && config('twill.media_library.translated_form_fields', false)) { |
||
205 | $repeatersMedias = call_user_func_array('array_merge', $repeatersMedias); |
||
206 | } |
||
207 | |||
208 | if (!empty($repeatersFiles)) { |
||
209 | $repeatersFiles = call_user_func_array('array_merge', $repeatersFiles); |
||
210 | } |
||
211 | |||
212 | $fields['repeaters'][$repeaterName] = $repeaters; |
||
213 | $fields['repeaterFields'][$repeaterName] = $repeatersFields; |
||
214 | $fields['repeaterMedias'][$repeaterName] = $repeatersMedias; |
||
215 | $fields['repeaterFiles'][$repeaterName] = $repeatersFiles; |
||
216 | $fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers; |
||
217 | |||
218 | return $fields; |
||
219 | } |
||
221 |