Skip to content

Commit c40320a

Browse files
authored
Fix some SensioLabs Insight violations (#2068)
* Insight * Rename ChainIdentifierDenormalizer => IdentifierConverter
1 parent f2539dd commit c40320a

File tree

15 files changed

+138
-103
lines changed

15 files changed

+138
-103
lines changed

src/Bridge/Doctrine/Orm/ItemDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface;
2121
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
2222
use ApiPlatform\Core\Exception\RuntimeException;
23-
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
23+
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
2424
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2525
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2626
use Doctrine\Common\Persistence\ManagerRegistry;
@@ -72,7 +72,7 @@ public function getItem(string $resourceClass, $id, string $operationName = null
7272
{
7373
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
7474

75-
if (!($context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] ?? false)) {
75+
if (!($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) {
7676
$id = $this->normalizeIdentifiers($id, $manager, $resourceClass);
7777
}
7878

src/Bridge/Doctrine/Orm/SubresourceDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
2424
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
2525
use ApiPlatform\Core\Exception\RuntimeException;
26-
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
26+
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
2727
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2828
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2929
use Doctrine\Common\Persistence\ManagerRegistry;
@@ -156,7 +156,7 @@ private function buildQuery(array $identifiers, array $context, QueryNameGenerat
156156

157157
if (isset($identifiers[$identifier])) {
158158
// if it's an array it's already normalized, the IdentifierManagerTrait is deprecated
159-
if ($context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] ?? false) {
159+
if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) {
160160
$normalizedIdentifiers = $identifiers[$identifier];
161161
} else {
162162
$normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass);

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<argument type="service" id="api_platform.property_accessor" />
6060
<argument type="service" id="api_platform.identifiers_extractor.cached" />
6161
<argument type="service" id="api_platform.subresource_data_provider" on-invalid="ignore" />
62-
<argument type="service" id="api_platform.identifier.denormalizer" on-invalid="ignore" />
62+
<argument type="service" id="api_platform.identifier.converter" on-invalid="ignore" />
6363
</service>
6464
<service id="ApiPlatform\Core\Api\IriConverterInterface" alias="api_platform.iri_converter" />
6565

@@ -145,7 +145,7 @@
145145
<argument type="service" id="api_platform.item_data_provider" />
146146
<argument type="service" id="api_platform.subresource_data_provider" />
147147
<argument type="service" id="api_platform.serializer.context_builder" />
148-
<argument type="service" id="api_platform.identifier.denormalizer" />
148+
<argument type="service" id="api_platform.identifier.converter" />
149149

150150
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="4" />
151151
</service>
@@ -235,7 +235,7 @@
235235
<argument type="service" id="api_platform.resource_class_resolver" />
236236
</service>
237237

238-
<service id="api_platform.identifier.denormalizer" class="ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer" public="false">
238+
<service id="api_platform.identifier.converter" class="ApiPlatform\Core\Identifier\IdentifierConverter" public="false">
239239
<argument type="service" id="api_platform.identifiers_extractor.cached" />
240240
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
241241
<argument type="tagged" tag="api_platform.identifier.denormalizer" />

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use ApiPlatform\Core\Exception\InvalidIdentifierException;
2626
use ApiPlatform\Core\Exception\ItemNotFoundException;
2727
use ApiPlatform\Core\Exception\RuntimeException;
28-
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
28+
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
2929
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
3030
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
3131
use ApiPlatform\Core\Util\AttributesExtractor;
@@ -49,14 +49,14 @@ final class IriConverter implements IriConverterInterface
4949
private $router;
5050
private $identifiersExtractor;
5151

52-
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null, SubresourceDataProviderInterface $subresourceDataProvider = null, ChainIdentifierDenormalizer $identifierDenormalizer = null)
52+
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null, SubresourceDataProviderInterface $subresourceDataProvider = null, IdentifierConverterInterface $identifierConverter = null)
5353
{
5454
$this->itemDataProvider = $itemDataProvider;
5555
$this->routeNameResolver = $routeNameResolver;
5656
$this->router = $router;
5757
$this->identifiersExtractor = $identifiersExtractor;
5858
$this->subresourceDataProvider = $subresourceDataProvider;
59-
$this->identifierDenormalizer = $identifierDenormalizer;
59+
$this->identifierConverter = $identifierConverter;
6060

6161
if (null === $identifiersExtractor) {
6262
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3', IdentifiersExtractorInterface::class), E_USER_DEPRECATED);
@@ -87,8 +87,8 @@ public function getItemFromIri(string $iri, array $context = [])
8787
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
8888
}
8989

90-
if ($this->identifierDenormalizer) {
91-
$context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true;
90+
if ($this->identifierConverter) {
91+
$context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] = true;
9292
}
9393

9494
if (isset($attributes['subresource_operation_name'])) {

src/DataProvider/OperationDataProviderTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use ApiPlatform\Core\Exception\InvalidIdentifierException;
1717
use ApiPlatform\Core\Exception\RuntimeException;
18-
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
18+
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
1919

2020
/**
2121
* @internal
@@ -38,9 +38,9 @@ trait OperationDataProviderTrait
3838
private $subresourceDataProvider;
3939

4040
/**
41-
* @var ChainIdentifierDenormalizer
41+
* @var IdentifierConverterInterface
4242
*/
43-
private $identifierDenormalizer;
43+
private $identifierConverter;
4444

4545
/**
4646
* Retrieves data for a collection operation.
@@ -95,8 +95,8 @@ private function extractIdentifiers(array $parameters, array $attributes)
9595

9696
$id = $parameters['id'];
9797

98-
if ($this->identifierDenormalizer) {
99-
return $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']);
98+
if ($this->identifierConverter) {
99+
return $this->identifierConverter->convert((string) $id, $attributes['resource_class']);
100100
}
101101

102102
return $id;
@@ -111,8 +111,8 @@ private function extractIdentifiers(array $parameters, array $attributes)
111111

112112
$identifiers[$id] = $parameters[$id];
113113

114-
if ($this->identifierDenormalizer) {
115-
$identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass);
114+
if ($this->identifierConverter) {
115+
$identifiers[$id] = $this->identifierConverter->convert((string) $identifiers[$id], $resourceClass);
116116
}
117117
}
118118

src/EventListener/ReadListener.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
2020
use ApiPlatform\Core\Exception\InvalidIdentifierException;
2121
use ApiPlatform\Core\Exception\RuntimeException;
22-
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
22+
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
2323
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
2424
use ApiPlatform\Core\Util\RequestAttributesExtractor;
2525
use ApiPlatform\Core\Util\RequestParser;
@@ -38,13 +38,13 @@ final class ReadListener
3838

3939
private $serializerContextBuilder;
4040

41-
public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider = null, SerializerContextBuilderInterface $serializerContextBuilder = null, ChainIdentifierDenormalizer $identifierDenormalizer = null)
41+
public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider = null, SerializerContextBuilderInterface $serializerContextBuilder = null, IdentifierConverterInterface $identifierConverter = null)
4242
{
4343
$this->collectionDataProvider = $collectionDataProvider;
4444
$this->itemDataProvider = $itemDataProvider;
4545
$this->subresourceDataProvider = $subresourceDataProvider;
4646
$this->serializerContextBuilder = $serializerContextBuilder;
47-
$this->identifierDenormalizer = $identifierDenormalizer;
47+
$this->identifierConverter = $identifierConverter;
4848
}
4949

5050
/**
@@ -84,8 +84,8 @@ public function onKernelRequest(GetResponseEvent $event)
8484

8585
$data = [];
8686

87-
if ($this->identifierDenormalizer) {
88-
$context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true;
87+
if ($this->identifierConverter) {
88+
$context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] = true;
8989
}
9090

9191
try {

src/Filter/QueryParameterValidateListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1919
use ApiPlatform\Core\Util\RequestAttributesExtractor;
2020
use Psr\Container\ContainerInterface;
21+
use Symfony\Component\HttpFoundation\Request;
2122
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2223

2324
/**
@@ -77,7 +78,7 @@ public function onKernelRequest(GetResponseEvent $event)
7778
/**
7879
* Test if required filter is valid. It validates array notation too like "required[bar]".
7980
*/
80-
private function isRequiredFilterValid($name, $request): bool
81+
private function isRequiredFilterValid(string $name, Request $request): bool
8182
{
8283
$matches = [];
8384
parse_str($name, $matches);

src/Identifier/Normalizer/ChainIdentifierDenormalizer.php renamed to src/Identifier/IdentifierConverter.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,20 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Identifier\Normalizer;
14+
namespace ApiPlatform\Core\Identifier;
1515

1616
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
1717
use ApiPlatform\Core\Exception\InvalidIdentifierException;
18-
use ApiPlatform\Core\Identifier\CompositeIdentifierParser;
1918
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2019
use Symfony\Component\PropertyInfo\Type;
2120

2221
/**
23-
* Identifier normalizer.
22+
* Identifier converter that chains identifier denormalizers.
2423
*
2524
* @author Antoine Bluchet <[email protected]>
2625
*/
27-
class ChainIdentifierDenormalizer
26+
final class IdentifierConverter implements IdentifierConverterInterface
2827
{
29-
const HAS_IDENTIFIER_DENORMALIZER = 'has_identifier_denormalizer';
30-
3128
private $propertyMetadataFactory;
3229
private $identifiersExtractor;
3330
private $identifierDenormalizers;
@@ -40,18 +37,16 @@ public function __construct(IdentifiersExtractorInterface $identifiersExtractor,
4037
}
4138

4239
/**
43-
* @throws InvalidIdentifierException
40+
* {@inheritdoc}
4441
*/
45-
public function denormalize($data, $class, $format = null, array $context = [])
42+
public function convert(string $data, string $class): array
4643
{
4744
$keys = $this->identifiersExtractor->getIdentifiersFromResourceClass($class);
4845

49-
if (!$keys) {
50-
throw new InvalidIdentifierException(sprintf('Resource "%s" has no identifiers.', $class));
51-
}
52-
53-
if (\count($keys) > 1) {
46+
if (($numIdentifiers = \count($keys)) > 1) {
5447
$identifiers = CompositeIdentifierParser::parse($data);
48+
} elseif (0 === $numIdentifiers) {
49+
throw new InvalidIdentifierException(sprintf('Resource "%s" has no identifiers.', $class));
5550
} else {
5651
$identifiers = [$keys[0] => $data];
5752
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Identifier;
15+
16+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17+
18+
/**
19+
* Identifier converter.
20+
*
21+
* @author Antoine Bluchet <[email protected]>
22+
*/
23+
interface IdentifierConverterInterface
24+
{
25+
/**
26+
* @internal
27+
*/
28+
const HAS_IDENTIFIER_CONVERTER = 'has_identifier_converter';
29+
30+
/**
31+
* @param string $data Identifier to convert to php values
32+
* @param string $class The class to which the identifiers belong
33+
*
34+
* @throws InvalidIdentifierException
35+
*
36+
* @return array Indexed by identifiers properties with their values denormalized
37+
*/
38+
public function convert(string $data, string $class): array;
39+
}

tests/Bridge/Doctrine/Orm/ItemDataProviderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
2020
use ApiPlatform\Core\Exception\PropertyNotFoundException;
2121
use ApiPlatform\Core\Exception\RuntimeException;
22-
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
22+
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
2323
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2424
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2525
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
@@ -47,7 +47,7 @@ class ItemDataProviderTest extends TestCase
4747
{
4848
public function testGetItemSingleIdentifier()
4949
{
50-
$context = ['foo' => 'bar', 'fetch_data' => true, ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true];
50+
$context = ['foo' => 'bar', 'fetch_data' => true, IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true];
5151
$queryProphecy = $this->prophesize(AbstractQuery::class);
5252
$queryProphecy->getOneOrNullResult()->willReturn([])->shouldBeCalled();
5353

@@ -119,7 +119,7 @@ public function testGetItemDoubleIdentifier()
119119
],
120120
], $queryBuilder);
121121

122-
$context = [ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true];
122+
$context = [IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true];
123123
$extensionProphecy = $this->prophesize(QueryItemExtensionInterface::class);
124124
$extensionProphecy->applyToItem($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, ['ida' => 1, 'idb' => 2], 'foo', $context)->shouldBeCalled();
125125

@@ -177,7 +177,7 @@ public function testQueryResultExtension()
177177
],
178178
], $queryBuilder);
179179

180-
$context = [ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true];
180+
$context = [IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true];
181181
$extensionProphecy = $this->prophesize(QueryResultItemExtensionInterface::class);
182182
$extensionProphecy->applyToItem($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, ['id' => 1], 'foo', $context)->shouldBeCalled();
183183
$extensionProphecy->supportsResult(Dummy::class, 'foo', $context)->willReturn(true)->shouldBeCalled();
@@ -234,7 +234,7 @@ public function testCannotCreateQueryBuilder()
234234
'id',
235235
]);
236236

237-
(new ItemDataProvider($managerRegistryProphecy->reveal(), $propertyNameCollectionFactory, $propertyMetadataFactory, [$extensionProphecy->reveal()]))->getItem(Dummy::class, 'foo', null, [ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true]);
237+
(new ItemDataProvider($managerRegistryProphecy->reveal(), $propertyNameCollectionFactory, $propertyMetadataFactory, [$extensionProphecy->reveal()]))->getItem(Dummy::class, 'foo', null, [IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true]);
238238
}
239239

240240
/**

0 commit comments

Comments
 (0)