| Total Complexity | 45 |
| 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) |
||
| 122 | { |
||
| 123 | $biDirVal = intval($biDir); |
||
| 124 | $isCached = isset(static::$relationCategories[$biDirVal]) && !empty(static::$relationCategories[$biDirVal]); |
||
| 125 | if ($isCached) { |
||
| 126 | return static::$relationCategories[$biDirVal]; |
||
| 127 | } |
||
| 128 | /** @var Model $model */ |
||
| 129 | $model = $this; |
||
| 130 | $relationships = [ |
||
| 131 | 'HasOne' => [], |
||
| 132 | 'UnknownPolyMorphSide' => [], |
||
| 133 | 'HasMany' => [], |
||
| 134 | 'KnownPolyMorphSide' => [] |
||
| 135 | ]; |
||
| 136 | $methods = $this->getModelClassMethods($model); |
||
| 137 | foreach ($methods as $method) { |
||
| 138 | //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php |
||
| 139 | $reflection = new \ReflectionMethod($model, $method); |
||
| 140 | $code = $this->getCodeForMethod($reflection); |
||
| 141 | foreach (static::$relTypes as $relation) { |
||
| 142 | //Resolve the relation's model to a Relation object. |
||
| 143 | if ( |
||
| 144 | !stripos($code, sprintf('$this->%s(', $relation)) || |
||
| 145 | !(($relationObj = $model->$method()) instanceof Relation) || |
||
| 146 | !in_array(MetadataTrait::class, class_uses($relObject = $relationObj->getRelated())) |
||
| 147 | ) { |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | $targetClass = $relation == 'morphTo' ? |
||
| 151 | '\Illuminate\Database\Eloquent\Model|\Eloquent' : |
||
| 152 | '\\' . get_class($relObject); |
||
| 153 | $targObject = $biDir ? $relationObj : $targetClass; |
||
| 154 | $relToKeyMap = [ |
||
| 155 | 'morphedByMany' => ['UnknownPolyMorphSide','HasMany'], |
||
| 156 | 'morphToMany' => ['KnownPolyMorphSide', 'HasMany'], |
||
| 157 | 'morphMany' => ['KnownPolyMorphSide', 'HasMany'], |
||
| 158 | 'hasMany' => ['HasMany'], |
||
| 159 | 'hasManyThrough' => ['HasMany'], |
||
| 160 | 'belongsToMany' => ['HasMany'], |
||
| 161 | 'morphOne' => ['KnownPolyMorphSide', 'HasOne'], |
||
| 162 | 'hasOne' => ['HasOne'], |
||
| 163 | 'belongsTo' => ['HasOne'], |
||
| 164 | 'morphTo' => ['UnknownPolyMorphSide'], |
||
| 165 | ]; |
||
| 166 | foreach ($relToKeyMap[$relation] as $key) { |
||
| 167 | $relationships[$key][$method] = $targObject; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | return static::$relationCategories[$biDirVal] = $relationships; |
||
| 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 |