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 | 195 |
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 |
||
7 | trait Relationships |
||
8 | { |
||
9 | /** |
||
10 | * From the field entity we get the relation instance. |
||
11 | * |
||
12 | * @param array $entity |
||
13 | * @return object |
||
14 | */ |
||
15 | public function getRelationInstance($field) |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get the fields with specific relation types. |
||
49 | * |
||
50 | * @param array|string $relation_types |
||
51 | * |
||
52 | * @return array The fields with corresponding relation types. |
||
53 | */ |
||
54 | public function getFieldsWithRelationType($relation_types): array |
||
55 | { |
||
56 | $relation_types = is_array($relation_types) ?: (array) $relation_types; |
||
57 | |||
58 | return collect($this->fields()) |
||
59 | ->where('model') |
||
60 | ->whereIn('relation_type', $relation_types) |
||
61 | ->toArray(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Grabs an relation instance and returns the class name of the related model. |
||
66 | * |
||
67 | * @param array $field |
||
68 | * @return string |
||
69 | */ |
||
70 | public function inferFieldModelFromRelationship($field) |
||
71 | { |
||
72 | $relation = $this->getRelationInstance($field); |
||
73 | |||
74 | return get_class($relation->getRelated()); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Return the relation type from a given field: BelongsTo, HasOne ... etc. |
||
79 | * |
||
80 | * @param array $field |
||
81 | * @return string |
||
82 | */ |
||
83 | public function inferRelationTypeFromRelationship($field) |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Parse the field name back to the related entity after the form is submited. |
||
92 | * Its called in getAllFieldNames(). |
||
93 | * |
||
94 | * @param array $fields |
||
95 | * @return array |
||
96 | */ |
||
97 | public function parseRelationFieldNamesFromHtml($fields) |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Based on relation type returns the default field type. |
||
118 | * |
||
119 | * @param string $relation_type |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function inferFieldTypeFromFieldRelation($field) |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Based on relation type returns if relation allows multiple entities. |
||
140 | * |
||
141 | * @param string $relation_type |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function guessIfFieldHasMultipleFromRelationType($relation_type) |
||
145 | { |
||
146 | switch ($relation_type) { |
||
147 | case 'BelongsToMany': |
||
148 | case 'HasMany': |
||
149 | case 'HasManyThrough': |
||
150 | case 'HasOneOrMany': |
||
151 | case 'MorphMany': |
||
152 | case 'MorphOneOrMany': |
||
153 | case 'MorphToMany': |
||
154 | return true; |
||
155 | |||
156 | default: |
||
157 | return false; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Based on relation type returns if relation has a pivot table. |
||
163 | * |
||
164 | * @param string $relation_type |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function guessIfFieldHasPivotFromRelationType($relation_type) |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Associate and dissociate the BelongsTo relations in primary model |
||
183 | * |
||
184 | * @param Model |
||
185 | * @param array The form data. |
||
186 | * @return Model Model with relationships set up. |
||
187 | */ |
||
188 | public function associateBelongsToRelations($item, array $data) |
||
206 |