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