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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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\CollectionType;
21
use Symfony\Component\TypeInfo\Type\GenericType;
22
use Symfony\Component\TypeInfo\Type\IntersectionType;
23
use Symfony\Component\TypeInfo\Type\NullableType;
24
use Symfony\Component\TypeInfo\Type\ObjectType;
25
use Symfony\Component\TypeInfo\Type\UnionType;
26
use Symfony\Component\TypeInfo\TypeIdentifier;
27

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

50
        $types = [];
×
51
        $nullable = false;
×
52

53
        foreach (array_map(self::convertLegacyTypeToType(...), $legacyTypes) as $type) {
×
54
            if ($type->isNullable()) {
×
55
                $nullable = true;
×
56

57
                if ($type instanceof BuiltinType && TypeIdentifier::NULL === $type->getTypeIdentifier()) {
×
58
                    continue;
×
59
                }
60

61
                $type = self::unwrapNullableType($type);
×
62
            }
63

64
            if ($type instanceof UnionType) {
×
65
                $types = [$types, ...$type->getTypes()];
×
66

67
                continue;
×
68
            }
69

70
            $types[] = $type;
×
71
        }
72

73
        if ($nullable && [] === $types) {
×
74
            return Type::null();
×
75
        }
76

77
        $type = \count($types) > 1 ? Type::union(...$types) : $types[0];
×
78
        if ($nullable) {
×
79
            $type = Type::nullable($type);
×
80
        }
81

82
        return $type;
×
83
    }
84

85
    /**
86
     * @param list<LegacyType> $collectionKeyTypes
87
     * @param list<LegacyType> $collectionValueTypes
88
     */
89
    public static function createTypeFromLegacyValues(string $builtinType, bool $nullable, ?string $class, bool $collection, array $collectionKeyTypes, array $collectionValueTypes): Type
90
    {
91
        $variableTypes = [];
×
92

93
        if ($collectionKeyTypes) {
×
94
            $collectionKeyTypes = array_unique(array_map(self::convertLegacyTypeToType(...), $collectionKeyTypes));
×
95
            $variableTypes[] = \count($collectionKeyTypes) > 1 ? Type::union(...$collectionKeyTypes) : $collectionKeyTypes[0];
×
96
        }
97

98
        if ($collectionValueTypes) {
×
99
            if (!$collectionKeyTypes) {
×
100
                $variableTypes[] = \is_array($collectionKeyTypes) ? Type::mixed() : Type::union(Type::int(), Type::string()); // @phpstan-ignore-line
×
101
            }
102

103
            $collectionValueTypes = array_unique(array_map(self::convertLegacyTypeToType(...), $collectionValueTypes));
×
104
            $variableTypes[] = \count($collectionValueTypes) > 1 ? Type::union(...$collectionValueTypes) : $collectionValueTypes[0];
×
105
        }
106

107
        if ($collectionKeyTypes && !$collectionValueTypes) {
×
108
            $variableTypes[] = Type::mixed();
×
109
        }
110

111
        try {
112
            $type = null !== $class ? Type::object($class) : Type::builtin(TypeIdentifier::from($builtinType));
×
113
        } catch (\ValueError) {
×
114
            throw new InvalidArgumentException(\sprintf('"%s" is not a valid PHP type.', $builtinType));
×
115
        }
116

117
        if (\count($variableTypes)) {
×
118
            // hack to have generic without classname
119
            // this is required because some tests are using invalid data
120
            if (null === $class && 'object' === $builtinType) {
×
121
                $type = Type::object(\stdClass::class);
×
122
            }
123
            $type = Type::generic($type, ...$variableTypes);
×
124
        }
125

126
        if ($collection) {
×
127
            $type = Type::collection($type);
×
128
        }
129

130
        if ($nullable && !$type->isNullable()) {
×
131
            $type = Type::nullable($type);
×
132
        }
133

134
        return $type;
×
135
    }
136

137
    public static function unwrapNullableType(Type $type): Type
138
    {
139
        // BC layer for "symfony/type-info" < 7.2
140
        if (method_exists($type, 'asNonNullable')) {
×
141
            return (!$type instanceof UnionType) ? $type : $type->asNonNullable();
×
142
        }
143

144
        if (!$type instanceof NullableType) {
×
145
            return $type;
×
146
        }
147

148
        return $type->getWrappedType();
×
149
    }
150

151
    /**
152
     * Recursive method that converts {@see LegacyType} to its related {@see Type}.
153
     */
154
    private static function convertLegacyTypeToType(LegacyType $legacyType): Type
155
    {
156
        return self::createTypeFromLegacyValues(
×
157
            $legacyType->getBuiltinType(),
×
158
            $legacyType->isNullable(),
×
159
            $legacyType->getClassName(),
×
160
            $legacyType->isCollection(),
×
161
            $legacyType->getCollectionKeyTypes(),
×
162
            $legacyType->getCollectionValueTypes(),
×
163
        );
×
164
    }
165

166
    /**
167
     * Converts a {@see Type} to what is should have been in the "symfony/property-info" component.
168
     *
169
     * @return list<LegacyType>|null
170
     */
171
    public static function convertTypeToLegacyTypes(?Type $type): ?array
172
    {
UNCOV
173
        if (null === $type) {
×
UNCOV
174
            return null;
×
175
        }
176

UNCOV
177
        if (\in_array((string) $type, ['mixed', 'never'], true)) {
×
178
            return null;
×
179
        }
180

UNCOV
181
        if (\in_array((string) $type, ['null', 'void'], true)) {
×
182
            return [new LegacyType('null')];
×
183
        }
184

UNCOV
185
        $legacyType = self::convertTypeToLegacy($type);
×
186

UNCOV
187
        if (!\is_array($legacyType)) {
×
UNCOV
188
            $legacyType = [$legacyType];
×
189
        }
190

UNCOV
191
        return $legacyType;
×
192
    }
