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

api-platform / core / 14854964964

06 May 2025 08:21AM UTC coverage: 6.873% (-1.6%) from 8.459%
14854964964

Pull #7122

github

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

0 of 35 new or added lines in 9 files covered. (0.0%)

2031 existing lines in 148 files now uncovered.

10887 of 158407 relevant lines covered (6.87%)

6.19 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\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) {
×
UNCOV
43
            return null;
×
44
        }
45

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

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

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

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

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

63
                continue;
×
64
            }
65

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

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

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

UNCOV
78
        return $type;
×
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 = [];
×
88

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

UNCOV
94
        if ($collectionValueTypes) {
×
UNCOV
95
            if (!$collectionKeyTypes) {
×
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));
×
UNCOV
100
            $variableTypes[] = \count($collectionValueTypes) > 1 ? Type::union(...$collectionValueTypes) : $collectionValueTypes[0];
×
101
        }
102

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

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

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

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

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

UNCOV
125
        return $type;
×
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')) {
×
132
            return (!$type instanceof UnionType) ? $type : $type->asNonNullable();
×
133
        }
134

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

UNCOV
139
        return $type->getWrappedType();
×
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(
×
UNCOV
148
            $legacyType->getBuiltinType(),
×
UNCOV
149
            $legacyType->isNullable(),
×
UNCOV
150
            $legacyType->getClassName(),
×
UNCOV
151
            $legacyType->isCollection(),
×
UNCOV
152
            $legacyType->getCollectionKeyTypes(),
×
UNCOV
153
            $legacyType->getCollectionValueTypes(),
×
UNCOV
154
        );
×
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