| Conditions | 11 |
| Paths | 18 |
| Total Lines | 49 |
| Code Lines | 29 |
| 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 |
||
| 73 | private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass, string $originAlias = 'o', string &$relationAlias = 'a', bool $wasLeftJoin = false) |
||
| 74 | { |
||
| 75 | $classMetadata = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass); |
||
| 76 | $j = 0; |
||
| 77 | |||
| 78 | foreach ($classMetadata->getAssociationNames() as $i => $association) { |
||
| 79 | $mapping = $classMetadata->associationMappings[$association]; |
||
| 80 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association); |
||
| 81 | |||
| 82 | if (ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch'] || false === $propertyMetadata->isReadableLink()) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (false === $wasLeftJoin) { |
||
| 87 | $joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null; |
||
| 88 | |||
| 89 | if (null === $joinColumns) { |
||
| 90 | $method = 'leftJoin'; |
||
| 91 | } else { |
||
| 92 | $method = false === $joinColumns[0]['nullable'] ? 'innerJoin' : 'leftJoin'; |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | $method = 'leftJoin'; |
||
| 96 | } |
||
| 97 | |||
| 98 | $associationAlias = $relationAlias.$i; |
||
| 99 | $queryBuilder->{$method}($originAlias.'.'.$association, $associationAlias); |
||
| 100 | $select = []; |
||
| 101 | $targetClassMetadata = $queryBuilder->getEntityManager()->getClassMetadata($mapping['targetEntity']); |
||
| 102 | |||
| 103 | foreach ($this->getMetadataProperties($mapping['targetEntity']) as $property => $propertyMetadata) { |
||
| 104 | if (true === $propertyMetadata->isIdentifier()) { |
||
| 105 | $select[] = $property; |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | //the field test allows to add methods to a Resource which do not reflect real database fields |
||
| 110 | if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) { |
||
| 111 | $select[] = $property; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select))); |
||
| 116 | |||
| 117 | $relationAlias = $relationAlias.++$j; |
||
| 118 | |||
| 119 | $this->joinRelations($queryBuilder, $mapping['targetEntity'], $associationAlias, $relationAlias, $method === 'leftJoin'); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 |