| Conditions | 13 |
| Paths | 30 |
| Total Lines | 53 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 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 |
||
| 110 | public function getRelations() |
||
| 111 | { |
||
| 112 | $classNames = array_keys($this->relations); |
||
| 113 | |||
| 114 | $associations = $this->buildRelationAssociations($classNames); |
||
| 115 | |||
| 116 | $unknowns = $this->buildUnknownSides(); |
||
| 117 | |||
| 118 | $monoAssoc = []; |
||
| 119 | $polyAssoc = []; |
||
| 120 | foreach ($associations as $assoc) { |
||
| 121 | if ($assoc->getFirst() instanceof AssociationStubMonomorphic) { |
||
| 122 | $monoAssoc[] = $assoc; |
||
| 123 | continue; |
||
| 124 | } |
||
| 125 | // monomorphic associations are dealt with, now for the polymorphic associations - they're a mite trickier |
||
| 126 | $firstKnown = $assoc->getFirst()->isKnownSide(); |
||
| 127 | /** @var AssociationStubPolymorphic $known */ |
||
| 128 | $known = $firstKnown ? $assoc->getFirst() : $assoc->getLast(); |
||
| 129 | /** @var AssociationStubPolymorphic $unknown */ |
||
| 130 | $unknown = $firstKnown ? $assoc->getLast() : $assoc->getFirst(); |
||
| 131 | $className = $known->getBaseType(); |
||
| 132 | $relName = $known->getRelationName(); |
||
| 133 | $unknowns[$className][$relName][] = $unknown; |
||
| 134 | } |
||
| 135 | |||
| 136 | foreach ($this->knownSides as $knownType => $knownDeets) { |
||
| 137 | foreach (array_keys($knownDeets) as $key) { |
||
| 138 | /** @var AssociationStubPolymorphic[] $lastCandidates */ |
||
| 139 | $lastCandidates = $unknowns[$knownType][$key]; |
||
| 140 | if (0 == count($lastCandidates)) { |
||
| 141 | continue; |
||
| 142 | } |
||
| 143 | foreach ($lastCandidates as $lc) { |
||
| 144 | /** @var AssociationStubPolymorphic $stub */ |
||
| 145 | $stub = clone $this->knownSides[$knownType][$key]; |
||
| 146 | $isMulti = ($stub->getMultiplicity() == AssociationStubRelationType::MANY()); |
||
| 147 | $relPolyTypeName = substr($lc->getBaseType(), strrpos($lc->getBaseType(), '\\')+1); |
||
| 148 | $relPolyTypeName = Str::plural($relPolyTypeName, $isMulti ? 2 : 1); |
||
| 149 | $stub->setRelationName($stub->getRelationName() . '_' . $relPolyTypeName); |
||
| 150 | $assoc = new AssociationMonomorphic(); |
||
| 151 | $first = -1 === $stub->compare($lc); |
||
| 152 | $assoc->setFirst($first ? $stub : $lc); |
||
| 153 | $assoc->setLast($first ? $lc : $stub); |
||
| 154 | if (!$assoc->isOk()) { |
||
| 155 | throw new InvalidOperationException(''); |
||
| 156 | } |
||
| 157 | $polyAssoc[] = $assoc; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | $result = array_merge($monoAssoc, $polyAssoc); |
||
| 162 | return $result; |
||
| 163 | } |
||
| 216 |