Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IIFE
85.19% covered (warning)
85.19%
23 / 27
60.00% covered (warning)
60.00%
3 / 5
16.83
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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 toJavascript
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 providesDefinitions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsDefinitions
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
6.73
1<?php
2namespace Apie\TypescriptCodeBuilder\Dto;
3
4use Apie\TypescriptCodeBuilder\Lists\CodeList;
5use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList;
6use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface;
7
8/**
9 * Represents a IIFE: an inmediately invoke function expression.
10 */
11class IIFE implements TypescriptFileExpressionInterface
12{
13    public function __construct(
14        public CodeList $codeList
15    ) {
16    }
17    public function toTypescript(): string
18    {
19        $list = [];
20        foreach ($this->codeList as $code) {
21            $codeRow = $code->toTypescript();
22            if ($codeRow) {
23                $list[] = $codeRow;
24            }
25        }
26        $firstPrefix = empty($list) ? '' : '    ';
27
28        return '(function(){' . PHP_EOL . $firstPrefix . implode(PHP_EOL . '    ', $list) . PHP_EOL . '}());';
29    }
30    public function toJavascript(): string
31    {
32        $list = [];
33        foreach ($this->codeList as $code) {
34            $codeRow = $code->toJavascript();
35            if ($codeRow) {
36                $list[] = $codeRow;
37            }
38        }
39
40        $firstPrefix = empty($list) ? '' : '    ';
41
42        return '(function(){' . PHP_EOL . $firstPrefix . implode(PHP_EOL . '    ', $list) . PHP_EOL . '}());';
43    }
44    public function providesDefinitions(): JavascriptIdentifierList
45    {
46        return new JavascriptIdentifierList();
47    }
48    public function needsDefinitions(): JavascriptIdentifierList
49    {
50        $provides = [];
51        foreach ($this->codeList as $code) {
52            foreach ($code->providesDefinitions() as $definition) {
53                $provides[$definition->toNative()] = true;
54            }
55        }
56        $list = [];
57        foreach ($this->codeList as $code) {
58            foreach ($code->needsDefinitions() as $definition) {
59                $key = $definition->toNative();
60                if (empty($provides[$key])) {
61                    $list[$key] = $definition;
62                }
63            }
64        }
65        return new JavascriptIdentifierList($list);
66    }
67}