Total Complexity | 48 |
Total Lines | 298 |
Duplicated Lines | 0 % |
Changes | 6 | ||
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 |
||
88 | { |
||
89 | $fileName = $method->getFileName(); |
||
90 | |||
91 | $file = new \SplFileObject($fileName); |
||
92 | $file->seek($method->getStartLine() - 1); |
||
93 | $code = ''; |
||
94 | while ($file->key() < $method->getEndLine()) { |
||
95 | $code .= $file->current(); |
||
96 | $file->next(); |
||
97 | } |
||
98 | |||
99 | $code = trim(preg_replace('/\s\s+/', '', $code)); |
||
100 | if (false === stripos($code, 'function')) { |
||
101 | $msg = 'Function definition must have keyword \'function\''; |
||
102 | throw new InvalidOperationException($msg); |
||
103 | } |
||
104 | $begin = strpos($code, 'function('); |
||
105 | $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); |
||
106 | $lastCode = $code[strlen($code) - 1]; |
||
107 | if ('}' != $lastCode) { |
||
108 | $msg = 'Final character of function definition must be closing brace'; |
||
109 | throw new InvalidOperationException($msg); |
||
110 | } |
||
111 | return $code; |
||
112 | } |
||
113 | /** |
||
114 | * @param bool $biDir |
||
115 | * |
||
116 | * @throws InvalidOperationException |
||
117 | * @throws \ReflectionException |
||
118 | * @return array |
||
119 | */ |
||
120 | protected function getRelationshipsFromMethods(bool $biDir = false) |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param array $rels |
||
181 | * @param array $hooks |
||
182 | * @throws InvalidOperationException |
||
183 | */ |
||
184 | protected function getRelationshipsHasMany(array $rels, array &$hooks) |
||
185 | { |
||
186 | /** |
||
187 | * @var string $property |
||
188 | * @var Relation $relation |
||
189 | */ |
||
190 | foreach ($rels['HasMany'] as $property => $relation) { |
||
191 | if ($relation instanceof MorphMany || $relation instanceof MorphToMany) { |
||
192 | continue; |
||
193 | } |
||
194 | $mult = '*'; |
||
195 | $targ = get_class($relation->getRelated()); |
||
196 | $keyName = $this->polyglotFkKey($relation); |
||
197 | $localName = $this->polyglotRkKey($relation); |
||
198 | $thruName = $relation instanceof HasManyThrough ? |
||
199 | $this->polyglotThroughKey($relation) : |
||
200 | null; |
||
201 | |||
202 | $first = $keyName; |
||
203 | $last = $localName; |
||
204 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, null, $thruName); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param array $rels |
||
210 | * @param array $hooks |
||
211 | * @throws InvalidOperationException |
||
212 | */ |
||
213 | protected function getRelationshipsHasOne(array $rels, array &$hooks) |
||
214 | { |
||
215 | /** |
||
216 | * @var string $property |
||
217 | * @var Relation $foo |
||
218 | */ |
||
219 | foreach ($rels['HasOne'] as $property => $foo) { |
||
220 | if ($foo instanceof MorphOne) { |
||
221 | continue; |
||
222 | } |
||
223 | $isBelong = $foo instanceof BelongsTo; |
||
224 | $mult = $isBelong ? '1' : '0..1'; |
||
225 | $targ = get_class($foo->getRelated()); |
||
226 | |||
227 | $keyName = $this->polyglotFkKey($foo); |
||
228 | $localName = $this->polyglotRkKey($foo); |
||
229 | $first = $isBelong ? $localName : $keyName; |
||
230 | $last = $isBelong ? $keyName : $localName; |
||
231 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param array $rels |
||
237 | * @param array $hooks |
||
238 | * @throws InvalidOperationException |
||
239 | */ |
||
240 | protected function getRelationshipsKnownPolyMorph(array $rels, array &$hooks) |
||
241 | { |
||
242 | /** |
||
243 | * @var string $property |
||
244 | * @var Relation $foo |
||
245 | */ |
||
246 | foreach ($rels['KnownPolyMorphSide'] as $property => $foo) { |
||
247 | $isMany = $foo instanceof MorphToMany; |
||
248 | $targ = get_class($foo->getRelated()); |
||
249 | $mult = $isMany || $foo instanceof MorphMany ? '*' : '1'; |
||
250 | $mult = $foo instanceof MorphOne ? '0..1' : $mult; |
||
251 | |||
252 | $keyName = $this->polyglotFkKey($foo); |
||
253 | $localName = $this->polyglotRkKey($foo); |
||
254 | $first = $isMany ? $keyName : $localName; |
||
255 | $last = $isMany ? $localName : $keyName; |
||
256 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'unknown'); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param array $rels |
||
262 | * @param array $hooks |
||
263 | * @throws InvalidOperationException |
||
264 | */ |
||
265 | protected function getRelationshipsUnknownPolyMorph(array $rels, array &$hooks) |
||
266 | { |
||
267 | /** |
||
268 | * @var string $property |
||
269 | * @var Relation $foo |
||
270 | */ |
||
271 | foreach ($rels['UnknownPolyMorphSide'] as $property => $foo) { |
||
272 | $isMany = $foo instanceof MorphToMany; |
||
273 | $targ = get_class($foo->getRelated()); |
||
274 | $mult = $isMany ? '*' : '1'; |
||
275 | |||
276 | $keyName = $this->polyglotFkKey($foo); |
||
277 | $localName = $this->polyglotRkKey($foo); |
||
278 | |||
279 | $first = $keyName; |
||
280 | $last = (isset($localName) && '' != $localName) ? $localName : $foo->getRelated()->getKeyName(); |
||
281 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'known'); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $hooks |
||
287 | * @param $foreignField |
||
288 | * @param $RelationName |
||
289 | * @param $localKey |
||
290 | * @param $mult |
||
291 | * @param string|null $relatedObject |
||
292 | * @param mixed|null $type |
||
293 | * @param mixed|null $through |
||
294 | */ |
||
295 | protected function addRelationsHook( |
||
317 | ]; |
||
318 | } |
||
319 | } |
||
320 |