Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
6 / 12 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UnionTypeDefinition | |
50.00% |
6 / 12 |
|
60.00% |
3 / 5 |
13.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toTypescript | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| toJavascript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| providesDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsDefinitions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | public function __construct( |
| 16 | public TypescriptDeclarationList $types, |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | public function toTypescript(): string |
| 21 | { |
| 22 | return implode(' | ', array_map( |
| 23 | static fn (TypescriptTypeDeclarationInterface $type): string => '(' . $type->toTypescript() . ')', |
| 24 | $this->types->toArray(), |
| 25 | )); |
| 26 | } |
| 27 | |
| 28 | public function toJavascript(): string |
| 29 | { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | public function providesDefinitions(): JavascriptIdentifierList |
| 34 | { |
| 35 | return new JavascriptIdentifierList(); |
| 36 | } |
| 37 | |
| 38 | public function needsDefinitions(): JavascriptIdentifierList |
| 39 | { |
| 40 | $definitions = new JavascriptIdentifierList(); |
| 41 | foreach ($this->types as $type) { |
| 42 | foreach ($type->needsDefinitions() as $definition) { |
| 43 | $definitions = $definitions->append($definition); |
| 44 | } |
| 45 | } |
| 46 | return $definitions; |
| 47 | } |
| 48 | } |