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

api-platform / core / 13202629907

07 Feb 2025 03:13PM UTC coverage: 7.554% (+0.3%) from 7.283%
13202629907

Pull #6954

github

web-flow
Merge 696625eca into 89816721e
Pull Request #6954: perf: various optimizations for Laravel/Symfony

10 of 18 new or added lines in 4 files covered. (55.56%)

94 existing lines in 8 files now uncovered.

9931 of 131474 relevant lines covered (7.55%)

4.44 hits per line

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

75.38
/src/Metadata/Resource/Factory/LinkFactory.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\Resource\Factory;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Link;
18
use ApiPlatform\Metadata\Metadata;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use Symfony\Component\PropertyInfo\Type;
23

24
/**
25
 * @internal
26
 */
27
final class LinkFactory implements LinkFactoryInterface, PropertyLinkFactoryInterface
28
{
29
    /**
30
     * @var array<class-string, string[]>
31
     */
32
    private $localIdentifiersPerResourceClassCache = [];
33

34
    public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceClassResolverInterface $resourceClassResolver)
35
    {
36
    }
312✔
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function createLinkFromProperty(Metadata $operation, string $property): Link
42
    {
43
        $metadata = $this->propertyMetadataFactory->create($resourceClass = $operation->getClass(), $property);
×
44
        $relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes());
×
45
        if (!$relationClass) {
×
46
            throw new RuntimeException(\sprintf('We could not find a class matching the uriVariable "%s" on "%s".', $property, $resourceClass));
×
47
        }
48

49
        $identifiers = $this->resourceClassResolver->isResourceClass($relationClass) ? $this->getIdentifiersFromResourceClass($relationClass) : ['id'];
×
50

51
        return new Link(fromClass: $relationClass, toProperty: $property, identifiers: $identifiers, parameterName: $property);
×
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function createLinksFromIdentifiers(Metadata $operation): array
58
    {
59
        $identifiers = $this->getIdentifiersFromResourceClass($resourceClass = $operation->getClass());
54✔
60

61
        if (!$identifiers) {
54✔
62
            return [];
16✔
63
        }
64

65
        $link = (new Link())->withFromClass($resourceClass)->withIdentifiers($identifiers);
42✔
66
        $parameterName = $identifiers[0];
42✔
67
        if ('value' === $parameterName && enum_exists($resourceClass)) {
42✔
68
            $parameterName = 'id';
2✔
69
        }
70

71
        if (1 < \count($identifiers)) {
42✔
72
            $parameterName = 'id';
×
73
            $link = $link->withCompositeIdentifier(true);
×
74
        }
75

76
        return [$link->withParameterName($parameterName)];
42✔
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function createLinksFromRelations(Metadata $operation): array
83
    {
84
        $links = [];
42✔
85
        foreach ($this->propertyNameCollectionFactory->create($resourceClass = $operation->getClass()) as $property) {
42✔
86
            $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
40✔
87

88
            if (!($relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes())) || !$this->resourceClassResolver->isResourceClass($relationClass)) {
40✔
89
                continue;
40✔
90
            }
91

92
            $identifiers = $this->getIdentifiersFromResourceClass($resourceClass);
12✔
93

94
            $links[] = (new Link())->withFromProperty($property)->withFromClass($resourceClass)->withToClass($relationClass)->withIdentifiers($identifiers);
12✔
95
        }
96

97
        return $links;
42✔
98
    }
99

100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function createLinksFromAttributes(Metadata $operation): array
104
    {
105
        $links = [];
42✔
106
        try {
107
            $reflectionClass = new \ReflectionClass($resourceClass = $operation->getClass());
42✔
108
            foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
42✔
109
                $reflectionProperty = $reflectionClass->getProperty($property);
40✔
110

111
                foreach ($reflectionProperty->getAttributes(Link::class) as $attributeLink) {
40✔
112
                    $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
×
113

114
                    $attributeLink = $attributeLink->newInstance()
×
115
                        ->withFromProperty($property);
×
116

117
                    if (!$attributeLink->getFromClass()) {
×
118
                        $attributeLink = $attributeLink->withFromClass($resourceClass)->withToClass($this->getPropertyClassType($metadata->getBuiltinTypes()) ?? $resourceClass);
×
119
                    }
120

121
                    $links[] = $attributeLink;
×
122
                }
123
            }
124
        } catch (\ReflectionException) {
12✔
125
        }
126

127
        return $links;
42✔
128
    }
129

130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function completeLink(Link $link): Link
134
    {
135
        if (!$link->getIdentifiers()) {
28✔
136
            $link = $link->withIdentifiers($this->getIdentifiersFromResourceClass($link->getFromClass()));
14✔
137
        }
138

139
        if (1 < \count((array) $link->getIdentifiers())) {
28✔
140
            $link = $link->withCompositeIdentifier(true);
×
141
        }
142

143
        return $link;
28✔
144
    }
145

146
    /**
147
     * @param class-string $resourceClass
148
     *
149
     * @return string[]
150
     */
151
    private function getIdentifiersFromResourceClass(string $resourceClass): array
152
    {
153
        if (isset($this->localIdentifiersPerResourceClassCache[$resourceClass])) {
56✔
154
            return $this->localIdentifiersPerResourceClassCache[$resourceClass];
54✔
155
        }
156

157
        $hasIdProperty = false;
56✔
158
        $identifiers = [];
56✔
159
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
56✔
160
            $isIdentifier = $this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier();
44✔
161

162
            if (!$hasIdProperty && null === $isIdentifier) {
44✔
163
                $hasIdProperty = 'id' === $property;
40✔
164
            }
165

166
            if ($isIdentifier) {
44✔
167
                $identifiers[] = $property;
42✔
168
            }
169
        }
170

171
        if ($hasIdProperty && !$identifiers) {
56✔
NEW
172
            return $this->localIdentifiersPerResourceClassCache[$resourceClass] = ['id'];
×
173
        }
174

175
        if (!$hasIdProperty && !$identifiers && enum_exists($resourceClass)) {
56✔
176
            return $this->localIdentifiersPerResourceClassCache[$resourceClass] = ['value'];
4✔
177
        }
178

179
        return $this->localIdentifiersPerResourceClassCache[$resourceClass] = $identifiers;
54✔
180
    }
181

182
    /**
183
     * @param Type[]|null $types
184
     */
185
    private function getPropertyClassType(?array $types): ?string
186
    {
187
        foreach ($types ?? [] as $type) {
40✔
188
            if ($type->isCollection()) {
40✔
189
                return $this->getPropertyClassType($type->getCollectionValueTypes());
22✔
190
            }
191

192
            if ($class = $type->getClassName()) {
40✔
193
                return $class;
32✔
194
            }
195
        }
196

197
        return null;
40✔
198
    }
199
}
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