Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
6 / 12
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ForOfStatement
50.00% covered (danger)
50.00%
6 / 12
50.00% covered (danger)
50.00%
3 / 6
22.50
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toTypescript
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toJavascript
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsDefinitions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 render
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 providesDefinitions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\TypescriptCodeBuilder\Dto\ControlFlow;
3
4use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList;
5use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface;
6use Apie\TypescriptCodeBuilder\Utils\ControlFlowUtils;
7
8class ForOfStatement implements TypescriptFileExpressionInterface
9{
10    public function __construct(
11        public TypescriptFileExpressionInterface $variable,
12        public TypescriptFileExpressionInterface $iterable,
13        public BlockStatement $codeList,
14    ) {
15    }
16
17    public function toTypescript(): string
18    {
19        return $this->render(true);
20    }
21    public function toJavascript(): string
22    {
23        return $this->render(false);
24    }
25    public function needsDefinitions(): JavascriptIdentifierList
26    {
27        $definitions = ControlFlowUtils::mergeDefinitions($this->variable, $this->iterable);
28        foreach ($this->codeList->needsDefinitions() as $definition) {
29            $definitions = $definitions->append($definition);
30        }
31        return $definitions;
32    }
33
34    protected function render(bool $typescript): string
35    {
36        $variable = $typescript ? $this->variable->toTypescript() : $this->variable->toJavascript();
37        $iterable = $typescript ? $this->iterable->toTypescript() : $this->iterable->toJavascript();
38        $body = ControlFlowUtils::renderBlock($this->codeList->codeList, $typescript);
39        return 'for (' . rtrim($variable, ';') . ' of ' . $iterable . ') {' . ($body === '' ? '' : PHP_EOL . $body) . PHP_EOL . '}';
40    }
41
42    public function providesDefinitions(bool $applyBlockScope): JavascriptIdentifierList
43    {
44        return $this->codeList->providesDefinitions(true);
45    }
46}