Debian Codegraph @

vendor/symfony/type-info/Type/UnionType.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 namespace Symfony\Component\TypeInfo\Type;
13
14 use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
15 use Symfony\Component\TypeInfo\Type;
16 use Symfony\Component\TypeInfo\TypeIdentifier;
17
18 /**
19 * @author Mathias Arlaud <[email protected]>
20 * @author Baptiste Leduc <[email protected]>
21 *
22 * @template T of Type
23 *
24 * @implements CompositeTypeInterface<T>
25 */
26 class UnionType extends Type implements CompositeTypeInterface
27 {
28 /**
29 * @var list<T>
30 */
31 private readonly array $types;
32
33 /**
34 * @param list<T> $types
35 */
36 public function __construct(Type ...$types)
37 {
38 if (\count($types) < 2) {
39 throw new InvalidArgumentException(\sprintf('"%s" expects at least 2 types.', self::class));
40 }
41
42 foreach ($types as $type) {
43 if ($type instanceof self) {
44 throw new InvalidArgumentException(\sprintf('Cannot set "%s" as a "%1$s" part.', self::class));
45 }
46
47 if ($type instanceof BuiltinType) {
48 if (TypeIdentifier::NULL === $type->getTypeIdentifier() && !is_a(static::class, NullableType::class, allow_string: true)) {
49 throw new InvalidArgumentException(\sprintf('Cannot create union with "null", please use "%s" instead.', NullableType::class));
50 }
51
52 if ($type->getTypeIdentifier()->isStandalone()) {
53 throw new InvalidArgumentException(\sprintf('Cannot create union with "%s" standalone type.', $type));
54 }
55 }
56 }
57
58 usort($types, static fn (Type $a, Type $b): int => (string) $a <=> (string) $b);
59 $this->types = array_values(array_unique($types));
60
61 $builtinTypesIdentifiers = array_map(
62 static fn (BuiltinType $t): TypeIdentifier => $t->getTypeIdentifier(),
63 array_filter($this->types, static fn (Type $t): bool => $t instanceof BuiltinType),
64 );
65
66 if ((\in_array(TypeIdentifier::TRUE, $builtinTypesIdentifiers, true) || \in_array(TypeIdentifier::FALSE, $builtinTypesIdentifiers, true)) && \in_array(TypeIdentifier::BOOL, $builtinTypesIdentifiers, true)) {
67 throw new InvalidArgumentException('Cannot create union with redundant boolean type.');
68 }
69
70 if (\in_array(TypeIdentifier::TRUE, $builtinTypesIdentifiers, true) && \in_array(TypeIdentifier::FALSE, $builtinTypesIdentifiers, true)) {
71 throw new InvalidArgumentException('Cannot create union with both "true" and "false", "bool" should be used instead.');
72 }
73
74 if (\in_array(TypeIdentifier::OBJECT, $builtinTypesIdentifiers, true) && \count(array_filter($this->types, static fn (Type $t): bool => $t instanceof ObjectType))) {
75 throw new InvalidArgumentException('Cannot create union with both "object" and class type.');
76 }
77 }
78
79 /**
80 * @return list<T>
81 */
82 public function getTypes(): array
83 {
84 return $this->types;
85 }
86
87 public function composedTypesAreSatisfiedBy(callable $specification): bool
88 {
89 foreach ($this->types as $type) {
90 if ($type->isSatisfiedBy($specification)) {
91 return true;
92 }
93 }
94
95 return false;
96 }
97
98 public function __toString(): string
99 {
100 $string = '';
101 $glue = '';
102
103 foreach ($this->types as $t) {
104 $string .= $glue.($t instanceof CompositeTypeInterface ? '('.$t.')' : $t);
105 $glue = '|';
106 }
107
108 return $string;
109 }
110 }