Completed
Push — master ( 738971...bd8e7f )
by Magnar Ovedal
07:25 queued 04:40
created

Coder::applyCurrent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\Formatter;
6
7
use Stadly\PasswordPolice\CharTree;
8
use Stadly\PasswordPolice\CodeMap;
9
use Stadly\PasswordPolice\Formatter;
10
11
abstract class Coder implements Formatter
12
{
13
    /**
14
     * @var CodeMap Code map for coding character trees.
15
     */
16
    private $codeMap;
17
18
    /**
19
     * @param CodeMap $codeMap Code map for coding character trees.
20
     */
21 5
    public function __construct(CodeMap $codeMap)
22
    {
23 5
        $this->codeMap = $codeMap;
24 5
    }
25
26
    /**
27
     * @param CharTree $charTree Character tree to format.
28
     * @return CharTree Coded variant of the character tree.
29
     */
30
    abstract protected function applyCurrent(CharTree $charTree): CharTree;
31
32
    /**
33
     * @param CharTree $charTree Character tree to format.
34
     * @return CharTree Coded variant of the character tree. Memoization is not used.
35
     */
36 31
    protected function format(CharTree $charTree): CharTree
37
    {
38 31
        if ($charTree->getRoot() === null) {
39 5
            return $charTree;
40
        }
41
42 26
        $branches = [];
43
44 26
        foreach ($this->codeMap->getMap($charTree) as $char => $codedChars) {
45 22
            $branch = $this->applyCurrent($charTree->getBranchesAfterRoot((string)$char));
46
47 22
            foreach ($codedChars as $codedChar) {
48 22
                $branches[] = CharTree::fromString($codedChar, [$branch]);
49
            }
50
        }
51
52 26
        return CharTree::fromString('', $branches);
53
    }
54
}
55