Passed
Push — master ( 170b0e...19a784 )
by Magnar Ovedal
02:51
created

LeetspeakDecoder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 33
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A applyCurrent() 0 10 2
A __construct() 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\LeetspeakMap;
9
use Stadly\PasswordPolice\Formatter;
10
11
final class LeetspeakDecoder implements Formatter
12
{
13
    use Chaining;
14
15
    /**
16
     * @var Coder Leetspeak coder.
17
     */
18
    private $leetspeakCoder;
19
20
    /**
21
     * @var CharTree[] Memoization of formatted character trees.
22
     */
23
    private static $memoization = [];
24
25 6
    public function __construct()
26
    {
27 6
        $this->leetspeakCoder = new Coder(new LeetspeakMap());
28 6
    }
29
30
    /**
31
     * @param CharTree $charTree Character tree to format.
32
     * @return CharTree Leetspeak decoded variant of the character tree.
33
     */
34 6
    protected function applyCurrent(CharTree $charTree): CharTree
35
    {
36
        // When PHP 7.1 is no longer supported, change to using spl_object_id.
37 6
        $hash = spl_object_hash($charTree);
38
39 6
        if (!isset(self::$memoization[$hash])) {
40 6
            self::$memoization[$hash] = $this->leetspeakCoder->apply($charTree);
41
        }
42
43 6
        return self::$memoization[$hash];
44
    }
45
}
46