Total Complexity | 47 |
Total Lines | 289 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like MetadataRelationsTrait 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 MetadataRelationsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | trait MetadataRelationsTrait |
||
20 | { |
||
21 | use MetadataKeyMethodNamesTrait; |
||
22 | |||
23 | protected static $relationHooks = []; |
||
24 | protected static $relationCategories = []; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Get model's relationships. |
||
29 | * |
||
30 | * @throws InvalidOperationException |
||
31 | * @throws \ReflectionException |
||
32 | * @return array |
||
33 | */ |
||
34 | public function getRelationships() |
||
35 | { |
||
36 | if (empty(static::$relationHooks)) { |
||
37 | $hooks = []; |
||
38 | |||
39 | $rels = $this->getRelationshipsFromMethods(true); |
||
40 | |||
41 | $this->getRelationshipsUnknownPolyMorph($rels, $hooks); |
||
42 | |||
43 | $this->getRelationshipsKnownPolyMorph($rels, $hooks); |
||
44 | |||
45 | $this->getRelationshipsHasOne($rels, $hooks); |
||
46 | |||
47 | $this->getRelationshipsHasMany($rels, $hooks); |
||
48 | |||
49 | static::$relationHooks = $hooks; |
||
50 | } |
||
51 | |||
52 | return static::$relationHooks; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Is this model the known side of at least one polymorphic relation? |
||
57 | * |
||
58 | * @throws InvalidOperationException |
||
59 | * @throws \ReflectionException |
||
60 | */ |
||
61 | public function isKnownPolymorphSide() |
||
62 | { |
||
63 | // isKnownPolymorph needs to be checking KnownPolymorphSide results - if you're checking UnknownPolymorphSide, |
||
64 | // you're turned around |
||
65 | $rels = $this->getRelationshipsFromMethods(); |
||
66 | return !empty($rels['KnownPolyMorphSide']); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Is this model on the unknown side of at least one polymorphic relation? |
||
71 | * |
||
72 | * @throws InvalidOperationException |
||
73 | * @throws \ReflectionException |
||
74 | */ |
||
75 | public function isUnknownPolymorphSide() |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param bool $biDir |
||
85 | * |
||
86 | * @throws InvalidOperationException |
||
87 | * @throws \ReflectionException |
||
88 | * @return array |
||
89 | */ |
||
90 | protected function getRelationshipsFromMethods(bool $biDir = false) |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param array $rels |
||
172 | * @param array $hooks |
||
173 | * @throws InvalidOperationException |
||
174 | */ |
||
175 | protected function getRelationshipsHasMany(array $rels, array &$hooks) |
||
176 | { |
||
177 | /** |
||
178 | * @var string $property |
||
179 | * @var Relation $relation |
||
180 | */ |
||
181 | foreach ($rels['HasMany'] as $property => $relation) { |
||
182 | if ($relation instanceof MorphMany || $relation instanceof MorphToMany) { |
||
183 | continue; |
||
184 | } |
||
185 | $mult = '*'; |
||
186 | $targ = get_class($relation->getRelated()); |
||
187 | $keyName = $this->polyglotFkKey($relation); |
||
188 | $localName = $this->polyglotRkKey($relation); |
||
189 | $thruName = $relation instanceof HasManyThrough ? |
||
190 | $this->polyglotThroughKey($relation) : |
||
191 | null; |
||
192 | |||
193 | $first = $keyName; |
||
194 | $last = $localName; |
||
195 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, null, $thruName); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param array $rels |
||
201 | * @param array $hooks |
||
202 | * @throws InvalidOperationException |
||
203 | */ |
||
204 | protected function getRelationshipsHasOne(array $rels, array &$hooks) |
||
205 | { |
||
206 | /** |
||
207 | * @var string $property |
||
208 | * @var Relation $foo |
||
209 | */ |
||
210 | foreach ($rels['HasOne'] as $property => $foo) { |
||
211 | if ($foo instanceof MorphOne) { |
||
212 | continue; |
||
213 | } |
||
214 | $isBelong = $foo instanceof BelongsTo; |
||
215 | $mult = $isBelong ? '1' : '0..1'; |
||
216 | $targ = get_class($foo->getRelated()); |
||
217 | |||
218 | $keyName = $this->polyglotFkKey($foo); |
||
219 | $localName = $this->polyglotRkKey($foo); |
||
220 | $first = $isBelong ? $localName : $keyName; |
||
221 | $last = $isBelong ? $keyName : $localName; |
||
222 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param array $rels |
||
228 | * @param array $hooks |
||
229 | * @throws InvalidOperationException |
||
230 | */ |
||
231 | protected function getRelationshipsKnownPolyMorph(array $rels, array &$hooks) |
||
232 | { |
||
233 | /** |
||
234 | * @var string $property |
||
235 | * @var Relation $foo |
||
236 | */ |
||
237 | foreach ($rels['KnownPolyMorphSide'] as $property => $foo) { |
||
238 | $isMany = $foo instanceof MorphToMany; |
||
239 | $targ = get_class($foo->getRelated()); |
||
240 | $mult = $isMany || $foo instanceof MorphMany ? '*' : '1'; |
||
241 | $mult = $foo instanceof MorphOne ? '0..1' : $mult; |
||
242 | |||
243 | $keyName = $this->polyglotFkKey($foo); |
||
244 | $localName = $this->polyglotRkKey($foo); |
||
245 | $first = $isMany ? $keyName : $localName; |
||
246 | $last = $isMany ? $localName : $keyName; |
||
247 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'unknown'); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param array $rels |
||
253 | * @param array $hooks |
||
254 | * @throws InvalidOperationException |
||
255 | */ |
||
256 | protected function getRelationshipsUnknownPolyMorph(array $rels, array &$hooks) |
||
257 | { |
||
258 | /** |
||
259 | * @var string $property |
||
260 | * @var Relation $foo |
||
261 | */ |
||
262 | foreach ($rels['UnknownPolyMorphSide'] as $property => $foo) { |
||
263 | $isMany = $foo instanceof MorphToMany; |
||
264 | $targ = get_class($foo->getRelated()); |
||
265 | $mult = $isMany ? '*' : '1'; |
||
266 | |||
267 | $keyName = $this->polyglotFkKey($foo); |
||
268 | $localName = $this->polyglotRkKey($foo); |
||
269 | |||
270 | $first = $keyName; |
||
271 | $last = (isset($localName) && '' != $localName) ? $localName : $foo->getRelated()->getKeyName(); |
||
272 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'known'); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param $hooks |
||
278 | * @param $foreignField |
||
279 | * @param $RelationName |
||
280 | * @param $localKey |
||
281 | * @param $mult |
||
282 | * @param string|null $relatedObject |
||
283 | * @param mixed|null $type |
||
284 | * @param mixed|null $through |
||
285 | */ |
||
286 | protected function addRelationsHook( |
||
308 | ]; |
||
309 | } |
||
310 | } |
||
311 |