Conditions | 18 |
Paths | 18 |
Total Lines | 81 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
50 | public function arrayField($block, $field, $key) { |
||
51 | // match link fields |
||
52 | if (array_key_exists('linktype', $field)) { |
||
53 | $class = 'Riclep\Storyblok\Fields\\' . Str::studly($field['linktype']) . 'Link'; |
||
54 | |||
55 | return new $class($field, $block); |
||
56 | } |
||
57 | |||
58 | // match rich-text fields |
||
59 | if (array_key_exists('type', $field) && $field['type'] === 'doc') { |
||
60 | return new RichText($field, $block); |
||
61 | } |
||
62 | |||
63 | // match asset fields - detecting raster images |
||
64 | if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'asset') { |
||
65 | |||
66 | // legacy image fields |
||
67 | if($this->isLegacyImageField($field['filename'])) { |
||
68 | return new Image($field, $block); |
||
69 | } |
||
70 | |||
71 | return new Asset($field, $block); |
||
72 | } |
||
73 | |||
74 | // match table fields |
||
75 | if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'table') { |
||
76 | return new Table($field, $block); |
||
77 | } |
||
78 | |||
79 | if (array_key_exists(0, $field)) { |
||
80 | // it’s an array of relations - request them if we’re auto or manual resolving |
||
81 | if (Str::isUuid($field[0])) { |
||
82 | if ($block->_autoResolveRelations || in_array($key, $block->_resolveRelations)) { |
||
83 | $relations = collect($field)->transform(function ($relation) use ($block) { |
||
84 | return $block->getRelation(new RequestStory(), $relation); |
||
85 | }); |
||
86 | |||
87 | if ($block->_filterRelations) { |
||
88 | $relations = $relations->filter(); |
||
89 | } |
||
90 | |||
91 | return $relations; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // has child items - single option, multi option and Blocks fields |
||
96 | if (is_array($field[0])) { |
||
97 | // resolved relationships - entire story is returned, we just want the content and a few meta items |
||
98 | if (array_key_exists('content', $field[0])) { |
||
99 | return collect($field)->transform(function ($relation) use ($block) { |
||
100 | $class = $block->getChildClassName('Block', $relation['content']['component']); |
||
101 | $relationClass = new $class($relation['content'], $block); |
||
102 | |||
103 | $relationClass->addMeta([ |
||
104 | 'name' => $relation['name'], |
||
105 | 'published_at' => $relation['published_at'], |
||
106 | 'full_slug' => $relation['full_slug'], |
||
107 | ]); |
||
108 | |||
109 | return $relationClass; |
||
110 | }); |
||
111 | } |
||
112 | |||
113 | // this field holds blocks! |
||
114 | if (array_key_exists('component', $field[0])) { |
||
115 | return collect($field)->transform(function ($childBlock) use ($block) { |
||
116 | $class = $block->getChildClassName('Block', $childBlock['component']); |
||
117 | |||
118 | return new $class($childBlock, $block); |
||
119 | }); |
||
120 | } |
||
121 | |||
122 | // multi assets |
||
123 | if (array_key_exists('filename', $field[0])) { |
||
124 | return new MultiAsset($field, $block); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // just return the array |
||
130 | return $field; |
||
131 | } |
||
146 | } |