Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
22.22% covered (danger)
22.22%
4 / 18
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockStatement
22.22% covered (danger)
22.22%
4 / 18
50.00% covered (danger)
50.00%
3 / 6
92.52
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
 providesDefinitions
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\TypescriptCodeBuilder\Dto\ControlFlow;
3
4use Apie\TypescriptCodeBuilder\Lists\CodeList;
5use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList;
6use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface;
7use Apie\TypescriptCodeBuilder\Utils\ControlFlowUtils;
8
9class BlockStatement implements TypescriptFileExpressionInterface
10{
11    public function __construct(public CodeList $codeList)
12    {
13    }
14
15    public function toTypescript(): string
16    {
17        return $this->render(true);
18    }
19
20    public function toJavascript(): string
21    {
22        return $this->render(false);
23    }
24
25    public function providesDefinitions(bool $applyBlockScope): JavascriptIdentifierList
26    {
27        $definitions = new JavascriptIdentifierList();
28        foreach ($this->codeList as $code) {
29            $childApplyBlockScope = $applyBlockScope || $code instanceof self;
30            foreach ($code->providesDefinitions($childApplyBlockScope) as $definition) {
31                $definitions = $definitions->append($definition);
32            }
33        }
34        return $definitions;
35    }
36
37    public function needsDefinitions(): JavascriptIdentifierList
38    {
39        $provides = ControlFlowUtils::blockDefinitions($this)->toStringArray();
40        $needs = new JavascriptIdentifierList();
41        foreach ($this->codeList as $code) {
42            foreach ($code->needsDefinitions() as $definition) {
43                if (!in_array($definition->toNative(), $provides, true)) {
44                    $needs = $needs->append($definition);
45                }
46            }
47        }
48        return $needs;
49    }
50
51    private function render(bool $typescript): string
52    {
53        $body = ControlFlowUtils::renderBlock($this->codeList, $typescript);
54        return '{' . ($body === '' ? '' : PHP_EOL . $body) . PHP_EOL . '}';
55    }
56}