|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace PslUnionV2Test; |
| 4 | + |
| 5 | +use Psl\Type; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** |
| 10 | + * For PSL >= 2.0.0 (literal_scalar) |
| 11 | + */ |
| 12 | +class UnionTypes |
| 13 | +{ |
| 14 | + public function literalScalarUnion($input): void |
| 15 | + { |
| 16 | + $ab = Type\union(Type\literal_scalar('a'), Type\literal_scalar('b')); |
| 17 | + $out = $ab->coerce($input); |
| 18 | + assertType("'a'|'b'", $out); |
| 19 | + } |
| 20 | + |
| 21 | + public function mixedUnion($input): void |
| 22 | + { |
| 23 | + $intOrString = Type\union(Type\int(), Type\string()); |
| 24 | + $out = $intOrString->coerce($input); |
| 25 | + assertType('int|string', $out); |
| 26 | + } |
| 27 | + |
| 28 | + public function shapeWithLiteralUnion($input): void |
| 29 | + { |
| 30 | + $shape = Type\shape([ |
| 31 | + 'kind' => Type\union( |
| 32 | + Type\literal_scalar('a'), |
| 33 | + Type\literal_scalar('b'), |
| 34 | + Type\literal_scalar('c'), |
| 35 | + ), |
| 36 | + ]); |
| 37 | + $out = $shape->coerce($input); |
| 38 | + assertType("array{kind: 'a'|'b'|'c'}", $out); |
| 39 | + } |
| 40 | + |
| 41 | + public function singleArgUnion($input): void |
| 42 | + { |
| 43 | + $single = Type\union(Type\int()); |
| 44 | + $out = $single->coerce($input); |
| 45 | + assertType('int', $out); |
| 46 | + } |
| 47 | +} |
0 commit comments