We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 16 |
| Paths | 16 |
| Total Lines | 56 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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 |
||
| 95 | private function getModelAttributeValue($model, $field) |
||
| 96 | { |
||
| 97 | if (isset($field['entity']) && $field['entity'] !== false) { |
||
| 98 | $relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
||
| 99 | |||
| 100 | $relation_array = explode('.', $relational_entity); |
||
| 101 | |||
| 102 | $relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) { |
||
| 103 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
| 104 | }, $model); |
||
| 105 | |||
| 106 | $relationMethod = Arr::last($relation_array); |
||
| 107 | if (method_exists($relatedModel, $relationMethod)) { |
||
| 108 | $relation = $relatedModel->{$relationMethod}(); |
||
| 109 | $relation_type = get_class($relation); |
||
| 110 | |||
| 111 | switch ($relation_type) { |
||
| 112 | case HasOne::class: |
||
| 113 | case MorphOne::class: |
||
| 114 | return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))}; |
||
| 115 | |||
| 116 | case HasMany::class: |
||
| 117 | case MorphMany::class: |
||
| 118 | |||
| 119 | $attribute_value = $this->getManyRelationAttributeValue($relatedModel, $relationMethod, $field, $relation_type); |
||
| 120 | // we only want to return the json_encoded values here |
||
| 121 | if (is_string($attribute_value)) { |
||
| 122 | return $attribute_value; |
||
| 123 | } |
||
| 124 | |||
| 125 | break; |
||
| 126 | case BelongsToMany::class: |
||
| 127 | case MorphToMany::class: |
||
| 128 | $attribute_value = $this->getManyRelationAttributeValue($relatedModel, $relationMethod, $field, $relation_type); |
||
| 129 | // we only want to return the json_encoded values here |
||
| 130 | if (is_string($attribute_value)) { |
||
| 131 | return $attribute_value; |
||
| 132 | } |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return $relatedModel->{$relationMethod}; |
||
| 138 | } |
||
| 139 | |||
| 140 | if (is_string($field['name'])) { |
||
| 141 | return $model->{$field['name']}; |
||
| 142 | } |
||
| 143 | |||
| 144 | if (is_array($field['name'])) { |
||
| 145 | $result = []; |
||
| 146 | foreach ($field['name'] as $key => $value) { |
||
| 147 | $result = $model->{$value}; |
||
| 148 | } |
||
| 149 | |||
| 150 | return $result; |
||
| 151 | } |
||
| 202 |