We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 41 |
Total Lines | 217 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Complex classes like Relationships often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Relationships, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait Relationships |
||
10 | { |
||
11 | /** |
||
12 | * From the field entity we get the relation instance. |
||
13 | * |
||
14 | * @param array $entity |
||
15 | * @return object |
||
16 | */ |
||
17 | public function getRelationInstance($field) |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get the fields for relationships, according to the relation type. It looks only for direct |
||
51 | * relations - it will NOT look through relationships of relationships. |
||
52 | * |
||
53 | * @param string|array $relation_types Eloquent relation class or array of Eloquent relation classes. Eg: BelongsTo |
||
54 | * |
||
55 | * @return array The fields with corresponding relation types. |
||
56 | */ |
||
57 | public function getFieldsWithRelationType($relation_types): array |
||
58 | { |
||
59 | $relation_types = (array) $relation_types; |
||
60 | |||
61 | return collect($this->fields()) |
||
62 | ->where('model') |
||
63 | ->whereIn('relation_type', $relation_types) |
||
64 | ->filter(function ($item) { |
||
65 | $related_model = get_class($this->model->{Arr::first(explode('.', $item['entity']))}()->getRelated()); |
||
66 | |||
67 | return Str::contains($item['entity'], '.') && $item['model'] !== $related_model ? false : true; |
||
68 | }) |
||
69 | ->toArray(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Grabs an relation instance and returns the class name of the related model. |
||
74 | * |
||
75 | * @param array $field |
||
76 | * @return string |
||
77 | */ |
||
78 | public function inferFieldModelFromRelationship($field) |
||
79 | { |
||
80 | $relation = $this->getRelationInstance($field); |
||
81 | |||
82 | return get_class($relation->getRelated()); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Return the relation type from a given field: BelongsTo, HasOne ... etc. |
||
87 | * |
||
88 | * @param array $field |
||
89 | * @return string |
||
90 | */ |
||
91 | public function inferRelationTypeFromRelationship($field) |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Parse the field name back to the related entity after the form is submited. |
||
100 | * Its called in getAllFieldNames(). |
||
101 | * |
||
102 | * @param array $fields |
||
103 | * @return array |
||
104 | */ |
||
105 | public function parseRelationFieldNamesFromHtml($fields) |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Based on relation type returns the default field type. |
||
126 | * |
||
127 | * @param string $relation_type |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function inferFieldTypeFromFieldRelation($field) |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Based on relation type returns if relation allows multiple entities. |
||
149 | * |
||
150 | * @param string $relation_type |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function guessIfFieldHasMultipleFromRelationType($relation_type) |
||
154 | { |
||
155 | switch ($relation_type) { |
||
156 | case 'BelongsToMany': |
||
157 | case 'HasMany': |
||
158 | case 'HasManyThrough': |
||
159 | case 'HasOneOrMany': |
||
160 | case 'MorphMany': |
||
161 | case 'MorphOneOrMany': |
||
162 | case 'MorphToMany': |
||
163 | return true; |
||
164 | break; |
||
165 | default: |
||
166 | return false; |
||
167 | break; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Based on relation type returns if relation has a pivot table. |
||
173 | * |
||
174 | * @param string $relation_type |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function guessIfFieldHasPivotFromRelationType($relation_type) |
||
178 | { |
||
179 | switch ($relation_type) { |
||
180 | case 'BelongsToMany': |
||
181 | case 'MorphToMany': |
||
182 | return true; |
||
183 | break; |
||
184 | default: |
||
185 | return false; |
||
186 | break; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Check if field name contains a dot, if so, meaning it's a nested relation. |
||
192 | * |
||
193 | * @param array $field |
||
194 | * @return bool |
||
195 | */ |
||
196 | protected function isNestedRelation($field): bool |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Associate and dissociate BelongsTo relations in the model. |
||
207 | * |
||
208 | * @param Model |
||
209 | * @param array The form data. |
||
210 | * @return Model Model with relationships set up. |
||
211 | */ |
||
212 | public function associateOrDissociateBelongsToRelations($item, array $data) |
||
226 | } |
||
227 | } |
||
228 |