Passed
Push — master ( 71234a...e5baf6 )
by Jhao
01:45
created

Compiler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 77
ccs 39
cts 39
cp 1
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
B compileStatement() 0 18 7
A compileBatch() 0 4 2
A compile() 0 19 4
A compileLabel() 0 7 1
A compileStatements() 0 4 2
A create() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of PhpAidc LabelPrinter package.
5
 *
6
 * © Appwilio (https://appwilio.com)
7
 * © JhaoDa (https://github.com/jhaoda)
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace PhpAidc\LabelPrinter;
16
17
use PhpAidc\LabelPrinter\Label\Batch;
18
use PhpAidc\LabelPrinter\Contract\Job;
19
use PhpAidc\LabelPrinter\Contract\Label;
20
use PhpAidc\LabelPrinter\Contract\Command;
21
use PhpAidc\LabelPrinter\Contract\Language;
22
use PhpAidc\LabelPrinter\Label\BooleanCondition;
23
use PhpAidc\LabelPrinter\Label\LanguageCondition;
24
25
final class Compiler
26
{
27
    /** @var Language */
28
    private $language;
29
30 7
    public static function create(Language $language): self
31
    {
32 7
        return new self($language);
33
    }
34
35 7
    public function __construct(Language $language)
36
    {
37 7
        $this->language = $language;
38 7
    }
39
40 6
    public function compile(Job $job): string
41
    {
42 6
        $instructions = [];
43
44 6
        if ($job instanceof Label) {
45 5
            $instructions[] = $this->compileLabel($job);
46
        }
47
48 6
        if ($job instanceof Batch) {
49 1
            $instructions[] = $this->compileBatch($job);
50
        }
51
52
        $payload = \array_reduce($instructions, static function ($carry, $item) {
53 6
            return $item instanceof \Generator
54 6
                ? \array_merge($carry, \iterator_to_array($item, false))
55 6
                : \array_merge($carry, $item);
56 6
        }, []);
57
58 6
        return \implode($payload);
59
    }
60
61 1
    private function compileBatch(Batch $batch): \Generator
62
    {
63 1
        foreach ($batch as $label) {
64 1
            yield from $this->compileLabel($label);
65
        }
66 1
    }
67
68 6
    private function compileLabel(Label $label): \Generator
69
    {
70 6
        yield from $this->language->compileDeclaration($label);
71
72 6
        yield from $this->compileStatements($label);
73
74 6
        yield from $this->language->compilePrint($label->getCopies());
75 6
    }
76
77 6
    private function compileStatements(Label $label): \Generator
78
    {
79 6
        foreach ($label as $statement) {
80 6
            yield from $this->compileStatement($label, $statement);
81
        }
82 6
    }
83
84 6
    private function compileStatement(Label $label, $statement): \Generator
85
    {
86 6
        if ($statement instanceof LanguageCondition && $statement->isMatch(\get_class($this->language))) {
87 1
            yield from $this->compileStatements($statement->apply((clone $label)->erase()));
0 ignored issues
show
Bug introduced by
The method erase() does not exist on PhpAidc\LabelPrinter\Contract\Label. Since it exists in all sub-types, consider adding an abstract or default implementation to PhpAidc\LabelPrinter\Contract\Label. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            yield from $this->compileStatements($statement->apply((clone $label)->/** @scrutinizer ignore-call */ erase()));
Loading history...
88
89 1
            return;
90
        }
91
92 6
        if ($statement instanceof BooleanCondition && $statement->isTruthy()) {
93 1
            yield from $this->compileStatements($statement->apply((clone $label)->erase()));
94
95 1
            return;
96
        }
97
98 6
        if ($statement instanceof Command && $this->language->isSupport($statement)) {
99 5
            yield from $this->language->compileCommand($statement);
100
101 5
            return;
102
        }
103 2
    }
104
}
105