Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 32 | final class EagerLoadingExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface |
||
| 33 | { |
||
| 34 | private $propertyMetadataFactory; |
||
| 35 | private $resourceMetadataFactory; |
||
| 36 | private $maxJoins; |
||
| 37 | private $forceEager; |
||
| 38 | |||
| 39 | View Code Duplication | public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, int $maxJoins = 30, bool $forceEager = true) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | * The context may contain serialization groups which helps defining joined entities that are readable. |
||
| 73 | */ |
||
| 74 | public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Joins relations to eager load. |
||
| 97 | * |
||
| 98 | * @param QueryBuilder $queryBuilder |
||
| 99 | * @param QueryNameGeneratorInterface $queryNameGenerator |
||
| 100 | * @param string $resourceClass |
||
| 101 | * @param bool $forceEager |
||
| 102 | * @param string $parentAlias |
||
| 103 | * @param array $propertyMetadataOptions |
||
| 104 | * @param bool $wasLeftJoin if the relation containing the new one had a left join, we have to force the new one to left join too |
||
| 105 | * @param int $joinCount the number of joins |
||
| 106 | * |
||
| 107 | * @throws RuntimeException when the max number of joins has been reached |
||
| 108 | */ |
||
| 109 | private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, bool $forceEager, string $parentAlias, array $propertyMetadataOptions = [], bool $wasLeftJoin = false, int &$joinCount = 0) |
||
| 110 | { |
||
| 111 | if ($joinCount > $this->maxJoins) { |
||
| 112 | throw new RuntimeException('The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary.'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $entityManager = $queryBuilder->getEntityManager(); |
||
| 116 | $classMetadata = $entityManager->getClassMetadata($resourceClass); |
||
| 117 | |||
| 118 | foreach ($classMetadata->associationMappings as $association => $mapping) { |
||
| 119 | try { |
||
| 120 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions); |
||
| 121 | } catch (PropertyNotFoundException $propertyNotFoundException) { |
||
| 122 | //skip properties not found |
||
| 123 | continue; |
||
| 124 | } catch (ResourceClassNotFoundException $resourceClassNotFoundException) { |
||
| 125 | //skip associations that are not resource classes |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (false === $forceEager && ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch']) { |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | |||
| 133 | if (false === $propertyMetadata->isReadableLink() || false === $propertyMetadata->isReadable()) { |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | |||
| 137 | $isNullable = $mapping['joinColumns'][0]['nullable'] ?? true; |
||
| 138 | if (false !== $wasLeftJoin || true === $isNullable) { |
||
| 139 | $method = 'leftJoin'; |
||
| 140 | } else { |
||
| 141 | $method = 'innerJoin'; |
||
| 142 | } |
||
| 143 | |||
| 144 | $associationAlias = $queryNameGenerator->generateJoinAlias($association); |
||
| 145 | $queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias); |
||
| 146 | ++$joinCount; |
||
| 147 | |||
| 148 | try { |
||
| 149 | $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $propertyMetadataOptions); |
||
| 150 | } catch (ResourceClassNotFoundException $resourceClassNotFoundException) { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $associationAlias, $propertyMetadataOptions, $method === 'leftJoin', $joinCount); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets serializer groups if available, if not it returns the $options array. |
||
| 183 | * |
||
| 184 | * @param string $resourceClass |
||
| 185 | * @param array $options represents the operation name so that groups are the one of the specific operation |
||
| 186 | * @param string $context normalization_context or denormalization_context |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | private function getSerializerGroups(string $resourceClass, array $options, string $context): array |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Does an operation force eager? |
||
| 211 | * |
||
| 212 | * @param string $resourceClass |
||
| 213 | * @param array $options |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | private function isForceEager(string $resourceClass, array $options): bool |
||
| 231 | } |
||
| 232 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.