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

Coder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A format() 0 17 4
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