| Conditions | 13 |
| Paths | 19 |
| Total Lines | 56 |
| Code Lines | 33 |
| 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 |
||
| 119 | private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass, array $propertyMetadataOptions = [], string $originAlias = 'o', string &$relationAlias = 'a', bool $wasLeftJoin = false, $depth = 0) |
||
| 120 | { |
||
| 121 | if ($depth > $this->maxJoins) { |
||
| 122 | throw new \RuntimeException('Too many joined relations, might be an infinite loop! Check out serialization groups!'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $entityManager = $queryBuilder->getEntityManager(); |
||
| 126 | $classMetadata = $entityManager->getClassMetadata($resourceClass); |
||
| 127 | $j = 0; |
||
| 128 | $i = 0; |
||
| 129 | |||
| 130 | foreach ($classMetadata->associationMappings as $association => $mapping) { |
||
| 131 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions); |
||
| 132 | |||
| 133 | if (ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch'] || false === $propertyMetadata->isReadableLink() || false === $propertyMetadata->isReadable()) { |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | |||
| 137 | if (false === $wasLeftJoin) { |
||
| 138 | $joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null; |
||
| 139 | |||
| 140 | if (null === $joinColumns) { |
||
| 141 | $method = 'leftJoin'; |
||
| 142 | } else { |
||
| 143 | $method = false === $joinColumns[0]['nullable'] ? 'innerJoin' : 'leftJoin'; |
||
| 144 | } |
||
| 145 | } else { |
||
| 146 | $method = 'leftJoin'; |
||
| 147 | } |
||
| 148 | |||
| 149 | $associationAlias = $relationAlias.$i++; |
||
| 150 | $queryBuilder->{$method}($originAlias.'.'.$association, $associationAlias); |
||
| 151 | $select = []; |
||
| 152 | $targetClassMetadata = $entityManager->getClassMetadata($mapping['targetEntity']); |
||
| 153 | |||
| 154 | foreach ($this->propertyNameCollectionFactory->create($mapping['targetEntity']) as $property) { |
||
| 155 | $propertyMetadata = $this->propertyMetadataFactory->create($mapping['targetEntity'], $property, $propertyMetadataOptions); |
||
| 156 | |||
| 157 | if (true === $propertyMetadata->isIdentifier()) { |
||
| 158 | $select[] = $property; |
||
| 159 | continue; |
||
| 160 | } |
||
| 161 | |||
| 162 | //the field test allows to add methods to a Resource which do not reflect real database fields |
||
| 163 | if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) { |
||
| 164 | $select[] = $property; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | $queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select))); |
||
| 169 | |||
| 170 | $relationAlias = $relationAlias.++$j; |
||
| 171 | |||
| 172 | $this->joinRelations($queryBuilder, $mapping['targetEntity'], $propertyMetadataOptions, $associationAlias, $relationAlias, $method === 'leftJoin', ++$depth); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 |