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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 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
    {
42
        if (!$legacyTypes) {
×
43
            return null;
×
44
        }
45

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

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

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

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

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

63
                continue;
×
64
            }
65

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

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

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

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
    {
87
        $variableTypes = [];
×
88

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

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

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

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

107
        try {
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

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

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

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

125
        return $type;
×
126
    }
127

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

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

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
    {
147
        return self::createTypeFromLegacyValues(
×
148
            $legacyType->getBuiltinType(),
×
149
            $legacyType->isNullable(),
×
150
            $legacyType->getClassName(),
×
151
            $legacyType->isCollection(),
×
152
            $legacyType->getCollectionKeyTypes(),
×
153
            $legacyType->getCollectionValueTypes(),
×
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