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 | 85 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 | $model = $this->setupRelatedModelLocale($model); |
||
122 | // when subfields are NOT set we don't need to get any more values |
||
123 | // we just return the plain models as we only need the ids |
||
124 | if (! isset($field['subfields'])) { |
||
125 | $result->push($model); |
||
126 | continue; |
||
127 | } |
||
128 | // when subfields are set we need to parse their values so they can be displayed |
||
129 | switch ($relationType) { |
||
130 | case 'HasMany': |
||
131 | case 'MorphMany': |
||
132 | // we will get model direct attributes and merge with subfields values. |
||
133 | $directAttributes = $this->getModelWithFakes($model)->getAttributes(); |
||
134 | $result->push(array_merge($directAttributes, $this->getSubfieldsValues($field['subfields'], $model))); |
||
135 | break; |
||
136 | |||
137 | case 'BelongsToMany': |
||
138 | case 'MorphToMany': |
||
139 | // for any given model, we grab the attributes that belong to our pivot table. |
||
140 | $item = $model->{$relation->getPivotAccessor()}->getAttributes(); |
||
141 | $item[$relationMethod] = $model->getKey(); |
||
142 | $result->push($item); |
||
143 | break; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | return $result; |
||
148 | break; |
||
149 | case 'HasOne': |
||
150 | case 'MorphOne': |
||
151 | if (! method_exists($relatedModel, $relationMethod)) { |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | $model = $relatedModel->{$relationMethod}; |
||
156 | |||
157 | if (! $model) { |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | $model = $this->setupRelatedModelLocale($model); |
||
162 | $model = $this->getModelWithFakes($model); |
||
163 | |||
164 | // if `entity` contains a dot here it means developer added a main HasOne/MorphOne relation with dot notation |
||
165 | if (Str::contains($field['entity'], '.')) { |
||
166 | return $model->{Str::afterLast($field['entity'], '.')}; |
||
167 | } |
||
168 | |||
169 | // when subfields exists developer used the repeatable interface to manage this relation |
||
170 | if ($field['subfields']) { |
||
171 | return [$this->getSubfieldsValues($field['subfields'], $model)]; |
||
172 | } |
||
173 | |||
174 | return $this->getModelWithFakes($model); |
||
175 | |||
176 | break; |
||
177 | case 'BelongsTo': |
||
178 | if ($relatedModel->{$relationMethod}) { |
||
179 | return $relatedModel->{$relationMethod}->getKey(); |
||
180 | } |
||
181 | |||
182 | return $relatedModel->{$relationMethod}; |
||
183 | break; |
||
184 | default: |
||
185 | return $relatedModel->{$relationMethod}; |
||
186 | } |
||
299 |