| Conditions | 14 |
| Paths | 20 |
| Total Lines | 61 |
| Code Lines | 36 |
| 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 |
||
| 136 | private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass, array $propertyMetadataOptions = [], string $originAlias = 'o', string &$relationAlias = 'a', bool $wasLeftJoin = false, int &$joinCount = 0) |
||
| 137 | { |
||
| 138 | if ($joinCount > $this->maxJoins) { |
||
| 139 | throw new RuntimeException('The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary.'); |
||
| 140 | } |
||
| 141 | |||
| 142 | $entityManager = $queryBuilder->getEntityManager(); |
||
| 143 | $classMetadata = $entityManager->getClassMetadata($resourceClass); |
||
| 144 | $j = 0; |
||
| 145 | $i = 0; |
||
| 146 | |||
| 147 | foreach ($classMetadata->associationMappings as $association => $mapping) { |
||
| 148 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions); |
||
| 149 | |||
| 150 | if (true === $this->eagerOnly && ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch']) { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | if (false === $propertyMetadata->isReadableLink() || false === $propertyMetadata->isReadable()) { |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | |||
| 158 | if (false === $wasLeftJoin) { |
||
| 159 | $joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null; |
||
| 160 | |||
| 161 | if (null === $joinColumns) { |
||
| 162 | $method = 'leftJoin'; |
||
| 163 | } else { |
||
| 164 | $method = false === $joinColumns[0]['nullable'] ? 'innerJoin' : 'leftJoin'; |
||
| 165 | } |
||
| 166 | } else { |
||
| 167 | $method = 'leftJoin'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $associationAlias = $relationAlias.$i++; |
||
| 171 | $queryBuilder->{$method}($originAlias.'.'.$association, $associationAlias); |
||
| 172 | ++$joinCount; |
||
| 173 | $select = []; |
||
| 174 | $targetClassMetadata = $entityManager->getClassMetadata($mapping['targetEntity']); |
||
| 175 | |||
| 176 | foreach ($this->propertyNameCollectionFactory->create($mapping['targetEntity']) as $property) { |
||
| 177 | $propertyMetadata = $this->propertyMetadataFactory->create($mapping['targetEntity'], $property, $propertyMetadataOptions); |
||
| 178 | |||
| 179 | if (true === $propertyMetadata->isIdentifier()) { |
||
| 180 | $select[] = $property; |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | //the field test allows to add methods to a Resource which do not reflect real database fields |
||
| 185 | if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) { |
||
| 186 | $select[] = $property; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select))); |
||
| 191 | |||
| 192 | $relationAlias .= ++$j; |
||
| 193 | |||
| 194 | $this->joinRelations($queryBuilder, $mapping['targetEntity'], $propertyMetadataOptions, $associationAlias, $relationAlias, $method === 'leftJoin', $joinCount); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 |