1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Turbo\ShadowedByTurboExtension;
7: use PHPStan\Type\Generic\TemplateTypeMap;
8: use function count;
9:
10: /** @api */
11: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/BenevolentUnionType.cpp')]
12: class BenevolentUnionType extends UnionType
13: {
14:
15: /**
16: * @api
17: * @param list<Type> $types
18: */
19: public function __construct(array $types, bool $normalized = false)
20: {
21: parent::__construct($types, $normalized);
22: }
23:
24: public function filterTypes(callable $filterCb): Type
25: {
26: $result = parent::filterTypes($filterCb);
27: if (!$result instanceof self && $result instanceof UnionType) {
28: return TypeUtils::toBenevolentUnion($result);
29: }
30:
31: return $result;
32: }
33:
34: public function tryRemove(Type $typeToRemove): ?Type
35: {
36: $result = parent::tryRemove($typeToRemove);
37: if ($result === null) {
38: return null;
39: }
40:
41: return TypeUtils::toBenevolentUnion($result);
42: }
43:
44: protected function unionTypes(callable $getType): Type
45: {
46: $changed = false;
47:
48: $resultTypes = [];
49: foreach ($this->getTypes() as $type) {
50: $result = $getType($type);
51: if ($result instanceof ErrorType) {
52: $changed = true;
53: continue;
54: }
55:
56: if ($result !== $type) {
57: $changed = true;
58: }
59:
60: $resultTypes[] = $result;
61: }
62:
63: if (!$changed) {
64: return $this;
65: }
66:
67: if (count($resultTypes) === 0) {
68: return new ErrorType();
69: }
70:
71: // a join of what the members answer
72: return TypeUtils::toBenevolentUnion(TypeCombinator::widen(TypeCombinator::union(...$resultTypes)));
73: }
74:
75: protected function pickFromTypes(
76: callable $getValues,
77: callable $criteria,
78: ): array
79: {
80: $values = [];
81: foreach ($this->getTypes() as $type) {
82: $innerValues = $getValues($type);
83: if ($innerValues === [] && $criteria($type)) {
84: return [];
85: }
86:
87: foreach ($innerValues as $innerType) {
88: $values[] = $innerType;
89: }
90: }
91:
92: return $values;
93: }
94:
95: public function getOffsetValueType(Type $offsetType): Type
96: {
97: $types = [];
98: foreach ($this->getTypes() as $innerType) {
99: $valueType = $innerType->getOffsetValueType($offsetType);
100: if ($valueType instanceof ErrorType) {
101: continue;
102: }
103:
104: $types[] = $valueType;
105: }
106:
107: if (count($types) === 0) {
108: return new ErrorType();
109: }
110:
111: return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$types));
112: }
113:
114: protected function unionResults(callable $getResult): TrinaryLogic
115: {
116: return TrinaryLogic::createNo()->lazyOr($this->getTypes(), $getResult);
117: }
118:
119: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
120: {
121: $result = AcceptsResult::createNo();
122: foreach ($this->getTypes() as $innerType) {
123: $result = $result->or($acceptingType->accepts($innerType, $strictTypes));
124: }
125:
126: return $result;
127: }
128:
129: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
130: {
131: $types = TemplateTypeMap::createEmpty();
132:
133: foreach ($this->getTypes() as $type) {
134: $types = $types->benevolentUnion($type->inferTemplateTypes($receivedType));
135: }
136:
137: return $types;
138: }
139:
140: public function inferTemplateTypesOn(Type $templateType): TemplateTypeMap
141: {
142: $types = TemplateTypeMap::createEmpty();
143:
144: foreach ($this->getTypes() as $type) {
145: $types = $types->benevolentUnion($templateType->inferTemplateTypes($type));
146: }
147:
148: return $types;
149: }
150:
151: public function traverse(callable $cb): Type
152: {
153: $types = [];
154: $changed = false;
155:
156: foreach ($this->getTypes() as $type) {
157: $newType = $cb($type);
158: if ($type !== $newType) {
159: $changed = true;
160: }
161: $types[] = $newType;
162: }
163:
164: if ($changed) {
165: return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$types));
166: }
167:
168: return $this;
169: }
170:
171: public function traverseSimultaneously(Type $right, callable $cb): Type
172: {
173: $newType = parent::traverseSimultaneously($right, $cb);
174: if ($newType === $this) {
175: return $this;
176: }
177:
178: return TypeUtils::toBenevolentUnion($newType);
179: }
180:
181: public function toValueSet(): ValueSet
182: {
183: return parent::toValueSet()->toBenevolent();
184: }
185:
186: }
187: