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