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 |
||
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 | * @return array |
||
31 | * @throws InvalidOperationException |
||
32 | * @throws \ReflectionException |
||
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 | * @return array |
||
87 | * @throws InvalidOperationException |
||
88 | * @throws \ReflectionException |
||
89 | */ |
||
90 | protected function getRelationshipsFromMethods($biDir = false) |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param $rels |
||
172 | * @param $hooks |
||
173 | * @throws InvalidOperationException |
||
174 | */ |
||
175 | protected function getRelationshipsHasMany($rels, &$hooks) |
||
176 | { |
||
177 | /** |
||
178 | * @var string $property |
||
179 | * @var Relation $foo |
||
180 | */ |
||
181 | foreach ($rels['HasMany'] as $property => $foo) { |
||
182 | if ($foo instanceof MorphMany || $foo instanceof MorphToMany) { |
||
183 | continue; |
||
184 | } |
||
185 | $mult = '*'; |
||
186 | $targ = get_class($foo->getRelated()); |
||
187 | list($thruName, $fkMethodName, $rkMethodName) = $this->getRelationsHasManyKeyNames($foo); |
||
188 | |||
189 | $keyRaw = $foo->$fkMethodName(); |
||
190 | $keySegments = explode('.', $keyRaw); |
||
191 | $keyName = $keySegments[count($keySegments) - 1]; |
||
192 | $localRaw = $foo->$rkMethodName(); |
||
193 | $localSegments = explode('.', $localRaw); |
||
194 | $localName = $localSegments[count($localSegments) - 1]; |
||
195 | if (null !== $thruName) { |
||
196 | $thruRaw = $foo->$thruName(); |
||
197 | $thruSegments = explode('.', $thruRaw); |
||
198 | $thruName = $thruSegments[count($thruSegments) - 1]; |
||
199 | } |
||
200 | $first = $keyName; |
||
201 | $last = $localName; |
||
202 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, null, $thruName); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param $rels |
||
208 | * @param $hooks |
||
209 | * @throws InvalidOperationException |
||
210 | */ |
||
211 | protected function getRelationshipsHasOne($rels, &$hooks) |
||
212 | { |
||
213 | /** |
||
214 | * @var string $property |
||
215 | * @var Relation $foo |
||
216 | */ |
||
217 | foreach ($rels['HasOne'] as $property => $foo) { |
||
218 | if ($foo instanceof MorphOne) { |
||
219 | continue; |
||
220 | } |
||
221 | $isBelong = $foo instanceof BelongsTo; |
||
222 | $mult = $isBelong ? '1' : '0..1'; |
||
223 | $targ = get_class($foo->getRelated()); |
||
224 | |||
225 | list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isBelong); |
||
226 | list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isBelong); |
||
227 | |||
228 | $keyName = $isBelong ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
||
229 | $keySegments = explode('.', $keyName); |
||
230 | $keyName = $keySegments[count($keySegments) - 1]; |
||
231 | $localRaw = $isBelong ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
||
232 | $localSegments = explode('.', $localRaw); |
||
233 | $localName = $localSegments[count($localSegments) - 1]; |
||
234 | $first = $isBelong ? $localName : $keyName; |
||
235 | $last = $isBelong ? $keyName : $localName; |
||
236 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param $rels |
||
242 | * @param $hooks |
||
243 | * @throws InvalidOperationException |
||
244 | */ |
||
245 | protected function getRelationshipsKnownPolyMorph($rels, &$hooks) |
||
246 | { |
||
247 | /** |
||
248 | * @var string $property |
||
249 | * @var Relation $foo |
||
250 | */ |
||
251 | foreach ($rels['KnownPolyMorphSide'] as $property => $foo) { |
||
252 | $isMany = $foo instanceof MorphToMany; |
||
253 | $targ = get_class($foo->getRelated()); |
||
254 | $mult = $isMany ? '*' : ($foo instanceof MorphMany ? '*' : '1'); |
||
255 | $mult = $foo instanceof MorphOne ? '0..1' : $mult; |
||
256 | |||
257 | list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isMany); |
||
258 | list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isMany); |
||
259 | |||
260 | $keyRaw = $isMany ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
||
261 | $keySegments = explode('.', $keyRaw); |
||
262 | $keyName = $keySegments[count($keySegments) - 1]; |
||
263 | $localRaw = $isMany ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
||
264 | $localSegments = explode('.', $localRaw); |
||
265 | $localName = $localSegments[count($localSegments) - 1]; |
||
266 | $first = $isMany ? $keyName : $localName; |
||
267 | $last = $isMany ? $localName : $keyName; |
||
268 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'unknown'); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @param $rels |
||
274 | * @param $hooks |
||
275 | * @throws InvalidOperationException |
||
276 | */ |
||
277 | protected function getRelationshipsUnknownPolyMorph($rels, &$hooks) |
||
278 | { |
||
279 | /** |
||
280 | * @var string $property |
||
281 | * @var Relation $foo |
||
282 | */ |
||
283 | foreach ($rels['UnknownPolyMorphSide'] as $property => $foo) { |
||
284 | $isMany = $foo instanceof MorphToMany; |
||
285 | $targ = get_class($foo->getRelated()); |
||
286 | $mult = $isMany ? '*' : '1'; |
||
287 | |||
288 | list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isMany); |
||
289 | list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isMany); |
||
290 | |||
291 | $keyRaw = $isMany ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
||
292 | $keySegments = explode('.', $keyRaw); |
||
293 | $keyName = $keySegments[count($keySegments) - 1]; |
||
294 | $localRaw = $isMany ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
||
295 | $localSegments = explode('.', $localRaw); |
||
296 | $localName = $localSegments[count($localSegments) - 1]; |
||
297 | |||
298 | $first = $keyName; |
||
299 | $last = (isset($localName) && '' != $localName) ? $localName : $foo->getRelated()->getKeyName(); |
||
300 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'known'); |
||
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param $hooks |
||
306 | * @param $first |
||
307 | * @param $property |
||
308 | * @param $last |
||
309 | * @param $mult |
||
310 | * @param string|null $targ |
||
311 | * @param mixed|null $type |
||
312 | * @param mixed|null $through |
||
313 | */ |
||
314 | protected function addRelationsHook(&$hooks, $first, $property, $last, $mult, $targ, $type = null, $through = null) |
||
328 | ]; |
||
329 | } |
||
330 | } |
||
331 |