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

api-platform / core / 10903050455

17 Sep 2024 12:29PM UTC coverage: 7.684% (+0.7%) from 6.96%
10903050455

push

github

web-flow
fix: swagger ui with route identifier (#6616)

2 of 6 new or added lines in 6 files covered. (33.33%)

9000 existing lines in 286 files now uncovered.

12668 of 164863 relevant lines covered (7.68%)

22.93 hits per line

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

84.62
/src/Metadata/Util/PropertyInfoToTypeInfoHelper.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\Util;
15

16
use Symfony\Component\PropertyInfo\Type as LegacyType;
17
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
18
use Symfony\Component\TypeInfo\Type;
19
use Symfony\Component\TypeInfo\Type\BuiltinType;
20
use Symfony\Component\TypeInfo\Type\UnionType;
21
use Symfony\Component\TypeInfo\TypeIdentifier;
22

23
/**
24
 * A helper about PropertyInfo Type conversion.
25
 *
26
 * @see https://github.com/mtarld/symfony/commits/backup/chore/deprecate-property-info-type/
27
 *
28
 * @author Mathias Arlaud <mathias.arlaud@gmail.com>
29
 *
30
 * @internal
31
 */
32
final class PropertyInfoToTypeInfoHelper
33
{
34
    /**
35
     * Converts a {@see LegacyType} to what is should have been in the "symfony/type-info" component.
36
     *
37
     * @param list<LegacyType>|null $legacyTypes
38
     */
39
    public static function convertLegacyTypesToType(?array $legacyTypes): ?Type
40
    {
UNCOV
41
        if (!$legacyTypes) {
588✔
UNCOV
42
            return null;
158✔
43
        }
44

UNCOV
45
        $types = [];
500✔
UNCOV
46
        $nullable = false;
500✔
47

UNCOV
48
        foreach (array_map(self::convertLegacyTypeToType(...), $legacyTypes) as $type) {
500✔
UNCOV
49
            if ($type->isNullable()) {
500✔
UNCOV
50
                $nullable = true;
234✔
51

UNCOV
52
                if ($type instanceof BuiltinType && TypeIdentifier::NULL === $type->getTypeIdentifier()) {
234✔
53
                    continue;
×
54
                }
55

UNCOV
56
                $type = self::unwrapNullableType($type);
234✔
57
            }
58

UNCOV
59
            if ($type instanceof UnionType) {
500✔
60
                $types = [$types, ...$type->getTypes()];
×
61

62
                continue;
×
63
            }
64

UNCOV
65
            $types[] = $type;
500✔
66
        }
67

UNCOV
68
        if ($nullable && [] === $types) {
500✔
69
            return Type::null();
×
70
        }
71

UNCOV
72
        $type = \count($types) > 1 ? Type::union(...$types) : $types[0];
500✔
UNCOV
73
        if ($nullable) {
500✔
UNCOV
74
            $type = Type::nullable($type);
234✔
75
        }
76

UNCOV
77
        return $type;
500✔
78
    }
79

80
    /**
81
     * @param list<LegacyType> $collectionKeyTypes
82
     * @param list<LegacyType> $collectionValueTypes
83
     */
84
    public static function createTypeFromLegacyValues(string $builtinType, bool $nullable, ?string $class, bool $collection, array $collectionKeyTypes, array $collectionValueTypes): Type
85
    {
UNCOV
86
        $variableTypes = [];
500✔
87

UNCOV
88
        if ($collectionKeyTypes) {
500✔
UNCOV
89
            $collectionKeyTypes = array_unique(array_map(self::convertLegacyTypeToType(...), $collectionKeyTypes));
198✔
UNCOV
90
            $variableTypes[] = \count($collectionKeyTypes) > 1 ? Type::union(...$collectionKeyTypes) : $collectionKeyTypes[0];
198✔
91
        }
92

UNCOV
93
        if ($collectionValueTypes) {
500✔
UNCOV
94
            if (!$collectionKeyTypes) {
198✔
95
                $variableTypes[] = \is_array($collectionKeyTypes) ? Type::mixed() : Type::union(Type::int(), Type::string()); // @phpstan-ignore-line
×
96
            }
97

UNCOV
98
            $collectionValueTypes = array_unique(array_map(self::convertLegacyTypeToType(...), $collectionValueTypes));
198✔
UNCOV
99
            $variableTypes[] = \count($collectionValueTypes) > 1 ? Type::union(...$collectionValueTypes) : $collectionValueTypes[0];
198✔
100
        }
101

UNCOV
102
        if ($collectionKeyTypes && !$collectionValueTypes) {
500✔
UNCOV
103
            $variableTypes[] = Type::mixed();
52✔
104
        }
105

106
        try {
UNCOV
107
            $type = null !== $class ? Type::object($class) : Type::builtin(TypeIdentifier::from($builtinType));
500✔
108
        } catch (\ValueError) {
×
109
            throw new InvalidArgumentException(\sprintf('"%s" is not a valid PHP type.', $builtinType));
×
110
        }
111

UNCOV
112
        if (\count($variableTypes)) {
500✔
UNCOV
113
            $type = Type::generic($type, ...$variableTypes);
198✔
114
        }
115

UNCOV
116
        if ($collection) {
500✔
UNCOV
117
            $type = Type::collection($type);
198✔
118
        }
119

UNCOV
120
        if ($nullable && !$type->isNullable()) {
500✔
UNCOV
121
            $type = Type::nullable($type);
234✔
122
        }
123

UNCOV
124
        return $type;
500✔
125
    }
126

127
    public static function unwrapNullableType(Type $type): Type
128
    {
UNCOV
129
        if (!$type instanceof UnionType) {
234✔
130
            return $type;
×
131
        }
132

UNCOV
133
        return $type->asNonNullable();
234✔
134
    }
135

136
    /**
137
     * Recursive method that converts {@see LegacyType} to its related {@see Type}.
138
     */
139
    private static function convertLegacyTypeToType(LegacyType $legacyType): Type
140
    {
UNCOV
141
        return self::createTypeFromLegacyValues(
500✔
UNCOV
142
            $legacyType->getBuiltinType(),
500✔
UNCOV
143
            $legacyType->isNullable(),
500✔
UNCOV
144
            $legacyType->getClassName(),
500✔
UNCOV
145
            $legacyType->isCollection(),
500✔
UNCOV
146
            $legacyType->getCollectionKeyTypes(),
500✔
UNCOV
147
            $legacyType->getCollectionValueTypes(),
500✔
UNCOV
148
        );
500✔
149
    }
150
}
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