Skip to content

Commit 8fcc415

Browse files
committed
Fix code quality
1 parent 316b3f3 commit 8fcc415

37 files changed

+152
-181
lines changed

src/Api/CachedIdentifiersExtractor.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,18 @@ private function getKeys($item, callable $retriever): array
106106

107107
try {
108108
$cacheItem = $this->cacheItemPool->getItem(self::CACHE_KEY_PREFIX.md5($resourceClass));
109-
if ($cacheItem->isHit()) {
110-
return $this->localCache[$resourceClass] = $cacheItem->get();
111-
}
112109
} catch (CacheException $e) {
113-
// do nothing
110+
return $this->localCache[$resourceClass] = array_keys($retriever($item));
111+
}
112+
113+
if ($cacheItem->isHit()) {
114+
return $this->localCache[$resourceClass] = $cacheItem->get();
114115
}
115116

116117
$keys = array_keys($retriever($item));
117118

118-
if (isset($cacheItem)) {
119-
$cacheItem->set($keys);
120-
$this->cacheItemPool->save($cacheItem);
121-
}
119+
$cacheItem->set($keys);
120+
$this->cacheItemPool->save($cacheItem);
122121

123122
return $this->localCache[$resourceClass] = $keys;
124123
}

src/Api/FilterLocatorTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,7 @@ private function getFilter(string $filterId)
6262
if ($this->filterLocator instanceof FilterCollection && $this->filterLocator->offsetExists($filterId)) {
6363
return $this->filterLocator->offsetGet($filterId);
6464
}
65+
66+
return null;
6567
}
6668
}

src/Api/IdentifiersExtractor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ public function getIdentifiersFromItem($item): array
6868
if (null === $identifier || false === $identifier) {
6969
continue;
7070
}
71+
7172
$identifier = $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
7273
if (!\is_object($identifier)) {
7374
continue;
74-
} elseif (method_exists($identifier, '__toString')) {
75+
}
76+
77+
if (method_exists($identifier, '__toString')) {
7578
$identifiers[$propertyName] = (string) $identifier;
7679
continue;
7780
}
81+
7882
$relatedResourceClass = $this->getObjectClass($identifier);
7983
$relatedItem = $identifier;
8084
unset($identifiers[$propertyName]);
@@ -84,9 +88,11 @@ public function getIdentifiersFromItem($item): array
8488
if (isset($identifiers[$propertyName])) {
8589
throw new RuntimeException(sprintf('Composite identifiers not supported in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass));
8690
}
91+
8792
$identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedPropertyName);
8893
}
8994
}
95+
9096
if (!isset($identifiers[$propertyName])) {
9197
throw new RuntimeException(sprintf('No identifier found in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass));
9298
}

src/Api/ResourceClassResolver.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,28 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
3939
*/
4040
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string
4141
{
42-
if (\is_object($value) && !$value instanceof \Traversable) {
43-
$typeToFind = $type = $this->getObjectClass($value);
44-
if (null === $resourceClass) {
45-
$resourceClass = $typeToFind;
46-
}
47-
} elseif (null === $resourceClass) {
42+
$type = \is_object($value) && !$value instanceof \Traversable ? $this->getObjectClass($value) : $resourceClass;
43+
$resourceClass = $resourceClass ?? $type;
44+
45+
if (null === $resourceClass) {
4846
throw new InvalidArgumentException(sprintf('No resource class found.'));
49-
} else {
50-
$typeToFind = $type = $resourceClass;
5147
}
5248

53-
if (($strict && isset($type) && $resourceClass !== $type) || false === $isResourceClass = $this->isResourceClass($typeToFind)) {
54-
if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) {
55-
return $type;
56-
}
57-
if ($isResourceClass ?? $this->isResourceClass($typeToFind)) {
58-
return $typeToFind;
59-
}
49+
if (
50+
null === $type
51+
|| ((!$strict || $resourceClass === $type) && $isResourceClass = $this->isResourceClass($type))
52+
) {
53+
return $resourceClass;
54+
}
6055

61-
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $typeToFind));
56+
if (
57+
($isResourceClass ?? $this->isResourceClass($type))
58+
|| (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass))
59+
) {
60+
return $type;
6261
}
6362

64-
return $resourceClass;
63+
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $type));
6564
}
6665

