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

LowerCaseConverter::applyCurrent()   A

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 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
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\LowerCaseMap;
9
use Stadly\PasswordPolice\Formatter;
10
11
final class LowerCaseConverter implements Formatter
12
{
13
    use Chaining;
14
15
    /**
16
     * @var Coder Lower case coder.
17
     */
18
    private $lowerCaseCoder;
19
20
    /**
21
     * @var CharTree[] Memoization of formatted character trees.
22
     */
23
    private static $memoization = [];
24
25 7
    public function __construct()
26
    {
27 7
        $this->lowerCaseCoder = new Coder(new LowerCaseMap());
28 7
    }
29
30
    /**
31
     * @param CharTree $charTree Character tree to format.
32
     * @return CharTree Lower case converted variant of the character tree.
33
     */
34 7
    protected function applyCurrent(CharTree $charTree): CharTree
35
    {
36
        // When PHP 7.1 is no longer supported, change to using spl_object_id.
37 7
        $hash = spl_object_hash($charTree);
38
39 7
        if (!isset(self::$memoization[$hash])) {
40 6
            self::$memoization[$hash] = $this->lowerCaseCoder->apply($charTree);
41
        }
42
43 7
        return self::$memoization[$hash];
44
    }
45
}
46