LeetspeakDecoder::applyCurrent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 4
b 0
f 1
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 10
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
10
final class LeetspeakDecoder extends Coder
11
{
12
    use Chaining;
13
14
    /**
15
     * @var array<CharTree> Memoization of formatted character trees.
16
     */
17
    private static $memoization = [];
18
19 6
    public function __construct()
20
    {
21 6
        parent::__construct(new LeetspeakMap());
22 6
    }
23
24
    /**
25
     * @param CharTree $charTree Character tree to format.
26
     * @return CharTree Leetspeak decoded variant of the character tree.
27
     */
28 6
    protected function applyCurrent(CharTree $charTree): CharTree
29
    {
30
        // When PHP 7.1 is no longer supported, change to using spl_object_id.
31 6
        $hash = spl_object_hash($charTree);
32
33 6
        if (!isset(self::$memoization[$hash])) {
34 6
            self::$memoization[$hash] = $this->format($charTree);
35
        }
36
37 6
        return self::$memoization[$hash];
38
    }
39
}
40