Total Complexity | 48 |
Total Lines | 291 |
Duplicated Lines | 0 % |
Changes | 8 | ||
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) |
||
121 | { |
||
122 | $biDirVal = intval($biDir); |
||
123 | $isCached = isset(static::$relationCategories[$biDirVal]) && !empty(static::$relationCategories[$biDirVal]); |
||
124 | if ($isCached) { |
||
125 | return static::$relationCategories[$biDirVal]; |
||
126 | } |
||
127 | /** @var Model $model */ |
||
128 | $model = $this; |
||
129 | $relationships = [ |
||
130 | 'HasOne' => [], |
||
131 | 'UnknownPolyMorphSide' => [], |
||
132 | 'HasMany' => [], |
||
133 | 'KnownPolyMorphSide' => [] |
||
134 | ]; |
||
135 | $methods = $this->getModelClassMethods($model); |
||
136 | foreach ($methods as $method) { |
||
137 | //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php |
||
138 | $reflection = new \ReflectionMethod($model, $method); |
||
139 | $code = $this->getCodeForMethod($reflection); |
||
140 | foreach (static::$relTypes as $relation) { |
||
141 | //Resolve the relation's model to a Relation object. |
||
142 | if ( |
||
143 | !stripos($code, sprintf('$this->%s(', $relation)) || |
||
144 | !(($relationObj = $model->$method()) instanceof Relation) || |
||
145 | !in_array(MetadataTrait::class, class_uses($relObject = $relationObj->getRelated())) |
||
146 | ) { |
||
147 | continue; |
||
148 | } |
||
149 | $targObject = $biDir ? $relationObj : '\\' . get_class($relObject); |
||
150 | if (in_array($relation, static::$manyRelTypes)) { |
||
151 | //Collection or array of models (because Collection is Arrayable) |
||
152 | $relationships['HasMany'][$method] = $targObject; |
||
153 | } elseif ('morphTo' === $relation) { |
||
154 | // Model isn't specified because relation is polymorphic |
||
155 | $relationships['UnknownPolyMorphSide'][$method] = |
||
156 | $biDir ? $relationObj : '\Illuminate\Database\Eloquent\Model|\Eloquent'; |
||
157 | } else { |
||
158 | //Single model is returned |
||
159 | $relationships['HasOne'][$method] = $targObject; |
||
160 | } |
||
161 | if (in_array($relation, ['morphMany', 'morphOne', 'morphToMany'])) { |
||
162 | $relationships['KnownPolyMorphSide'][$method] = $targObject; |
||
163 | } |
||
164 | if (in_array($relation, ['morphedByMany'])) { |
||
165 | $relationships['UnknownPolyMorphSide'][$method] = $targObject; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | return static::$relationCategories[$biDirVal] = $relationships; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param array $rels |
||
174 | * @param array $hooks |
||
175 | * @throws InvalidOperationException |
||
176 | */ |
||
177 | protected function getRelationshipsHasMany(array $rels, array &$hooks) |
||
178 | { |
||
179 | /** |
||
180 | * @var string $property |
||
181 | * @var Relation $relation |
||
182 | */ |
||
183 | foreach ($rels['HasMany'] as $property => $relation) { |
||
184 | if ($relation instanceof MorphMany || $relation instanceof MorphToMany) { |
||
185 | continue; |
||
186 | } |
||
187 | $mult = '*'; |
||
188 | $targ = get_class($relation->getRelated()); |
||
189 | $keyName = $this->polyglotFkKey($relation); |
||
190 | $localName = $this->polyglotRkKey($relation); |
||
191 | $thruName = $relation instanceof HasManyThrough ? |
||
192 | $this->polyglotThroughKey($relation) : |
||
193 | null; |
||
194 | |||
195 | $first = $keyName; |
||
196 | $last = $localName; |
||
197 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, null, $thruName); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param array $rels |
||
203 | * @param array $hooks |
||
204 | * @throws InvalidOperationException |
||
205 | */ |
||
206 | protected function getRelationshipsHasOne(array $rels, array &$hooks) |
||
207 | { |
||
208 | /** |
||
209 | * @var string $property |
||
210 | * @var Relation $foo |
||
211 | */ |
||
212 | foreach ($rels['HasOne'] as $property => $foo) { |
||
213 | if ($foo instanceof MorphOne) { |
||
214 | continue; |
||
215 | } |
||
216 | $isBelong = $foo instanceof BelongsTo; |
||
217 | $mult = $isBelong ? '1' : '0..1'; |
||
218 | $targ = get_class($foo->getRelated()); |
||
219 | |||
220 | $keyName = $this->polyglotFkKey($foo); |
||
221 | $localName = $this->polyglotRkKey($foo); |
||
222 | $first = $isBelong ? $localName : $keyName; |
||
223 | $last = $isBelong ? $keyName : $localName; |
||
224 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param array $rels |
||
230 | * @param array $hooks |
||
231 | * @throws InvalidOperationException |
||
232 | */ |
||
233 | protected function getRelationshipsKnownPolyMorph(array $rels, array &$hooks) |
||
234 | { |
||
235 | /** |
||
236 | * @var string $property |
||
237 | * @var Relation $foo |
||
238 | */ |
||
239 | foreach ($rels['KnownPolyMorphSide'] as $property => $foo) { |
||
240 | $isMany = $foo instanceof MorphToMany; |
||
241 | $targ = get_class($foo->getRelated()); |
||
242 | $mult = $isMany || $foo instanceof MorphMany ? '*' : '1'; |
||
243 | $mult = $foo instanceof MorphOne ? '0..1' : $mult; |
||
244 | |||
245 | $keyName = $this->polyglotFkKey($foo); |
||
246 | $localName = $this->polyglotRkKey($foo); |
||
247 | $first = $isMany ? $keyName : $localName; |
||
248 | $last = $isMany ? $localName : $keyName; |
||
249 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'unknown'); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param array $rels |
||
255 | * @param array $hooks |
||
256 | * @throws InvalidOperationException |
||
257 | */ |
||
258 | protected function getRelationshipsUnknownPolyMorph(array $rels, array &$hooks) |
||
259 | { |
||
260 | /** |
||
261 | * @var string $property |
||
262 | * @var Relation $foo |
||
263 | */ |
||
264 | foreach ($rels['UnknownPolyMorphSide'] as $property => $foo) { |
||
265 | $isMany = $foo instanceof MorphToMany; |
||
266 | $targ = get_class($foo->getRelated()); |
||
267 | $mult = $isMany ? '*' : '1'; |
||
268 | |||
269 | $keyName = $this->polyglotFkKey($foo); |
||
270 | $localName = $this->polyglotRkKey($foo); |
||
271 | |||
272 | $first = $keyName; |
||
273 | $last = (isset($localName) && '' != $localName) ? $localName : $foo->getRelated()->getKeyName(); |
||
274 | $this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'known'); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param $hooks |
||
280 | * @param $foreignField |
||
281 | * @param $RelationName |
||
282 | * @param $localKey |
||
283 | * @param $mult |
||
284 | * @param string|null $relatedObject |
||
285 | * @param mixed|null $type |
||
286 | * @param mixed|null $through |
||
287 | */ |
||
288 | protected function addRelationsHook( |
||
310 | ]; |
||
311 | } |
||
312 | } |
||
313 |