Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
25 / 30
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
NamedFunction
83.33% covered (warning)
83.33%
25 / 30
80.00% covered (warning)
80.00%
4 / 5
19.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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 toJavascript
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 providesDefinitions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsDefinitions
64.29% covered (warning)
64.29%
9 / 14
0.00% covered (danger)
0.00%
0 / 1
10.92
1<?php
2namespace Apie\TypescriptCodeBuilder\Dto;
3
4use Apie\TypescriptCodeBuilder\Lists\ArgumentList;
5use Apie\TypescriptCodeBuilder\Lists\CodeList;
6use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList;
7use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface;
8use Apie\TypescriptCodeBuilder\ValueObjects\JavascriptIdentifier;
9
10class NamedFunction implements TypescriptFileExpressionInterface
11{
12    public function __construct(
13        public JavascriptIdentifier $name,
14        public ArgumentList $arguments,
15        public CodeList $codeList
16    ) {
17    }
18
19    public function toTypescript(): string
20    {
21        $list = [];
22        foreach ($this->codeList as $code) {
23            $codeRow = $code->toTypescript();
24            if ($codeRow) {
25                $list[] = $codeRow;
26            }
27        }
28        $firstPrefix = empty($list) ? '' : '    ';
29        return 'function ' . $this->name . '(' . $this->arguments->toTypescript() . ') {' . PHP_EOL . $firstPrefix . implode(PHP_EOL . '    ', $list) . PHP_EOL . '}';
30    }
31    public function toJavascript(): string
32    {
33        $list = [];
34        foreach ($this->codeList as $code) {
35            $codeRow = $code->toJavascript();
36            if ($codeRow) {
37                $list[] = $codeRow;
38            }
39        }
40        $firstPrefix = empty($list) ? '' : '    ';
41        return 'function ' . $this->name . '(' . $this->arguments->toJavascript() . ') {' . PHP_EOL . $firstPrefix . implode(PHP_EOL . '    ', $list) . PHP_EOL . '}';
42    }
43    
44    public function providesDefinitions(): JavascriptIdentifierList
45    {
46        return new JavascriptIdentifierList([$this->name]);
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        foreach ($this->arguments as $argument) {
66            foreach ($argument->needsDefinitions() as $definition) {
67                $list[$definition->toNative()] = $definition;
68            }
69        }
70        return new JavascriptIdentifierList($list);
71    }
72}