Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
42.11% |
8 / 19 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ForStatement | |
42.11% |
8 / 19 |
|
50.00% |
3 / 6 |
34.48 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toTypescript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toJavascript | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsDefinitions | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| render | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| providesDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto\ControlFlow; |
| 3 | |
| 4 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 5 | use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface; |
| 6 | use Apie\TypescriptCodeBuilder\Utils\ControlFlowUtils; |
| 7 | |
| 8 | class ForStatement implements TypescriptFileExpressionInterface |
| 9 | { |
| 10 | public function __construct( |
| 11 | public ?TypescriptFileExpressionInterface $initialization, |
| 12 | public ?TypescriptFileExpressionInterface $condition, |
| 13 | public ?TypescriptFileExpressionInterface $increment, |
| 14 | public BlockStatement $codeList, |
| 15 | ) { |
| 16 | } |
| 17 | |
| 18 | public function toTypescript(): string |
| 19 | { |
| 20 | return $this->render(true); |
| 21 | } |
| 22 | public function toJavascript(): string |
| 23 | { |
| 24 | return $this->render(false); |
| 25 | } |
| 26 | public function needsDefinitions(): JavascriptIdentifierList |
| 27 | { |
| 28 | $expressions = array_values(array_filter([ |
| 29 | $this->initialization, |
| 30 | $this->condition, |
| 31 | $this->increment, |
| 32 | ])); |
| 33 | $definitions = ControlFlowUtils::mergeDefinitions(...$expressions); |
| 34 | foreach ($this->codeList->needsDefinitions() as $definition) { |
| 35 | $definitions = $definitions->append($definition); |
| 36 | } |
| 37 | return $definitions; |
| 38 | } |
| 39 | |
| 40 | private function render(bool $typescript): string |
| 41 | { |
| 42 | $parts = []; |
| 43 | foreach ([$this->initialization, $this->condition, $this->increment] as $expression) { |
| 44 | $parts[] = $expression ? ($typescript ? $expression->toTypescript() : $expression->toJavascript()) : ''; |
| 45 | } |
| 46 | $parts[0] = rtrim($parts[0], ';'); |
| 47 | $body = ControlFlowUtils::renderBlock($this->codeList->codeList, $typescript); |
| 48 | return 'for (' . implode('; ', $parts) . ') {' . ($body === '' ? '' : PHP_EOL . $body) . PHP_EOL . '}'; |
| 49 | } |
| 50 | |
| 51 | public function providesDefinitions(bool $applyBlockScope): JavascriptIdentifierList |
| 52 | { |
| 53 | return $this->codeList->providesDefinitions(true); |
| 54 | } |
| 55 | } |