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