• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.27
/src/Metadata/IdentifiersExtractor.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Metadata;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
22
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
23
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
24
use Symfony\Component\PropertyAccess\PropertyAccess;
25
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
26

27
/**
28
 * {@inheritdoc}
29
 *
30
 * @author Antoine Bluchet <soyuka@gmail.com>
31
 */
32
final class IdentifiersExtractor implements IdentifiersExtractorInterface
33
{
34
    use ResourceClassInfoTrait;
35
    private readonly PropertyAccessorInterface $propertyAccessor;
36

37
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, ?PropertyAccessorInterface $propertyAccessor = null)
38
    {
39
        $this->resourceMetadataFactory = $resourceMetadataFactory;
1,999✔
40
        $this->resourceClassResolver = $resourceClassResolver;
1,999✔
41
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
1,999✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     *
47
     * TODO: 3.0 identifiers should be stringable?
48
     */
49
    public function getIdentifiersFromItem(object $item, ?Operation $operation = null, array $context = []): array
50
    {
51
        if (!$this->isResourceClass($this->getObjectClass($item))) {
1,682✔
52
            return ['id' => $this->propertyAccessor->getValue($item, 'id')];
15✔
53
        }
54

55
        if ($operation && $operation->getClass()) {
1,669✔
56
            return $this->getIdentifiersFromOperation($item, $operation, $context);
1,669✔
57
        }
58

59
        $resourceClass = $this->getResourceClass($item, true);
215✔
60
        $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
215✔
61

62
        return $this->getIdentifiersFromOperation($item, $operation, $context);
215✔
63
    }
64

65
    private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
66
    {
67
        if ($operation instanceof HttpOperation) {
1,669✔
68
            $links = $operation->getUriVariables();
1,669✔
69
        } elseif ($operation instanceof GraphQlOperation) {
×
70
            $links = $operation->getLinks();
×
71
        }
72

73
        $identifiers = [];
1,669✔
74
        foreach ($links ?? [] as $k => $link) {
1,669✔
75
            if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
1,662✔
UNCOV
76
                $compositeIdentifiers = [];
16✔
UNCOV
77
                foreach ($link->getIdentifiers() as $identifier) {
16✔
UNCOV
78
                    $compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName());
16✔
79
                }
80

UNCOV
81
                $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
16✔
UNCOV
82
                continue;
16✔
83
            }
84

85
            $parameterName = $link->getParameterName();
1,661✔
86
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty());
1,661✔
87
        }
88

89
        return $identifiers;
1,666✔
90
    }
91

92
    /**
93
     * Gets the value of the given class property.
94
     */
95
    private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, ?string $toProperty = null): float|bool|int|string
96
    {
97
        if ($item instanceof $class) {
1,662✔
98
            try {
99
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
1,662✔
100
            } catch (NoSuchPropertyException $e) {
116✔
101
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
102
            }
103
        }
104

105
        if ($toProperty) {
38✔
106
            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
21✔
107
        }
108

109
        $resourceClass = $this->getResourceClass($item, true);
22✔
110
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
22✔
111
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
22✔
112

113
            $types = $propertyMetadata->getBuiltinTypes();
22✔
114
            if (null === ($type = $types[0] ?? null)) {
22✔
115
                continue;
×
116
            }
117

118
            try {
119
                if ($type->isCollection()) {
22✔
120
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
20✔
121

122
                    if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) {
20✔
123
                        return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
4✔
124
                    }
125
                }
126

127
                if ($type->getClassName() === $class) {
22✔
128
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
22✔
129
                }
130
            } catch (NoSuchPropertyException $e) {
2✔
131
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
2✔
132
            }
133
        }
134

135
        throw new RuntimeException('Not able to retrieve identifiers.');
6✔
136
    }
137

138
    /**
139
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
140
     *
141
     * @param mixed|\Stringable $identifierValue
142
     */
143
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
144
    {
145
        if (null === $identifierValue) {
1,662✔
146
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
116✔
147
        }
148

149
        if (\is_scalar($identifierValue)) {
1,659✔
150
            return $identifierValue;
1,648✔
151
        }
152

UNCOV
153
        if ($identifierValue instanceof \Stringable) {
29✔
UNCOV
154
            return (string) $identifierValue;
29✔
155
        }
156

157
        if ($identifierValue instanceof \BackedEnum) {
×
158
            return (string) $identifierValue->value;
×
159
        }
160

161
        throw new RuntimeException(\sprintf('We were not able to resolve the identifier matching parameter "%s".', $parameterName));
×
162
    }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc