Passed
Push — master ( 19a784...76ed2d )
by Magnar Ovedal
02:38
created

Coder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 47
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A format() 0 17 4
A applyCurrent() 0 3 1
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
final class Coder implements Formatter
12
{
13
    use Chaining;
14
15
    /**
16
     * @var CodeMap Code map for coding character trees.
17
     */
18
    private $codeMap;
19
20
    /**
21
     * @param CodeMap $codeMap Code map for coding character trees.
22
     */
23 5
    public function __construct(CodeMap $codeMap)
24
    {
25 5
        $this->codeMap = $codeMap;
26 5
    }
27
28
    /**
29
     * @param CharTree $charTree Character tree to format.
30
     * @return CharTree Coded variant of the character tree.
31
     */
32 5
    protected function applyCurrent(CharTree $charTree): CharTree
33
    {
34 5
        return $this->format($charTree);
35
    }
36
37
    /**
38
     * @param CharTree $charTree Character tree to format.
39
     * @return CharTree Coded variant of the character tree. Memoization is not used.
40
     */
41 5
    private function format(CharTree $charTree): CharTree
42
    {
43 5
        if ($charTree->getRoot() === null) {
44 1
            return $charTree;
45
        }
46
47 4
        $branches = [];
48
49 4
        foreach ($this->codeMap->getMap($charTree) as $char => $codedChars) {
50 3
            $branch = $this->applyCurrent($charTree->getBranchesAfterRoot((string)$char));
51
52 3
            foreach ($codedChars as $codedChar) {
53 3
                $branches[] = CharTree::fromString($codedChar, [$branch]);
54
            }
55
        }
56
57 4
        return CharTree::fromString('', $branches);
58
    }
59
}
60