Total Complexity | 54 |
Total Lines | 299 |
Duplicated Lines | 0 % |
Changes | 9 | ||
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 | * @param \ReflectionMethod $method |
||
84 | * @return string |
||
85 | * @throws InvalidOperationException |
||
86 | */ |
||
87 | protected function getCodeForMethod(\ReflectionMethod $method) : string |
||
112 | } |
||
113 | /** |
||
114 | * @param bool $biDir |
||
115 | * |
||
116 | * @throws InvalidOperationException |
||
117 | * @throws \ReflectionException |
||
118 | * @return array |
||
119 | */ |
||
120 | protected function getRelationshipsFromMethods(bool $biDir = false) |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param array $rels |
||
182 | * @param array $hooks |
||
183 | * @throws InvalidOperationException |
||
184 | */ |
||
185 | protected function getRelationshipsHasMany(array $rels, array &$hooks) |
||
186 | { |
||
187 | /** |
||
188 | * @var string $property |
||
189 | * @var Relation $relation |
||
190 | */ |
||
191 | foreach ($rels['HasMany'] as $property => $relation) { |
||
192 | if ($relation instanceof MorphMany || $relation instanceof MorphToMany) { |
||
193 | continue; |
||
194 | } |
||
195 | $mult = '*'; |
||
196 | $targ = get_class($relation->getRelated()); |
||
197 | $keyName = $this->polyglotFkKey($relation); |
||
198 | $localName = $this->polyglotRkKey($relation); |
||
199 | $thruName = $relation instanceof HasManyThrough ? |
||
200 | $this->polyglotThroughKey($relation) : |
||
201 | null; |
||
202 | |||
203 | $first = $keyName; |
||
204 | $last = $localName; |
||
205 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, null, $thruName); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param array $rels |
||
211 | * @param array $hooks |
||
212 | * @throws InvalidOperationException |
||
213 | */ |
||
214 | protected function getRelationshipsHasOne(array $rels, array &$hooks) |
||
215 | { |
||
216 | /** |
||
217 | * @var string $property |
||
218 | * @var Relation $foo |
||
219 | */ |
||
220 | foreach ($rels['HasOne'] as $property => $foo) { |
||
221 | if ($foo instanceof MorphOne) { |
||
222 | continue; |
||
223 | } |
||
224 | $isBelong = $foo instanceof BelongsTo; |
||
225 | $mult = $isBelong ? '1' : '0..1'; |
||
226 | $targ = get_class($foo->getRelated()); |
||
227 | |||
228 | $keyName = $this->polyglotFkKey($foo); |
||
229 | $localName = $this->polyglotRkKey($foo); |
||
230 | $first = $isBelong ? $localName : $keyName; |
||
231 | $last = $isBelong ? $keyName : $localName; |
||
232 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param array $rels |
||
238 | * @param array $hooks |
||
239 | * @throws InvalidOperationException |
||
240 | */ |
||
241 | protected function getRelationshipsKnownPolyMorph(array $rels, array &$hooks) |
||
242 | { |
||
243 | /** |
||
244 | * @var string $property |
||
245 | * @var Relation $foo |
||
246 | */ |
||
247 | foreach ($rels['KnownPolyMorphSide'] as $property => $foo) { |
||
248 | $isMany = $foo instanceof MorphToMany; |
||
249 | $targ = get_class($foo->getRelated()); |
||
250 | $mult = $isMany || $foo instanceof MorphMany ? '*' : '1'; |
||
251 | $mult = $foo instanceof MorphOne ? '0..1' : $mult; |
||
252 | |||
253 | $keyName = $this->polyglotFkKey($foo); |
||
254 | $localName = $this->polyglotRkKey($foo); |
||
255 | $first = $isMany ? $keyName : $localName; |
||
256 | $last = $isMany ? $localName : $keyName; |
||
257 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'unknown'); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param array $rels |
||
263 | * @param array $hooks |
||
264 | * @throws InvalidOperationException |
||
265 | */ |
||
266 | protected function getRelationshipsUnknownPolyMorph(array $rels, array &$hooks) |
||
267 | { |
||
268 | /** |
||
269 | * @var string $property |
||
270 | * @var Relation $foo |
||
271 | */ |
||
272 | foreach ($rels['UnknownPolyMorphSide'] as $property => $foo) { |
||
273 | $isMany = $foo instanceof MorphToMany; |
||
274 | $targ = get_class($foo->getRelated()); |
||
275 | $mult = $isMany ? '*' : '1'; |
||
276 | |||
277 | $keyName = $this->polyglotFkKey($foo); |
||
278 | $localName = $this->polyglotRkKey($foo); |
||
279 | |||
280 | $first = $keyName; |
||
281 | $last = (isset($localName) && '' != $localName) ? $localName : $foo->getRelated()->getKeyName(); |
||
282 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'known'); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $hooks |
||
288 | * @param $foreignField |
||
289 | * @param $RelationName |
||
290 | * @param $localKey |
||
291 | * @param $mult |
||
292 | * @param string|null $relatedObject |
||
293 | * @param mixed|null $type |
||
294 | * @param mixed|null $through |
||
295 | */ |
||
296 | protected function addRelationsHook( |
||
318 | ]; |
||
319 | } |
||
320 | } |
||
321 |