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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

96.51
/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.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\ApiResource;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Metadata;
21
use ApiPlatform\Metadata\Operations;
22
use ApiPlatform\Metadata\Parameter;
23
use ApiPlatform\Metadata\Parameters;
24
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
25
use Psr\Log\LoggerInterface;
26
use Psr\Log\NullLogger;
27

28
/**
29
 * @internal
30
 *
31
 * This trait shares the common logic between attributes and Laravel concerns factories
32
 *
33
 * @author Antoine Bluchet <soyuka@gmail.com>
34
 * @author Kévin Dunglas <kevin@dunglas.dev>
35
 */
36
trait MetadataCollectionFactoryTrait
37
{
38
    use OperationDefaultsTrait;
39

40
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, ?LoggerInterface $logger = null, array $defaults = [], private readonly bool $graphQlEnabled = false)
41
    {
42
        $this->logger = $logger ?? new NullLogger();
730✔
43
        $this->defaults = $defaults;
730✔
44
        $this->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter();
730✔
45
    }
46

47
    private function isResourceMetadata(string $name): bool
48
    {
49
        return is_a($name, ApiResource::class, true) || is_subclass_of($name, HttpOperation::class) || is_subclass_of($name, GraphQlOperation::class) || is_a($name, Parameter::class, true);
118✔
50
    }
51

52
    /**
53
     * Builds resource operations to support:
54
     * Resource
55
     * Get
56
     * Post
57
     * Resource
58
     * Get
59
     * In the future, we will be able to use nested attributes (https://wiki.php.net/rfc/new_in_initializers).
60
     *
61
     * @param array<Metadata|Parameter> $metadataCollection
62
     *
63
     * @return ApiResource[]
64
     */
65
    private function buildResourceOperations(array $metadataCollection, string $resourceClass, array $resources = []): array
