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

api-platform / core / 15955912273

29 Jun 2025 01:51PM UTC coverage: 22.057% (-0.03%) from 22.082%
15955912273

Pull #7249

github

web-flow
Merge d9904d788 into a42034dc3
Pull Request #7249: chore: solve some phpstan issues

0 of 9 new or added lines in 8 files covered. (0.0%)

11540 existing lines in 372 files now uncovered.

11522 of 52237 relevant lines covered (22.06%)

11.08 hits per line

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

93.33
/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.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\Validator\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Parameter;
18
use ApiPlatform\Metadata\Parameters;
19
use ApiPlatform\Metadata\QueryParameter;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
23
use Psr\Container\ContainerInterface;
24
use Symfony\Component\TypeInfo\Type\CollectionType;
25
use Symfony\Component\TypeInfo\Type\UnionType;
26
use Symfony\Component\Validator\Constraints\All;
27
use Symfony\Component\Validator\Constraints\Choice;
28
use Symfony\Component\Validator\Constraints\Collection;
29
use Symfony\Component\Validator\Constraints\Count;
30
use Symfony\Component\Validator\Constraints\DivisibleBy;
31
use Symfony\Component\Validator\Constraints\GreaterThan;
32
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
33
use Symfony\Component\Validator\Constraints\Length;
34
use Symfony\Component\Validator\Constraints\LessThan;
35
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
36
use Symfony\Component\Validator\Constraints\NotBlank;
37
use Symfony\Component\Validator\Constraints\NotNull;
38
use Symfony\Component\Validator\Constraints\Range;
39
use Symfony\Component\Validator\Constraints\Regex;
40
use Symfony\Component\Validator\Constraints\Type;
41
use Symfony\Component\Validator\Constraints\Unique;
42

43
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
44
{
45
    public function __construct(
46
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
47
        private readonly ?ContainerInterface $filterLocator = null,
48
    ) {
UNCOV
49
    }
291✔
50

51
    public function create(string $resourceClass): ResourceMetadataCollection
52
    {
UNCOV
53
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
50✔
54

UNCOV
55
        foreach ($resourceMetadataCollection as $i => $resource) {
50✔
UNCOV
56
            $operations = $resource->getOperations();
49✔
57

UNCOV
58
            foreach ($operations as $operationName => $operation) {
49✔
UNCOV
59
                $parameters = $operation->getParameters() ?? new Parameters();
49✔
UNCOV
60
                foreach ($parameters as $key => $parameter) {
49✔
UNCOV
61
                    $parameters->add($key, $this->addSchemaValidation($parameter));
14✔
62
                }
63

64
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
UNCOV
65
                if ($operation->getFilters() && 0 === $parameters->count()) {
49✔
UNCOV
66
                    $parameters = $this->addFilterValidation($operation);
3✔
67
                }
68

UNCOV
69
                if (\count($parameters) > 0) {
49✔
UNCOV
70
                    $operations->add($operationName, $operation->withParameters($parameters));
17✔
71
                }
72
            }
73

UNCOV
74
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
49✔
75

UNCOV
76
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
49✔
UNCOV
77
                continue;
24✔
78
            }
79

UNCOV
80
            foreach ($graphQlOperations as $operationName => $operation) {
36✔
UNCOV
81
                $parameters = $operation->getParameters() ?? new Parameters();
36✔
UNCOV
82
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
36✔
UNCOV
83
                    $parameters->add($key, $this->addSchemaValidation($parameter));
1✔
84
                }
85

UNCOV
86
                if (\count($parameters) > 0) {
36✔
UNCOV
87
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
1✔
88
                }
89
            }
90

UNCOV
91
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
36✔
92
        }
93

UNCOV
94
        return $resourceMetadataCollection;
50✔
95
    }
96

97
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
98
    {
UNCOV
99
        if (null !== $parameter->getConstraints()) {
17✔
UNCOV
100
            return $parameter;
2✔
101
        }
102

UNCOV
103
        $schema ??= $parameter->getSchema();
16✔
UNCOV
104
        $required ??= $parameter->getRequired() ?? false;
16✔
UNCOV
105
        $openApi ??= $parameter->getOpenApi();
16✔
106

107
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
108
        // only getAllowEmptyValue is used here anyways
UNCOV
109
        if (\is_array($openApi)) {
16✔
UNCOV
110
            $openApi = $openApi[0];
4✔
UNCOV
111
        } elseif (false === $openApi) {
15✔
UNCOV
112
            $openApi = null;
4✔
113
        }
114

UNCOV
115
        $assertions = [];
16✔
UNCOV
116
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
16✔
UNCOV
117
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
16✔
UNCOV
118
            $assertions[] = new NotBlank(allowNull: !$required);
1✔
119
        }
