We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 24 |
| Paths | 70 |
| Total Lines | 94 |
| Code Lines | 60 |
| 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 |
||
| 109 | private function getModelAttributeValueFromRelationship($model, $field) |
||
| 110 | { |
||
| 111 | [$relatedModel, $relationMethod] = $this->getModelAndMethodFromEntity($model, $field); |
||
| 112 | |||
| 113 | if (! method_exists($relatedModel, $relationMethod)) { |
||
| 114 | return $relatedModel->{$relationMethod}; |
||
| 115 | } |
||
| 116 | |||
| 117 | $relation = $relatedModel->{$relationMethod}(); |
||
| 118 | $relationType = Str::afterLast(get_class($relation), '\\'); |
||
| 119 | |||
| 120 | switch ($relationType) { |
||
| 121 | case 'MorphMany': |
||
| 122 | case 'HasMany': |
||
| 123 | case 'BelongsToMany': |
||
| 124 | case 'MorphToMany': |
||
| 125 | if( |
||
| 126 | ($relationType == 'BelongsToMany' || $relationType == 'MorphToMany') && |
||
| 127 | $relatedModel->{$relationMethod}->count() > 0 && |
||
| 128 | $relatedModel->{$relationMethod}[0]->{$relatedModel->{$relationMethod}()->getRelatedPivotKeyName()}){ |
||
| 129 | $key = $relatedModel->{$relationMethod}[0]->{$relatedModel->{$relationMethod}()->getRelatedPivotKeyName()}->getKeyName(); |
||
| 130 | $relationModels = $relatedModel->{$relationMethod}()->withPivot($key)->get(); |
||
| 131 | } else { |
||
| 132 | $relationModels = $relatedModel->{$relationMethod}; |
||
| 133 | } |
||
| 134 | $result = collect(); |
||
| 135 | |||
| 136 | foreach ($relationModels as $model) { |
||
| 137 | $model = $this->setupRelatedModelLocale($model); |
||
| 138 | // when subfields are NOT set we don't need to get any more values |
||
| 139 | // we just return the plain models as we only need the ids |
||
| 140 | if (! isset($field['subfields'])) { |
||
| 141 | $result->push($model); |
||
| 142 | |||
| 143 | continue; |
||
| 144 | } |
||
| 145 | // when subfields are set we need to parse their values so they can be displayed |
||
| 146 | switch ($relationType) { |
||
| 147 | case 'HasMany': |
||
| 148 | case 'MorphMany': |
||
| 149 | // we will get model direct attributes and merge with subfields values. |
||
| 150 | $directAttributes = $this->getModelWithFakes($model)->getAttributes(); |
||
| 151 | $result->push(array_merge($directAttributes, $this->getSubfieldsValues($field['subfields'], $model))); |
||
| 152 | break; |
||
| 153 | |||
| 154 | case 'BelongsToMany': |
||
| 155 | case 'MorphToMany': |
||
| 156 | // for any given model, we grab the attributes that belong to our pivot table. |
||
| 157 | $item = $model->{$relation->getPivotAccessor()}->getAttributes(); |
||
| 158 | $item[$relationMethod] = $model->getKey(); |
||
| 159 | $result->push($item); |
||
| 160 | break; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $result; |
||
| 165 | break; |
||
| 166 | case 'HasOne': |
||
| 167 | case 'MorphOne': |
||
| 168 | if (! method_exists($relatedModel, $relationMethod)) { |
||
| 169 | return; |
||
| 170 | } |
||
| 171 | |||
| 172 | $model = $relatedModel->{$relationMethod}; |
||
| 173 | |||
| 174 | if (! $model) { |
||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | $model = $this->setupRelatedModelLocale($model); |
||
| 179 | $model = $this->getModelWithFakes($model); |
||
| 180 | |||
| 181 | // if `entity` contains a dot here it means developer added a main HasOne/MorphOne relation with dot notation |
||
| 182 | if (Str::contains($field['entity'], '.')) { |
||
| 183 | return $model->{Str::afterLast($field['entity'], '.')}; |
||
| 184 | } |
||
| 185 | |||
| 186 | // when subfields exists developer used the repeatable interface to manage this relation |
||
| 187 | if (isset($field['subfields'])) { |
||
| 188 | return [$this->getSubfieldsValues($field['subfields'], $model)]; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->getModelWithFakes($model); |
||
| 192 | |||
| 193 | break; |
||
| 194 | case 'BelongsTo': |
||
| 195 | if ($relatedModel->{$relationMethod}) { |
||
| 196 | return $relatedModel->{$relationMethod}->getKey(); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $relatedModel->{$relationMethod}; |
||
| 200 | break; |
||
| 201 | default: |
||
| 202 | return $relatedModel->{$relationMethod}; |
||
| 203 | } |
||
| 311 |