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