6766
/**

src/Bridge/Doctrine/Common/DataPersister.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ public function remove($data)
7878
*/
7979
private function getManager($data)
8080
{
81-
return is_object($data) ? $this->managerRegistry->getManagerForClass($this->getObjectClass($data)) : null;
81+
return \is_object($data) ? $this->managerRegistry->getManagerForClass($this->getObjectClass($data)) : null;
8282
}
8383
}

src/Bridge/Doctrine/Orm/Filter/AbstractFilter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ protected function getNestedMetadata(string $resourceClass, array $associations)
223223
*/
224224
protected function splitPropertyParts(string $property/*, string $resourceClass*/): array
225225
{
226+
$resourceClass = null;
226227
$parts = explode('.', $property);
227228

228229
if (\func_num_args() > 1) {
@@ -236,7 +237,7 @@ protected function splitPropertyParts(string $property/*, string $resourceClass*
236237
}
237238
}
238239

239-
if (!isset($resourceClass)) {
240+
if (null === $resourceClass) {
240241
return [
241242
'associations' => \array_slice($parts, 0, -1),
242243
'field' => end($parts),
@@ -317,13 +318,14 @@ protected function addJoinsForNestedProperty(string $property, string $rootAlias
317318

318319
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
319320
$parentAlias = $rootAlias;
321+
$alias = null;
320322

321323
foreach ($propertyParts['associations'] as $association) {
322324
$alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $parentAlias, $association);
323325
$parentAlias = $alias;
324326
}
325327

326-
if (!isset($alias)) {
328+
if (null === $alias) {
327329
throw new InvalidArgumentException(sprintf('Cannot add joins for property "%s" - property is not nested.', $property));
328330
}
329331

src/Bridge/Doctrine/Orm/Filter/ExistsFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected function isNullableField(string $property, string $resourceClass): boo
169169
*/
170170
private function isAssociationNullable(array $associationMapping): bool
171171
{
172-
if (isset($associationMapping['id']) && $associationMapping['id']) {
172+
if (!empty($associationMapping['id'])) {
173173
return false;
174174
}
175175

src/Bridge/Doctrine/Orm/SubresourceDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ public function getSubresource(string $resourceClass, array $identifiers, array
6868
{
6969
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
7070
if (null === $manager) {
71-
throw new ResourceClassNotSupportedException();
71+
throw new ResourceClassNotSupportedException(sprintf('The object manager associated with the "%s" resource class cannot be retrieved.', $resourceClass));
7272
}
7373

7474
$repository = $manager->getRepository($resourceClass);
7575
if (!method_exists($repository, 'createQueryBuilder')) {
7676
throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
7777
}
7878

79-
if (!isset($context['identifiers']) || !isset($context['property'])) {
79+
if (!isset($context['identifiers'], $context['property'])) {
8080
throw new ResourceClassNotSupportedException('The given resource class is not a subresource.');
8181
}
8282

src/Bridge/Doctrine/Orm/Util/IdentifierManagerTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private function normalizeIdentifiers($id, ObjectManager $manager, string $resou
4444
$doctrineIdentifierFields = $doctrineClassMetadata->getIdentifier();
4545
$isOrm = interface_exists(EntityManagerInterface::class) && $manager instanceof EntityManagerInterface;
4646
$platform = $isOrm ? $manager->getConnection()->getDatabasePlatform() : null;
47+
$identifiersMap = null;
4748

4849
if (\count($doctrineIdentifierFields) > 1) {
4950
$identifiersMap = [];
@@ -69,7 +70,7 @@ private function normalizeIdentifiers($id, ObjectManager $manager, string $resou
6970
continue;
7071
}
7172

72-
$identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
73+
$identifier = null === $identifiersMap ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
7374
if (null === $identifier) {
7475
throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
7576
}

src/Bridge/FosUser/EventListener.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
5050
return;
5151
}
5252

53-
switch ($request->getMethod()) {
54-
case 'DELETE':
55-
$this->userManager->deleteUser($user);
56-
$event->setControllerResult(null);
57-
break;
58-
default:
59-
$this->userManager->updateUser($user);
60-
break;
53+
if ('DELETE' === $request->getMethod()) {
54+
$this->userManager->deleteUser($user);
55+
$event->setControllerResult(null);
56+
} else {
57+
$this->userManager->updateUser($user);
6158
}
6259
}
6360
}

0 commit comments

Comments
 (0)