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