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

api-platform / core / 13198036619

07 Feb 2025 10:32AM UTC coverage: 7.126% (-0.2%) from 7.282%
13198036619

push

github

web-flow
fix(laravel): Prevent overwriting existing routes on the router (#6941)

* Prevent overwriting existing routes on the router
* Move routes to its own routes file and add domain support
* Fix tests and stan. Run cs fixer
* Revert laravel pint changes

0 of 61 new or added lines in 4 files covered. (0.0%)

821 existing lines in 63 files now uncovered.

12156 of 170580 relevant lines covered (7.13%)

11.98 hits per line

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

88.89
/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
    public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceClassResolverInterface $resourceClassResolver)
30
    {
31
    }
1,177✔
32

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

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

46
        return new Link(fromClass: $relationClass, toProperty: $property, identifiers: $identifiers, parameterName: $property);
×
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function createLinksFromIdentifiers(Metadata $operation): array
53
    {
54
        $identifiers = $this->getIdentifiersFromResourceClass($resourceClass = $operation->getClass());
105✔
55

56
        if (!$identifiers) {
105✔
57
            return [];
26✔
58
        }
59

60
        $link = (new Link())->withFromClass($resourceClass)->withIdentifiers($identifiers);
86✔
61
        $parameterName = $identifiers[0];
86✔
62
        if ('value' === $parameterName && enum_exists($resourceClass)) {
86✔
63
            $parameterName = 'id';
4✔
64
        }
65

66
        if (1 < \count($identifiers)) {
86✔
UNCOV
67
            $parameterName = 'id';
1✔
UNCOV
68
            $link = $link->withCompositeIdentifier(true);
1✔
69
        }
70

71
        return [$link->withParameterName($parameterName)];
86✔
72
    }
73

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

83
            if (!($relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes())) || !$this->resourceClassResolver->isResourceClass($relationClass)) {
84✔
84
                continue;
84✔
85
            }
86

87
            $identifiers = $this->getIdentifiersFromResourceClass($resourceClass);
28✔
88

89
            $links[] = (new Link())->withFromProperty($property)->withFromClass($resourceClass)->withToClass($relationClass)->withIdentifiers($identifiers);
28✔
90
        }
91

92
        return $links;
87✔
93
    }
94

95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function createLinksFromAttributes(Metadata $operation): array
99
    {
100
        $links = [];
87✔
101
        try {
102
            $reflectionClass = new \ReflectionClass($resourceClass = $operation->getClass());
87✔
103
            foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
87✔
104
                $reflectionProperty = $reflectionClass->getProperty($property);
84✔
105

106
                foreach ($reflectionProperty->getAttributes(Link::class) as $attributeLink) {
84✔
UNCOV
107
                    $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
1✔
108

UNCOV
109
                    $attributeLink = $attributeLink->newInstance()
1✔
UNCOV
110
                        ->withFromProperty($property);
1✔
111

UNCOV
112
                    if (!$attributeLink->getFromClass()) {
1✔
UNCOV
113
                        $attributeLink = $attributeLink->withFromClass($resourceClass)->withToClass($this->getPropertyClassType($metadata->getBuiltinTypes()) ?? $resourceClass);
1✔
114
                    }
115

UNCOV
116
                    $links[] = $attributeLink;
1✔
117
                }
118
            }
119
        } catch (\ReflectionException) {
23✔
120
        }
121

122
        return $links;
87✔
123
    }
124

125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function completeLink(Link $link): Link
129
    {
130
        if (!$link->getIdentifiers()) {
54✔
131
            $link = $link->withIdentifiers($this->getIdentifiersFromResourceClass($link->getFromClass()));
28✔
132
        }
133

134
        if (1 < \count((array) $link->getIdentifiers())) {
54✔
135
            $link = $link->withCompositeIdentifier(true);
×
136
        }
137

138
        return $link;
54✔
139
    }
140

141
    private function getIdentifiersFromResourceClass(string $resourceClass): array
142
    {
143
        $hasIdProperty = false;
108✔
144
        $identifiers = [];
108✔
145
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
108✔
146
            $isIdentifier = $this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier();
90✔
147

148
            if (!$hasIdProperty && null === $isIdentifier) {
90✔
149
                $hasIdProperty = 'id' === $property;
65✔
150
            }
151

152
            if ($isIdentifier) {
90✔
153
                $identifiers[] = $property;
85✔
154
            }
155
        }
156

157
        if ($hasIdProperty && !$identifiers) {
108✔
UNCOV
158
            return ['id'];
2✔
159
        }
160

161
        if (!$hasIdProperty && !$identifiers && enum_exists($resourceClass)) {
107✔
162
            return ['value'];
7✔
163
        }
164

165
        return $identifiers;
104✔
166
    }
167

168
    /**
169
     * @param Type[]|null $types
170
     */
171
    private function getPropertyClassType(?array $types): ?string
172
    {
173
        foreach ($types ?? [] as $type) {
84✔
174
            if ($type->isCollection()) {
84✔
175
                return $this->getPropertyClassType($type->getCollectionValueTypes());
42✔
176
            }
177

178
            if ($class = $type->getClassName()) {
84✔
179
                return $class;
64✔
180
            }
181
        }
182

183
        return null;
84✔
184
    }
185
}
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

© 2026 Coveralls, Inc