Skip to content

Commit 3a4af93

Browse files
authored
Merge pull request #1773 from meyerbaptiste/fix_quality
Fix code quality
2 parents 97616e2 + 8fcc415 commit 3a4af93

37 files changed

+152
-181
lines changed

Diff for: src/Api/CachedIdentifiersExtractor.php

+7-8
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
}

Diff for: src/Api/FilterLocatorTrait.php

+2
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
}

Diff for: src/Api/IdentifiersExtractor.php

+7-1
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
}

Diff for: src/Api/ResourceClassResolver.php

+16-17
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
/**

Diff for: src/Bridge/Doctrine/Common/DataPersister.php

+1-1
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
}

Diff for: src/Bridge/Doctrine/Orm/Filter/AbstractFilter.php

+4-2
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

Diff for: src/Bridge/Doctrine/Orm/Filter/ExistsFilter.php

+1-1
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

Diff for: src/Bridge/Doctrine/Orm/SubresourceDataProvider.php

+2-2
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

Diff for: src/Bridge/Doctrine/Orm/Util/IdentifierManagerTrait.php

+2-1
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
}

Diff for: src/Bridge/FosUser/EventListener.php

+5-8
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
}

Diff for: src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private function registerMetadataConfiguration(ContainerBuilder $container, arra
200200

201201
list($xmlResources, $yamlResources) = $this->getResourcesToWatch($container, $config['mapping']['paths']);
202202

203-
if (isset($config['resource_class_directories']) && $config['resource_class_directories']) {
203+
if (!empty($config['resource_class_directories'])) {
204204
$container->setParameter('api_platform.resource_class_directories', array_merge(
205205
$config['resource_class_directories'], $container->getParameter('api_platform.resource_class_directories')
206206
));

Diff for: src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function getConfigTreeBuilder()
215215
->variableNode('request_options')
216216
->defaultValue([])
217217
->validate()
218-
->ifTrue(function ($v) { return false === is_array($v); })
218+
->ifTrue(function ($v) { return false === \is_array($v); })
219219
->thenInvalid('The request_options parameter must be an array.')
220220
->end()
221221
->info('To pass options to the client charged with the request.')

Diff for: src/Bridge/Symfony/Bundle/Resources/config/doctrine_orm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
<argument>null</argument>
113113
<argument>null</argument>
114114
<argument>%api_platform.eager_loading.fetch_partial%</argument>
115-
<argument type="service" id="serializer.mapping.class_metadata_factory"></argument>
115+
<argument type="service" id="serializer.mapping.class_metadata_factory" />
116116

117117
<tag name="api_platform.doctrine.orm.query_extension.item" priority="64" />
118118
<tag name="api_platform.doctrine.orm.query_extension.collection" priority="64" />

Diff for: src/Bridge/Symfony/Bundle/Resources/views/SwaggerUi/index.html.twig

+27-27
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@
1313
</head>
1414

1515
<body>
16-
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0">
17-
<defs>
18-
<symbol viewBox="0 0 20 20" id="unlocked">
19-
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
20-
</symbol>
16+
<svg xmlns="http://www.w3.org/2000/svg" style="position:absolute;width:0;height:0">
17+
<defs>
18+
<symbol viewBox="0 0 20 20" id="unlocked">
19+
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
20+
</symbol>
2121

22-
<symbol viewBox="0 0 20 20" id="locked">
23-
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z"/>
24-
</symbol>
22+
<symbol viewBox="0 0 20 20" id="locked">
23+
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z"></path>
24+
</symbol>
2525

26-
<symbol viewBox="0 0 20 20" id="close">
27-
<path d="M14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z"/>
28-
</symbol>
26+
<symbol viewBox="0 0 20 20" id="close">
27+
<path d="M14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z"></path>
28+
</symbol>
2929

30-
<symbol viewBox="0 0 20 20" id="large-arrow">
31-
<path d="M13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z"/>
32-
</symbol>
30+
<symbol viewBox="0 0 20 20" id="large-arrow">
31+
<path d="M13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z"></path>
32+
</symbol>
3333

34-
<symbol viewBox="0 0 20 20" id="large-arrow-down">
35-
<path d="M17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z"/>
36-
</symbol>
34+
<symbol viewBox="0 0 20 20" id="large-arrow-down">
35+
<path d="M17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z"></path>
36+
</symbol>
3737

3838

39-
<symbol viewBox="0 0 24 24" id="jump-to">
40-
<path d="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z"/>
41-
</symbol>
39+
<symbol viewBox="0 0 24 24" id="jump-to">
40+
<path d="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z"></path>
41+
</symbol>
4242

43-
<symbol viewBox="0 0 24 24" id="expand">
44-
<path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"/>
45-
</symbol>
43+
<symbol viewBox="0 0 24 24" id="expand">
44+
<path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path>
45+
</symbol>
4646

47-
</defs>
47+
</defs>
4848
</svg>
4949
<header>
50-
<a id = "logo" href="https://api-platform.com"><img src="{{ asset('bundles/apiplatform/logo-header.svg') }}" alt="API Platform"></a>
50+
<a id="logo" href="https://api-platform.com"><img src="{{ asset('bundles/apiplatform/logo-header.svg') }}" alt="API Platform"></a>
5151
</header>
5252

53-
<div class="web"><img src="{{ asset('bundles/apiplatform/web.png') }}"></div>
54-
<div class="spider"><img src="{{ asset('bundles/apiplatform/spider.png') }}"></div>
53+
<div class="web"><img src="{{ asset('bundles/apiplatform/web.png') }}"></div>
54+
<div class="spider"><img src="{{ asset('bundles/apiplatform/spider.png') }}"></div>
5555

5656
<div id="swagger-ui" class="api-platform"></div>
5757

Diff for: src/Bridge/Symfony/Routing/CachedRouteNameResolver.php

+5-19
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,18 @@ public function getRouteName(string $resourceClass, $operationType /**, array $c
4444

4545
try {
4646
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
47-
48-
if ($cacheItem->isHit()) {
49-
return $cacheItem->get();
50-
}
5147
} catch (CacheException $e) {
52-
// do nothing
48+
return $this->decorated->getRouteName($resourceClass, $operationType, $context);
5349
}
5450

55-
if (\func_num_args() > 2) {
56-
$context = func_get_arg(2);
57-
} else {
58-
$context = [];
51+
if ($cacheItem->isHit()) {
52+
return $cacheItem->get();
5953
}
6054

6155
$routeName = $this->decorated->getRouteName($resourceClass, $operationType, $context);
6256

63-
if (!isset($cacheItem)) {
64-
return $routeName;
65-
}
66-
67-
try {
68-
$cacheItem->set($routeName);
69-
$this->cacheItemPool->save($cacheItem);
70-
} catch (CacheException $e) {
71-
// do nothing
72-
}
57+
$cacheItem->set($routeName);
58+
$this->cacheItemPool->save($cacheItem);
7359

7460
return $routeName;
7561
}

Diff for: src/Bridge/Symfony/Routing/RouteNameResolver.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ private function isSameSubresource(array $context, array $currentContext): bool
7171
$currentSubresources[] = $identiferContext[1];
7272
}
7373

74-
if ($currentSubresources === $subresources) {
75-
return true;
76-
}
77-
78-
return false;
74+
return $currentSubresources === $subresources;
7975
}
8076
}

Diff for: src/DataProvider/ChainItemDataProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getItem(string $resourceClass, $id, string $operationName = null
4646

4747
return $dataProvider->getItem($resourceClass, $id, $operationName, $context);
4848
} catch (ResourceClassNotSupportedException $e) {
49-
@trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
49+
@trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', \get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
5050
continue;
5151
}
5252
}

Diff for: src/DataProvider/ChainSubresourceDataProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ public function getSubresource(string $resourceClass, array $identifiers, array
4444
continue;
4545
}
4646
}
47+
48+
return null;
4749
}
4850
}

Diff for: src/GraphQl/Type/Definition/IterableType.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333
*/
3434
final class IterableType extends ScalarType
3535
{
36-
public $name = 'Iterable';
36+
public function __construct()
37+
{
38+
$this->name = 'Iterable';
39+
$this->description = 'The `Iterable` scalar type represents an array or a Traversable with any kind of data.';
3740

38-
public $description = 'The `Iterable` scalar type represents an array or a Traversable with any kind of data.';
41+
parent::__construct();
42+
}
3943

4044
/**
4145
* {@inheritdoc}

0 commit comments

Comments
 (0)