| 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: | |
| 11: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/BenevolentUnionType.cpp')] |
| 12: | class BenevolentUnionType extends UnionType |
| 13: | { |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 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: | return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$resultTypes)); |
| 72: | } |
| 73: | |
| 74: | protected function pickFromTypes( |
| 75: | callable $getValues, |
| 76: | callable $criteria, |
| 77: | ): array |
| 78: | { |
| 79: | $values = []; |
| 80: | foreach ($this->getTypes() as $type) { |
| 81: | $innerValues = $getValues($type); |
| 82: | if ($innerValues === [] && $criteria($type)) { |
| 83: | return []; |
| 84: | } |
| 85: | |
| 86: | foreach ($innerValues as $innerType) { |
| 87: | $values[] = $innerType; |
| 88: | } |
| 89: | } |
| 90: | |
| 91: | return $values; |
| 92: | } |
| 93: | |
| 94: | public function getOffsetValueType(Type $offsetType): Type |
| 95: | { |
| 96: | $types = []; |
| 97: | foreach ($this->getTypes() as $innerType) { |
| 98: | $valueType = $innerType->getOffsetValueType($offsetType); |
| 99: | if ($valueType instanceof ErrorType) { |
| 100: | continue; |
| 101: | } |
| 102: | |
| 103: | $types[] = $valueType; |
| 104: | } |
| 105: | |
| 106: | if (count($types) === 0) { |
| 107: | return new ErrorType(); |
| 108: | } |
| 109: | |
| 110: | return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$types)); |
| 111: | } |
| 112: | |
| 113: | protected function unionResults(callable $getResult): TrinaryLogic |
| 114: | { |
| 115: | return TrinaryLogic::createNo()->lazyOr($this->getTypes(), $getResult); |
| 116: | } |
| 117: | |
| 118: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
| 119: | { |
| 120: | $result = AcceptsResult::createNo(); |
| 121: | foreach ($this->getTypes() as $innerType) { |
| 122: | $result = $result->or($acceptingType->accepts($innerType, $strictTypes)); |
| 123: | } |
| 124: | |
| 125: | return $result; |
| 126: | } |
| 127: | |
| 128: | public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
| 129: | { |
| 130: | $types = TemplateTypeMap::createEmpty(); |
| 131: | |
| 132: | foreach ($this->getTypes() as $type) { |
| 133: | $types = $types->benevolentUnion($type->inferTemplateTypes($receivedType)); |
| 134: | } |
| 135: | |
| 136: | return $types; |
| 137: | } |
| 138: | |
| 139: | public function inferTemplateTypesOn(Type $templateType): TemplateTypeMap |
| 140: | { |
| 141: | $types = TemplateTypeMap::createEmpty(); |
| 142: | |
| 143: | foreach ($this->getTypes() as $type) { |
| 144: | $types = $types->benevolentUnion($templateType->inferTemplateTypes($type)); |
| 145: | } |
| 146: | |
| 147: | return $types; |
| 148: | } |
| 149: | |
| 150: | public function traverse(callable $cb): Type |
| 151: | { |
| 152: | $types = []; |
| 153: | $changed = false; |
| 154: | |
| 155: | foreach ($this->getTypes() as $type) { |
| 156: | $newType = $cb($type); |
| 157: | if ($type !== $newType) { |
| 158: | $changed = true; |
| 159: | } |
| 160: | $types[] = $newType; |
| 161: | } |
| 162: | |
| 163: | if ($changed) { |
| 164: | return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$types)); |
| 165: | } |
| 166: | |
| 167: | return $this; |
| 168: | } |
| 169: | |
| 170: | public function traverseSimultaneously(Type $right, callable $cb): Type |
| 171: | { |
| 172: | $newType = parent::traverseSimultaneously($right, $cb); |
| 173: | if ($newType === $this) { |
| 174: | return $this; |
| 175: | } |
| 176: | |
| 177: | return TypeUtils::toBenevolentUnion($newType); |
| 178: | } |
| 179: | |
| 180: | public function toValueSet(): ValueSet |
| 181: | { |
| 182: | return parent::toValueSet()->toBenevolent(); |
| 183: | } |
| 184: | |
| 185: | } |
| 186: | |