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

api-platform / core / 14837364288

05 May 2025 01:16PM UTC coverage: 8.457% (-0.002%) from 8.459%
14837364288

Pull #7122

github

web-flow
Merge 6606a9d5c into f55606b01
Pull Request #7122: Cherry picks from main (deprecations)

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

2030 existing lines in 147 files now uncovered.

13397 of 158416 relevant lines covered (8.46%)

22.88 hits per line

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

83.33
/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\NullableType;
21
use Symfony\Component\TypeInfo\Type\UnionType;
22
use Symfony\Component\TypeInfo\TypeIdentifier;
23

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

UNCOV
46
        $types = [];
254✔
UNCOV
47
        $nullable = false;
254✔
48

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

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

UNCOV
57
                $type = self::unwrapNullableType($type);
118✔
58
            }
59

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

63
                continue;
×
64
            }
65

UNCOV
66
            $types[] = $type;
254✔
67
        }
68

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

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

UNCOV
78
        return $type;
254✔
79
    }
80

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

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

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

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

UNCOV
103
        if ($collectionKeyTypes && !$collectionValueTypes) {
254✔
UNCOV
104
            $variableTypes[] = Type::mixed();
26✔
105
        }
106

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

UNCOV
113
        if (\count($variableTypes)) {
254✔
UNCOV
114
            $type = Type::generic($type, ...$variableTypes);
101✔
115
        }
116

UNCOV
117
        if ($collection) {
254✔
UNCOV
118
            $type = Type::collection($type);
101✔
119
        }
120

UNCOV
121
        if ($nullable && !$type->isNullable()) {
254✔
UNCOV
122
            $type = Type::nullable($type);
118✔
123
        }
124

UNCOV
125
        return $type;
254✔
126
    }
127

128
    public static function unwrapNullableType(Type $type): Type
129
    {
130
        // BC layer for "symfony/type-info" < 7.2
UNCOV
131
        if (method_exists($type, 'asNonNullable')) {
118✔
132
            return (!$type instanceof UnionType) ? $type : $type->asNonNullable();
×
133
        }
134

UNCOV
135
        if (!$type instanceof NullableType) {
118✔
136
            return $type;
×
137
        }
138

UNCOV
139
        return $type->getWrappedType();
118✔
140
    }
141

142
    /**
143
     * Recursive method that converts {@see LegacyType} to its related {@see Type}.
144
     */
145
    private static function convertLegacyTypeToType(LegacyType $legacyType): Type
146
    {
UNCOV
147
        return self::createTypeFromLegacyValues(
254✔
UNCOV
148
            $legacyType->getBuiltinType(),
254✔
UNCOV
149
            $legacyType->isNullable(),
254✔
UNCOV
150
            $legacyType->getClassName(),
254✔
UNCOV
151
            $legacyType->isCollection(),
254✔
UNCOV
152
            $legacyType->getCollectionKeyTypes(),
254✔
UNCOV
153
            $legacyType->getCollectionValueTypes(),
254✔
UNCOV
154
        );
254✔
155
    }
156
}
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