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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

74.07
/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) {
50✔
47
            return null;
×
48
        }
49

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

53
        foreach (array_map(self::convertLegacyTypeToType(...), $legacyTypes) as $type) {
50✔
54
            if ($type->isNullable()) {
50✔
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) {
50✔
65
                $types = [$types, ...$type->getTypes()];
×
66

67
                continue;
×
68
            }
69

70
            $types[] = $type;
50✔
71
        }
72

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

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

82
        return $type;
50✔
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 = [];
50✔
92

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

98
        if ($collectionValueTypes) {
50✔
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) {
50✔
108
            $variableTypes[] = Type::mixed();
×
109
        }
110

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

117
        if (\count($variableTypes)) {
50✔
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) {
50✔
127
            $type = Type::collection($type);
×
128
        }
129

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

134
        return $type;
50✔
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(
50✔
157
            $legacyType->getBuiltinType(),
50✔
158
            $legacyType->isNullable(),
50✔
159
            $legacyType->getClassName(),
50✔
160
            $legacyType->isCollection(),
50✔
161
            $legacyType->getCollectionKeyTypes(),
50✔
162
            $legacyType->getCollectionValueTypes(),
50✔
163
        );
50✔
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
    {
173
        if (null === $type) {
312✔
174
            return null;
46✔
175
        }
176

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

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

185
        $legacyType = self::convertTypeToLegacy($type);
297✔
186

187
        if (!\is_array($legacyType)) {
297✔
188
            $legacyType = [$legacyType];
295✔
189
        }
190

191
        return $legacyType;
297✔
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
    {
201
        $nullable = false;
297✔
202

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

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

215
                if ($nullable) {
11✔
216
                    $t = Type::nullable($t);
10✔
217
                }
218

219
                $unionTypes[] = $t;
11✔
220
            }
221

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

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

229
            return $legacyTypes;
11✔
230
        }
231

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

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

240
            return $legacyTypes;
5✔
241
        }
242

243
        if ($type instanceof CollectionType) {
297✔
244
            $type = $type->getWrappedType();
108✔
245
            if ($nullable) {
108✔
246
                $type = Type::nullable($type);
35✔
247
            }
248

249
            return self::convertTypeToLegacy($type);
108✔
250
        }
251

252
        $typeIdentifier = TypeIdentifier::MIXED;
297✔
253
        $className = null;
297✔
254
        $collectionKeyType = $collectionValueType = null;
297✔
255

256
        if ($type instanceof GenericType) {
297✔
257
            $wrappedType = $type->getWrappedType();
84✔
258

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

266
            $variableTypes = $type->getVariableTypes();
84✔
267

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

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

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