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