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