Skip to content

Commit 376a14d

Browse files
authored
Merge pull request #1550 from dunglas/style_fix_eager_loading
Some code style fixes in EagerLoadingExtension
2 parents 371879b + 44a5699 commit 376a14d

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,11 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
6969
*/
7070
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
7171
{
72-
$options = [];
73-
74-
if (null !== $operationName) {
75-
$options = ['collection_operation_name' => $operationName];
76-
}
72+
$options = null === $operationName ? [] : ['collection_operation_name' => $operationName];
7773

7874
$forceEager = $this->shouldOperationForceEager($resourceClass, $options);
7975
$fetchPartial = $this->shouldOperationFetchPartial($resourceClass, $options);
80-
$serializerContext = $this->getSerializerContext($resourceClass, 'normalization_context', $options);
76+
$serializerContext = $this->getPropertyMetadataOptions($resourceClass, 'normalization_context', $options);
8177

8278
$groups = $this->getSerializerGroups($options, $serializerContext);
8379

@@ -89,19 +85,15 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
8985
*/
9086
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = [])
9187
{
92-
$options = [];
93-
94-
if (null !== $operationName) {
95-
$options = ['item_operation_name' => $operationName];
96-
}
88+
$options = null === $operationName ? [] : ['item_operation_name' => $operationName];
9789

9890
$forceEager = $this->shouldOperationForceEager($resourceClass, $options);
9991
$fetchPartial = $this->shouldOperationFetchPartial($resourceClass, $options);
10092
$contextType = isset($context['api_denormalize']) ? 'denormalization_context' : 'normalization_context';
101-
$serializerContext = $this->getSerializerContext($context['resource_class'] ?? $resourceClass, $contextType, $options);
102-
$groups = $this->getSerializerGroups($options, $serializerContext);
93+
$propertyMetadataOptions = $this->getPropertyMetadataOptions($context['resource_class'] ?? $resourceClass, $contextType, $options);
94+
$serializerGroups = $this->getSerializerGroups($options, $propertyMetadataOptions);
10395

104-
$this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $fetchPartial, $queryBuilder->getRootAliases()[0], $groups, $serializerContext);
96+
$this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $fetchPartial, $queryBuilder->getRootAliases()[0], $serializerGroups, $propertyMetadataOptions);
10597
}
10698

10799
/**
@@ -231,10 +223,9 @@ private function addSelect(QueryBuilder $queryBuilder, string $entity, string $a
231223
* @param string $contextType normalization_context or denormalization_context
232224
* @param array $options represents the operation name so that groups are the one of the specific operation
233225
*/
234-
private function getSerializerContext(string $resourceClass, string $contextType, array $options): array
226+
private function getPropertyMetadataOptions(string $resourceClass, string $contextType, array $options): array
235227
{
236228
$request = null;
237-
238229
if (null !== $this->requestStack && null !== $this->serializerContextBuilder) {
239230
$request = $this->requestStack->getCurrentRequest();
240231
}
@@ -244,7 +235,6 @@ private function getSerializerContext(string $resourceClass, string $contextType
244235
}
245236

246237
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
247-
248238
if (isset($options['collection_operation_name'])) {
249239
$context = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], $contextType, null, true);
250240
} elseif (isset($options['item_operation_name'])) {
@@ -263,10 +253,10 @@ private function getSerializerContext(string $resourceClass, string $contextType
263253
*/
264254
private function getSerializerGroups(array $options, array $context): array
265255
{
266-
if (empty($context[AbstractNormalizer::GROUPS])) {
267-
return $options;
256+
if (!empty($context[AbstractNormalizer::GROUPS])) {
257+
$options['serializer_groups'] = $context[AbstractNormalizer::GROUPS];
268258
}
269259

270-
return ['serializer_groups' => $context[AbstractNormalizer::GROUPS]];
260+
return $options;
271261
}
272262
}

tests/Bridge/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,6 @@ public function testApplyToCollectionWithANonRedableButFetchEagerProperty()
673673

674674
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
675675

676-
$relatedNameCollection = new PropertyNameCollection(['id', 'name', 'notindatabase', 'notreadable']);
677-
678676
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
679677
$relationPropertyMetadata = new PropertyMetadata();
680678
$relationPropertyMetadata = $relationPropertyMetadata->withAttributes(['fetchEager' => true]);

0 commit comments

Comments
 (0)