| Conditions | 18 |
| Paths | 15 |
| Total Lines | 86 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | public function getRelationsByKey($className, $keyName) |
||
| 36 | { |
||
| 37 | $this->checkClassExists($className); |
||
| 38 | |||
| 39 | $rels = $this->relations[$className]; |
||
| 40 | if (!array_key_exists($keyName, $rels)) { |
||
| 41 | $msg = 'Key ' . $keyName . ' not registered on ' . $className; |
||
| 42 | throw new \InvalidArgumentException($msg); |
||
| 43 | } |
||
| 44 | |||
| 45 | $result = []; |
||
| 46 | $payload = $rels[$keyName]; |
||
| 47 | $principalType = $className; |
||
| 48 | $numRel = 0; |
||
| 49 | $isKnown = false; |
||
| 50 | |||
| 51 | foreach ($payload as $dependentType => $targDeets) { |
||
| 52 | if (!array_key_exists($dependentType, $this->relations)) { |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | // if principal and ostensible dependent type are equal, drop through to specific handler |
||
| 56 | // at moment, this is only for morphTo relations - morphedByMany doesn't cause this |
||
| 57 | if ($principalType === $dependentType) { |
||
| 58 | $morphToLines = $this->getMorphToRelations($principalType, $targDeets, $keyName); |
||
| 59 | foreach ($morphToLines as $morph) { |
||
| 60 | if (!in_array($morph, $result)) { |
||
| 61 | $result[] = $morph; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | $isKnown = true; |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | $foreign = $this->relations[$dependentType]; |
||
| 69 | |||
| 70 | foreach ($targDeets as $principalProperty => $rawDeets) { |
||
| 71 | $targKey = $rawDeets['local']; |
||
| 72 | $principalMult = $rawDeets['multiplicity']; |
||
| 73 | $principalProperty = $rawDeets['property']; |
||
| 74 | if (!array_key_exists($targKey, $foreign)) { |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | $numRel++; |
||
| 78 | |||
| 79 | $foreignDeets = $foreign[$targKey]; |
||
| 80 | foreach ($foreignDeets as $foreignType => $raw) { |
||
| 81 | if (!array_key_exists($foreignType, $this->relations)) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | foreach ($raw as $dependentProperty => $dependentPayload) { |
||
| 85 | if ($principalType !== $foreignType) { |
||
| 86 | if (null === $dependentPayload['type']) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | if ($keyName == $dependentPayload['local']) { |
||
| 91 | $dependentMult = $dependentPayload['multiplicity']; |
||
| 92 | // generate forward and reverse relations |
||
| 93 | list($forward, $reverse) = $this->calculateRoundTripRelationsGenForwardReverse( |
||
| 94 | $principalType, |
||
| 95 | $principalMult, |
||
| 96 | $principalProperty, |
||
| 97 | $dependentType, |
||
| 98 | $dependentMult, |
||
| 99 | $dependentProperty |
||
| 100 | ); |
||
| 101 | if (!in_array($forward, $result)) { |
||
| 102 | // add forward relation |
||
| 103 | $result[] = $forward; |
||
| 104 | } |
||
| 105 | if (!in_array($reverse, $result)) { |
||
| 106 | // add reverse relation |
||
| 107 | $result[] = $reverse; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $maxRel = $isKnown ? PHP_INT_MAX : 2 * $numRel; |
||
| 116 | $msg = 'Key '.$keyName. ' on class '.$className . ' should have no more than ' |
||
| 117 | .$maxRel.' lines, has '.count($result); |
||
| 118 | assert($maxRel >= count($result), $msg); |
||
| 119 | return $result; |
||
| 120 | } |
||
| 121 | |||
| 263 |