193

194
    /**
195
     * Recursive method that converts {@see Type} to its related {@see LegacyType} (or list of {@see @LegacyType}).
196
     *
197
     * @return LegacyType|list<LegacyType>
198
     */
199
    private static function convertTypeToLegacy(Type $type): LegacyType|array
200
    {
UNCOV
201
        $nullable = false;
×
202

UNCOV
203
        if ($type instanceof NullableType) {
×
UNCOV
204
            $nullable = true;
×
UNCOV
205
            $type = $type->getWrappedType();
×
206
        }
207

UNCOV
208
        if ($type instanceof UnionType) {
×
UNCOV
209
            $unionTypes = [];
×
UNCOV
210
            foreach ($type->getTypes() as $t) {
×
UNCOV
211
                if ($t instanceof IntersectionType) {
×
212
                    throw new \LogicException(\sprintf('DNF types are not supported by "%s".', LegacyType::class));
×
213
                }
214

UNCOV
215
                if ($nullable) {
×
UNCOV
216
                    $t = Type::nullable($t);
×
217
                }
218

UNCOV
219
                $unionTypes[] = $t;
×
220
            }
221

222
            /** @var list<LegacyType> $legacyTypes */
UNCOV
223
            $legacyTypes = array_map(self::convertTypeToLegacy(...), $unionTypes);
×
224

UNCOV
225
            if (1 === \count($legacyTypes)) {
×
226
                return $legacyTypes[0];
×
227
            }
228

UNCOV
229
            return $legacyTypes;
×
230
        }
231

UNCOV
232
        if ($type instanceof IntersectionType) {
×
233
            /** @var list<LegacyType> $legacyTypes */
UNCOV
234
            $legacyTypes = array_map(self::convertTypeToLegacy(...), $type->getTypes());
×
235

UNCOV
236
            if (1 === \count($legacyTypes)) {
×
237
                return $legacyTypes[0];
×
238
            }
239

UNCOV
240
            return $legacyTypes;
×
241
        }
242

UNCOV
243
        if ($type instanceof CollectionType) {
×
UNCOV
244
            $type = $type->getWrappedType();
×
UNCOV
245
            if ($nullable) {
×
UNCOV
246
                $type = Type::nullable($type);
×
247
            }
248

UNCOV
249
            return self::convertTypeToLegacy($type);
×
250
        }
251

UNCOV
252
        $typeIdentifier = TypeIdentifier::MIXED;
×
UNCOV
253
        $className = null;
×
UNCOV
254
        $collectionKeyType = $collectionValueType = null;
×
255

UNCOV
256
        if ($type instanceof GenericType) {
×
UNCOV
257
            $wrappedType = $type->getWrappedType();
×
258

UNCOV
259
            if ($wrappedType instanceof BuiltinType) {
×
UNCOV
260
                $typeIdentifier = $wrappedType->getTypeIdentifier();
×
UNCOV
261
            } elseif ($wrappedType instanceof ObjectType) {
×
UNCOV
262
                $typeIdentifier = TypeIdentifier::OBJECT;
×
UNCOV
263
                $className = $wrappedType->getClassName();
×
264
            }
265

UNCOV
266
            $variableTypes = $type->getVariableTypes();
×
267

UNCOV
268
            if (2 === \count($variableTypes)) {
×
UNCOV
269
                if ('int|string' !== (string) $variableTypes[0]) {
×
UNCOV
270
                    $collectionKeyType = self::convertTypeToLegacy($variableTypes[0]);
×
271
                }
UNCOV
272
                $collectionValueType = self::convertTypeToLegacy($variableTypes[1]);
×
273
            } elseif (1 === \count($variableTypes)) {
×
274
                $collectionValueType = self::convertTypeToLegacy($variableTypes[0]);
×
275
            }
UNCOV
276
        } elseif ($type instanceof ObjectType) {
×
UNCOV
277
            $typeIdentifier = TypeIdentifier::OBJECT;
×
UNCOV
278
            $className = $type->getClassName();
×
UNCOV
279
        } elseif ($type instanceof BuiltinType) {
×
UNCOV
280
            $typeIdentifier = $type->getTypeIdentifier();
×
281
        }
282

UNCOV
283
        if (TypeIdentifier::MIXED === $typeIdentifier) {
×
284
            return [
×
285
                new LegacyType(LegacyType::BUILTIN_TYPE_INT, true),
×
286
                new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT, true),
×
287
                new LegacyType(LegacyType::BUILTIN_TYPE_STRING, true),
×
288
                new LegacyType(LegacyType::BUILTIN_TYPE_BOOL, true),
×
289
                new LegacyType(LegacyType::BUILTIN_TYPE_RESOURCE, true),
×
290
                new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, true),
×
291
                new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, true),
×
292
                new LegacyType(LegacyType::BUILTIN_TYPE_NULL, true),
×
293
                new LegacyType(LegacyType::BUILTIN_TYPE_CALLABLE, true),
×
294
                new LegacyType(LegacyType::BUILTIN_TYPE_ITERABLE, true),
×
295
            ];
×
296
        }
297

UNCOV
298
        return new LegacyType(
×
UNCOV
299
            builtinType: $typeIdentifier->value,
×
UNCOV
300
            nullable: $nullable,
×
UNCOV
301
            class: $className,
×
UNCOV
302
            collection: $type instanceof GenericType,
×
UNCOV
303
            collectionKeyType: $collectionKeyType,
×
UNCOV
304
            collectionValueType: $collectionValueType,
×
UNCOV
305
        );
×
306
    }
307
}
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