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