Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UnionTypeDefinition | |
95.00% |
19 / 20 |
|
80.00% |
4 / 5 |
11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| toTypescript | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| toJavascript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| providesDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsDefinitions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto\Typehints; |
| 3 | |
| 4 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 5 | use Apie\TypescriptCodeBuilder\Lists\TypescriptDeclarationList; |
| 6 | use Apie\TypescriptCodeBuilder\TypescriptTypeDeclarationInterface; |
| 7 | |
| 8 | /** |
| 9 | * Typescript definition for a union type. |
| 10 | * |
| 11 | * @param array<int, TypescriptTypeDeclarationInterface> $types |
| 12 | */ |
| 13 | class UnionTypeDefinition implements TypescriptTypeDeclarationInterface |
| 14 | { |
| 15 | private bool $simple = true; |
| 16 | public function __construct( |
| 17 | public TypescriptDeclarationList $types, |
| 18 | ) { |
| 19 | foreach ($types as $type) { |
| 20 | if ($type instanceof IdentifierTypeDefinition || $type instanceof LiteralTypeDefinition) { |
| 21 | } else { |
| 22 | $this->simple = false; |
| 23 | break; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public function toTypescript(): string |
| 29 | { |
| 30 | if ($this->simple) { |
| 31 | return implode(' | ', array_map( |
| 32 | static fn (TypescriptTypeDeclarationInterface $type): string => $type->toTypescript(), |
| 33 | $this->types->toArray(), |
| 34 | )); |
| 35 | } |
| 36 | return implode(' | ', array_map( |
| 37 | static fn (TypescriptTypeDeclarationInterface $type): string => '(' . $type->toTypescript() . ')', |
| 38 | $this->types->toArray(), |
| 39 | )); |
| 40 | } |
| 41 | |
| 42 | public function toJavascript(): string |
| 43 | { |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | public function providesDefinitions(bool $applyBlockScope): JavascriptIdentifierList |
| 48 | { |
| 49 | return new JavascriptIdentifierList(); |
| 50 | } |
| 51 | |
| 52 | public function needsDefinitions(): JavascriptIdentifierList |
| 53 | { |
| 54 | $definitions = new JavascriptIdentifierList(); |
| 55 | foreach ($this->types as $type) { |
| 56 | foreach ($type->needsDefinitions() as $definition) { |
| 57 | $definitions = $definitions->append($definition); |
| 58 | } |
| 59 | } |
| 60 | return $definitions; |
| 61 | } |
| 62 | } |