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