120

UNCOV
121
        $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null;
16✔
UNCOV
122
        $exclusiveMinimum = isset($schema['exclusiveMinimum']);
16✔
UNCOV
123
        $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null;
16✔
UNCOV
124
        $exclusiveMaximum = isset($schema['exclusiveMaximum']);
16✔
125

UNCOV
126
        if ($minimum && $maximum) {
16✔
UNCOV
127
            if (!$exclusiveMinimum && !$exclusiveMaximum) {
1✔
UNCOV
128
                $assertions[] = new Range(min: $minimum, max: $maximum);
1✔
129
            } else {
UNCOV
130
                $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
1✔
UNCOV
131
                $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
1✔
132
            }
UNCOV
133
        } elseif ($minimum) {
16✔
134
            $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
×
UNCOV
135
        } elseif ($maximum) {
16✔
136
            $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
×
137
        }
138

UNCOV
139
        if (isset($schema['pattern'])) {
16✔
UNCOV
140
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
1✔
141
        }
142

UNCOV
143
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
16✔
UNCOV
144
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
1✔
145
        }
146

UNCOV
147
        if (isset($schema['multipleOf'])) {
16✔
UNCOV
148
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
1✔
149
        }
150

UNCOV
151
        if (isset($schema['enum'])) {
16✔
UNCOV
152
            $assertions[] = new Choice(choices: $schema['enum']);
5✔
153
        }
154

UNCOV
155
        if ($properties = $parameter->getExtraProperties()['_properties'] ?? []) {
16✔
UNCOV
156
            $fields = [];
4✔
UNCOV
157
            foreach ($properties as $propertyName) {
4✔
UNCOV
158
                $fields[$propertyName] = $assertions;
4✔
159
            }
160

UNCOV
161
            return $parameter->withConstraints(new Collection(fields: $fields, allowMissingFields: true));
4✔
162
        }
163

UNCOV
164
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
15✔
UNCOV
165
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
15✔
166

167
        // type-info 7.2
UNCOV
168
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
15✔
169
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
170
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
171
                    break;
×
172
                }
173
            }
174
        }
175

UNCOV
176
        if ($isCollection) {
15✔
UNCOV
177
            $assertions = $assertions ? [new All($assertions)] : [];
6✔
178
        }
179

UNCOV
180
        if ($required && false !== $allowEmptyValue) {
15✔
UNCOV
181
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
1✔
182
        }
183

UNCOV
184
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
15✔
UNCOV
185
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
1✔
186
        }
187

UNCOV
188
        if ($schema['uniqueItems'] ?? false) {
15✔
UNCOV
189
            $assertions[] = new Unique();
1✔
190
        }
191

UNCOV
192
        if (isset($schema['type']) && 'array' === $schema['type']) {
15✔
UNCOV
193
            $assertions[] = new Type(type: 'array');
1✔
194
        }
195

UNCOV
196
        if (!$assertions) {
15✔
UNCOV
197
            return $parameter;
14✔
198
        }
199

UNCOV
200
        if (1 === \count($assertions)) {
3✔
UNCOV
201
            return $parameter->withConstraints($assertions[0]);
3✔
202
        }
203

UNCOV
204
        return $parameter->withConstraints($assertions);
1✔
205
    }
206

207
    private function addFilterValidation(HttpOperation $operation): Parameters
208
    {
UNCOV
209
        $parameters = new Parameters();
3✔
UNCOV
210
        $internalPriority = -1;
3✔
211

UNCOV
212
        foreach ($operation->getFilters() as $filter) {
3✔
UNCOV
213
            if (!$this->filterLocator->has($filter)) {
3✔
214
                continue;
×
215
            }
216

UNCOV
217
            $filter = $this->filterLocator->get($filter);
3✔
UNCOV
218
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
3✔
UNCOV
219
                $key = $parameterName;
3✔
UNCOV
220
                $required = $definition['required'] ?? false;
3✔
UNCOV
221
                $schema = $definition['schema'] ?? null;
3✔
222

UNCOV
223
                $openApi = null;
3✔
UNCOV
224
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
3✔
UNCOV
225
                    $openApi = $definition['openapi'];
1✔
226
                }
227

228
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
229
                if (true === $required && !$openApi) {
3✔
230
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
×
231
                }
232

UNCOV
233
                $parameters->add($key, $this->addSchemaValidation(
3✔
234
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
235
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
3✔
UNCOV
236
                    $schema,
3✔
UNCOV
237
                    $required,
3✔
UNCOV
238
                    $openApi
3✔
UNCOV
239
                ));
3✔
240
            }
241
        }
242

UNCOV
243
        return $parameters;
3✔
244
    }
245
}
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