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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

90.0
/src/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaChoiceRestriction.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\Symfony\Validator\Metadata\Property\Restriction;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use Symfony\Component\PropertyInfo\Type;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\Constraints\Choice;
20

21
/**
22
 * @author Tomas Norkūnas <norkunas.tom@gmail.com>
23
 */
24
final class PropertySchemaChoiceRestriction implements PropertySchemaRestrictionMetadataInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @param Choice $constraint
30
     */
31
    public function create(Constraint $constraint, ApiProperty $propertyMetadata): array
32
    {
UNCOV
33
        $choices = [];
29✔
34

UNCOV
35
        if (\is_callable($constraint->callback)) {
29✔
UNCOV
36
            $choices = ($constraint->callback)();
4✔
UNCOV
37
        } elseif (\is_array($constraint->choices)) {
25✔
UNCOV
38
            $choices = $constraint->choices;
25✔
39
        }
40

UNCOV
41
        if (!$choices) {
29✔
42
            return [];
×
43
        }
44

UNCOV
45
        $restriction = [];
29✔
46

UNCOV
47
        if (!$constraint->multiple) {
29✔
UNCOV
48
            $restriction['enum'] = $choices;
7✔
49

UNCOV
50
            return $restriction;
7✔
51
        }
52

UNCOV
53
        $restriction['type'] = 'array';
22✔
54

UNCOV
55
        $types = array_values(array_unique(array_map(fn (mixed $choice) => \is_string($choice) ? 'string' : 'number', $choices)));
22✔
56

UNCOV
57
        if ($count = \count($types)) {
22✔
UNCOV
58
            if (1 === $count) {
22✔
UNCOV
59
                $types = $types[0];
18✔
60
            }
61

UNCOV
62
            $restriction['items'] = ['type' => $types, 'enum' => $choices];
22✔
63
        }
64

UNCOV
65
        if (null !== $constraint->min) {
22✔
UNCOV
66
            $restriction['minItems'] = $constraint->min;
10✔
67
        }
68

UNCOV
69
        if (null !== $constraint->max) {
22✔
UNCOV
70
            $restriction['maxItems'] = $constraint->max;
10✔
71
        }
72

UNCOV
73
        return $restriction;
22✔
74
    }
75

76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function supports(Constraint $constraint, ApiProperty $propertyMetadata): bool
80
    {
UNCOV
81
        $types = array_map(static fn (Type $type) => $type->getBuiltinType(), $propertyMetadata->getBuiltinTypes() ?? []);
57✔
UNCOV
82
        if ($propertyMetadata->getExtraProperties()['nested_schema'] ?? false) {
57✔
83
            $types = [Type::BUILTIN_TYPE_STRING];
×
84
        }
85

86
        if (
UNCOV
87
            null !== ($builtinType = $propertyMetadata->getBuiltinTypes()[0] ?? null)
57✔
UNCOV
88
            && $builtinType->isCollection()
57✔
UNCOV
89
            && \count($builtinType->getCollectionValueTypes())
57✔
90
        ) {
91
            $types = array_unique(array_merge($types, array_map(static fn (Type $type) => $type->getBuiltinType(), $builtinType->getCollectionValueTypes())));
×
92
        }
93

UNCOV
94
        return $constraint instanceof Choice && \count($types) && array_intersect($types, [Type::BUILTIN_TYPE_STRING, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT]);
57✔
95
    }
96
}
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