We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 42 |
Total Lines | 210 |
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) |
||
18 | { |
||
19 | $entity = $this->getOnlyRelationEntity($field); |
||
|
|||
20 | $entity_array = explode('.', $entity); |
||
21 | $relation_model = $this->getRelationModel($entity); |
||
22 | |||
23 | $related_method = Arr::last($entity_array); |
||
24 | if (count(explode('.', $entity)) == count(explode('.', $field['entity']))) { |
||
25 | $relation_model = $this->getRelationModel($entity, -1); |
||
26 | } |
||
27 | $relation_model = new $relation_model(); |
||
28 | |||
29 | //if counts are diferent means that last element of entity is the field in relation. |
||
30 | if (count(explode('.', $entity)) != count(explode('.', $field['entity']))) { |
||
31 | if (in_array($related_method, $relation_model->getFillable())) { |
||
32 | if (count($entity_array) > 1) { |
||
33 | $related_method = $entity_array[(count($entity_array) - 2)]; |
||
34 | $relation_model = $this->getRelationModel($entity, -2); |
||
35 | } else { |
||
36 | $relation_model = $this->model; |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | if (count($entity_array) == 1) { |
||
41 | if (method_exists($this->model, $related_method)) { |
||
42 | return $this->model->{$related_method}(); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | return $relation_model->{$related_method}(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get the fields with specific relation types that are not nested relations. |
||
51 | * |
||
52 | * @param array|string $relation_types |
||
53 | * |
||
54 | * @return array The fields with corresponding relation types. |
||
55 | */ |
||
56 | public function getFieldsWithRelationType($relation_types): array |
||
57 | { |
||
58 | $relation_types = is_array($relation_types) ?: (array) $relation_types; |
||
59 | |||
60 | return collect($this->fields()) |
||
61 | ->where('model') |
||
62 | ->whereIn('relation_type', $relation_types) |
||
63 | ->filter(function ($item) { |
||
64 | return Str::contains($item['entity'], '.') && $item['model'] !== get_class($this->model->{Arr::first(explode('.', $item['entity']))}()->getRelated()) ? false : true; |
||
65 | }) |
||
66 | ->toArray(); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Grabs an relation instance and returns the class name of the related model. |
||
71 | * |
||
72 | * @param array $field |
||
73 | * @return string |
||
74 | */ |
||
75 | public function inferFieldModelFromRelationship($field) |
||
76 | { |
||
77 | $relation = $this->getRelationInstance($field); |
||
78 | |||
79 | return get_class($relation->getRelated()); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Return the relation type from a given field: BelongsTo, HasOne ... etc. |
||
84 | * |
||
85 | * @param array $field |
||
86 | * @return string |
||
87 | */ |
||
88 | public function inferRelationTypeFromRelationship($field) |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Parse the field name back to the related entity after the form is submited. |
||
97 | * Its called in getAllFieldNames(). |
||
98 | * |
||
99 | * @param array $fields |
||
100 | * @return array |
||
101 | */ |
||
102 | public function parseRelationFieldNamesFromHtml($fields) |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Based on relation type returns the default field type. |
||
123 | * |
||
124 | * @param string $relation_type |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function inferFieldTypeFromFieldRelation($field) |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Based on relation type returns if relation allows multiple entities. |
||
145 | * |
||
146 | * @param string $relation_type |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function guessIfFieldHasMultipleFromRelationType($relation_type) |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Based on relation type returns if relation has a pivot table. |
||
168 | * |
||
169 | * @param string $relation_type |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function guessIfFieldHasPivotFromRelationType($relation_type) |
||
173 | { |
||
174 | switch ($relation_type) { |
||
175 | case 'BelongsToMany': |
||
176 | case 'MorphToMany': |
||
177 | return true; |
||
178 | default: |
||
179 | return false; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Check if field name contains a dot, if so, meaning it's a nested relation. |
||
185 | * |
||
186 | * @param array $field |
||
187 | * @return bool |
||
188 | */ |
||
189 | protected function isNestedRelation($field): bool |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Associate and dissociate BelongsTo relations in the model. |
||
200 | * |
||
201 | * @param Model |
||
202 | * @param array The form data. |
||
203 | * @return Model Model with relationships set up. |
||
204 | */ |
||
205 | public function associateOrDissociateBelongsToRelations($item, array $data) |
||
219 | } |
||
220 | } |
||
221 |