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

api-platform / core / 11035928124

25 Sep 2024 03:10PM UTC coverage: 5.963% (-1.9%) from 7.833%
11035928124

push

github

dunglas
Merge branch '4.0'

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

2414 existing lines in 166 files now uncovered.

9834 of 164915 relevant lines covered (5.96%)

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

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

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

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

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

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

62
                continue;
×
63
            }
64

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

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

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

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

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

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

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

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

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

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

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

UNCOV
124
        return $type;
×
125
    }
126

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

UNCOV
133
        return $type->asNonNullable();
×
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(
×
UNCOV
142
            $legacyType->getBuiltinType(),
×
UNCOV
143
            $legacyType->isNullable(),
×
UNCOV
144
            $legacyType->getClassName(),
×
UNCOV
145
            $legacyType->isCollection(),
×
UNCOV
146
            $legacyType->getCollectionKeyTypes(),
×
UNCOV
147
            $legacyType->getCollectionValueTypes(),
×
UNCOV
148
        );
×
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