| Conditions | 9 |
| Paths | 9 |
| Total Lines | 70 |
| Code Lines | 37 |
| 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 |
||
| 68 | public function buildAccessPath(array $dimensionNames) |
||
| 69 | { |
||
| 70 | $currentEntity = current($this->rootEntities); |
||
| 71 | $lastAlias = key($this->aliasMap); |
||
| 72 | |||
| 73 | foreach ($dimensionNames as $i => $dimension) { |
||
| 74 | if (!$this->em->getClassMetadata($currentEntity)) { |
||
| 75 | throw new \Exception(sprintf('Metadata not found for entity "%s"', $currentEntity)); |
||
| 76 | } |
||
| 77 | |||
| 78 | // the current dimension is an alias (a table already joined for instance) |
||
| 79 | if (isset($this->aliasMap[$dimension])) { |
||
| 80 | $currentEntity = $this->getEntity($dimension); |
||
| 81 | $lastAlias = $dimension; |
||
| 82 | |||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | // the current dimension is a relation of the current entity |
||
| 87 | if ($this->relationExists($dimension, $currentEntity)) { |
||
| 88 | /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $classMetadata */ |
||
| 89 | $classMetadata = $this->em->getClassMetadata($currentEntity); |
||
| 90 | $association = $classMetadata->getAssociationMapping($dimension); |
||
| 91 | |||
| 92 | if (!isset($this->knownEntities[$currentEntity], $this->knownEntities[$currentEntity][$association['fieldName']])) { |
||
| 93 | $alias = sprintf('_%d_%s', count($this->knownEntities, COUNT_RECURSIVE), $dimension); |
||
| 94 | |||
| 95 | $this->saveAlias($currentEntity, $association['fieldName'], $alias); |
||
| 96 | |||
| 97 | $this->detectedJoins[] = [ |
||
| 98 | 'root' => $lastAlias, |
||
| 99 | 'column' => $dimension, |
||
| 100 | 'as' => $alias = $this->getAlias($currentEntity, $association['fieldName']), |
||
| 101 | ]; |
||
| 102 | } else { |
||
| 103 | $alias = $this->getAlias($currentEntity, $association['fieldName']); |
||
| 104 | } |
||
| 105 | |||
| 106 | $currentEntity = $association['targetEntity']; |
||
| 107 | $lastAlias = $alias; |
||
| 108 | |||
| 109 | continue; |
||
| 110 | } |
||
| 111 | |||
| 112 | // the current dimension is an embedded class |
||
| 113 | if ($this->embeddedExists($dimension, $currentEntity)) { |
||
| 114 | /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $classMetadata */ |
||
| 115 | $classMetadata = $this->em->getClassMetadata($currentEntity); |
||
| 116 | $embeddableMetadata = $classMetadata->embeddedClasses[$dimension]; |
||
| 117 | |||
| 118 | $currentEntity = $embeddableMetadata['class']; |
||
| 119 | $lastAlias = $lastAlias.'.'.$dimension; |
||
| 120 | |||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | // or, at last, it's a column access. |
||
| 125 | if ($this->columnExists($dimension, $currentEntity)) { |
||
| 126 | if (($i + 1) === count($dimensionNames)) { |
||
| 127 | return sprintf('%s.%s', $lastAlias, $dimension); |
||
| 128 | } |
||
| 129 | |||
| 130 | throw new \RuntimeException('Found scalar attribute in the middle of an access path.'); |
||
| 131 | } |
||
| 132 | |||
| 133 | throw new \Exception(sprintf('"%s" not found for entity "%s"', $dimension, $currentEntity)); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $lastAlias; |
||
| 137 | } |
||
| 138 | |||
| 191 |