66
    {
67
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
118✔
68
        $index = -1;
118✔
69
        $operationPriority = 0;
118✔
70
        $hasApiResource = false;
118✔
71
        $globalParameters = new Parameters();
118✔
72

73
        foreach ($metadataCollection as $metadata) {
118✔
74
            if ($metadata instanceof Parameter) {
116✔
75
                if (!$k = $metadata->getKey()) {
4✔
76
                    throw new RuntimeException('Parameter "key" is mandatory when used on a class.');
×
77
                }
78
                $globalParameters->add($k, $metadata);
4✔
79
                continue;
4✔
80
            }
81

82
            if ($metadata instanceof ApiResource) {
116✔
83
                $hasApiResource = true;
84✔
84
                $resource = $this->getResourceWithDefaults($resourceClass, $shortName, $metadata);
84✔
85
                $operations = [];
84✔
86
                foreach ($resource->getOperations() ?? new Operations() as $operation) {
84✔
87
                    [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
42✔
88
                    $operations[$key] = $operation;
42✔
89
                }
90
                if ($operations) {
84✔
91
                    $resource = $resource->withOperations(new Operations($operations));
42✔
92
                }
93
                $resources[++$index] = $resource;
84✔
94
                continue;
84✔
95
            }
96

97
            if (!is_subclass_of($metadata, HttpOperation::class) && !is_subclass_of($metadata, GraphQlOperation::class)) {
72✔
98
                continue;
×
99
            }
100

101
            if ($metadata instanceof GraphQlOperation) {
72✔
102
                [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
2✔
103
                $graphQlOperations = $resources[$index]->getGraphQlOperations();
2✔
104
                $graphQlOperations[$key] = $operation;
2✔
105
                $resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperations);
2✔
106
                continue;
2✔
107
            }
108

109
            if (-1 === $index || $this->hasSameOperation($resources[$index], $metadata::class, $metadata)) {
72✔
110
                $resources[++$index] = $this->getResourceWithDefaults($resourceClass, $shortName, new ApiResource());
44✔
111
            }
112

113
            [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
72✔
114
            if (null === $operation->getPriority()) {
72✔
115
                $operation = $operation->withPriority(++$operationPriority);
72✔
116
            }
117
            $operations = $resources[$index]->getOperations() ?? new Operations();
72✔
118
            $resources[$index] = $resources[$index]->withOperations($operations->add($key, $operation));
72✔
119
        }
120

121
        // Loop again and set default operations if none where found
122
        foreach ($resources as $index => $resource) {
118✔
123
            if (\count($globalParameters) > 0) {
116✔
124
                $resources[$index] = $resource = $this->mergeOperationParameters($resource, $globalParameters);
4✔
125
            }
126

127
            if (null === $resource->getOperations()) {
116✔
128
                $operations = [];
36✔
129
                foreach ($this->getDefaultHttpOperations($resource) as $operation) {
36✔
130
                    [$key, $operation] = $this->getOperationWithDefaults($resource, $operation, true);
36✔
131
                    $operations[$key] = $operation;
36✔
132
                }
133
                $resources[$index] = $resource = $resource->withOperations(new Operations($operations));
36✔
134
            }
135

136
            if ($parameters = $resource->getParameters()) {
116✔
137
                $operations = [];
4✔
138
                foreach ($resource->getOperations() ?? [] as $i => $operation) {
4✔
139
                    $operations[$i] = $this->mergeOperationParameters($operation, $parameters);
4✔
140
                }
141
                $resources[$index] = $resource = $resource->withOperations(new Operations($operations)); // @phpstan-ignore-line
4✔
142
            }
143

144
            if (!$this->graphQlEnabled) {
116✔
145
                continue;
6✔
146
            }
147

148
            $graphQlOperations = $resource->getGraphQlOperations();
110✔
149
            if (null === $graphQlOperations) {
110✔
150
                if (!$hasApiResource) {
110✔
151
                    $resources[$index] = $resources[$index]->withGraphQlOperations([]);
42✔
152
                    continue;
42✔
153
                }
154

155
                // Add default GraphQL operations on the first resource
156
                if (0 === $index) {
76✔
157
                    $resources[$index] = $this->addDefaultGraphQlOperations($resources[$index]);
74✔
158
                }
159
                continue;
76✔
160
            }
161

162
            $resources[$index] = $this->completeGraphQlOperations($resources[$index]);
12✔
163
            $graphQlOperations = $resources[$index]->getGraphQlOperations();
12✔
164

165
            $graphQlOperationsWithDefaults = [];
12✔
166
            foreach ($graphQlOperations as $operation) {
12✔
167
                [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
12✔
168
                if ($parameters) {
12✔
169
                    $operation = $this->mergeOperationParameters($operation, $parameters);
2✔
170
                }
171

172
                $graphQlOperationsWithDefaults[$key] = $operation;
12✔
173
            }
174

175
            $resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperationsWithDefaults);
12✔
176
        }
177

178
        return $resources;
118✔
179
    }
180

181
    /**
182
     * Does the resource already have an operation of the $operationClass type?
183
     * Useful to determine if we need to create a new ApiResource when the class has only operation attributes, for example:.
184
     *
185
     * #[Get]
186
     * #[Get(uriTemplate: '/alternate')]
187
     * class Example {}
188
     */
189
    private function hasSameOperation(ApiResource $resource, string $operationClass, HttpOperation $operation): bool
190
    {
191
        foreach ($resource->getOperations() ?? [] as $o) {
38✔
192
            if ($o instanceof $operationClass && $operation->getUriTemplate() === $o->getUriTemplate() && $operation->getName() === $o->getName() && $operation->getRouteName() === $o->getRouteName()) {
20✔
193
                return true;
×
194
            }
195
        }
196

197
        return false;
38✔
198
    }
199

200
    /**
201
     * @template T of Metadata
202
     *
203
     * @param T $resource
204
     *
205
     * @return T
206
     */
207
    private function mergeOperationParameters(Metadata $resource, Parameters $globalParameters): Metadata
208
    {
209
        $parameters = $resource->getParameters() ?? new Parameters();
4✔
210
        foreach ($globalParameters as $parameterName => $parameter) {
4✔
211
            if ($key = $parameter->getKey()) {
4✔
212
                $parameterName = $key;
4✔
213
            }
214

215
            if (!$parameters->has($parameterName, $parameter::class)) {
4✔
216
                $parameters->add($parameterName, $parameter);
4✔
217
            }
218
        }
219

220
        return $resource->withParameters($parameters);
4✔
221
    }
222
}
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