Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.85% covered (warning)
51.85%
14 / 27
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
IfStatement
51.85% covered (warning)
51.85%
14 / 27
50.00% covered (danger)
50.00%
3 / 6
40.11
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 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 render
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 providesDefinitions
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace Apie\TypescriptCodeBuilder\Dto\ControlFlow;
3
4use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList;
5use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface;
6use Apie\TypescriptCodeBuilder\Utils\ControlFlowUtils;
7
8class IfStatement implements TypescriptFileExpressionInterface
9{
10    public function __construct(
11        public TypescriptFileExpressionInterface $condition,
12        public BlockStatement $then,
13        public ?BlockStatement $else = null,
14    ) {
15    }
16
17    public function toTypescript(): string
18    {
19        return $this->render(true);
20    }
21
22    public function toJavascript(): string
23    {
24        return $this->render(false);
25    }
26
27    public function needsDefinitions(): JavascriptIdentifierList
28    {
29        $definitions = ControlFlowUtils::mergeDefinitions($this->condition);
30        foreach ([$this->then, $this->else] as $block) {
31            if (!$block) {
32                continue;
33            }
34            foreach ($block->needsDefinitions() as $definition) {
35                $definitions = $definitions->append($definition);
36            }
37        }
38        return $definitions;
39    }
40
41    private function render(bool $typescript): string
42    {
43        $result = 'if (' . ($typescript ? $this->condition->toTypescript() : $this->condition->toJavascript()) . ') {';
44        $body = ControlFlowUtils::renderBlock($this->then->codeList, $typescript);
45        if ($body !== '') {
46            $result .= PHP_EOL . $body;
47        }
48        $result .= PHP_EOL . '}';
49        if ($this->else) {
50            $result .= ' else {';
51            $body = ControlFlowUtils::renderBlock($this->else->codeList, $typescript);
52            if ($body !== '') {
53                $result .= PHP_EOL . $body;
54            }
55            $result .= PHP_EOL . '}';
56        }
57        return $result;
58    }
59
60    public function providesDefinitions(bool $applyBlockScope): JavascriptIdentifierList
61    {
62        $definitions = $this->then->providesDefinitions(true);
63        if ($this->else) {
64            foreach ($this->else->providesDefinitions(true) as $definition) {
65                $definitions = $definitions->append($definition);
66            }
67        }
68        return $definitions;
69    }
70}