LeetspeakMap   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 69
ccs 14
cts 14
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A code() 0 9 1
A getLengths() 0 3 1
A __construct() 0 10 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\CodeMap;
6
7
use Stadly\PasswordPolice\CodeMap;
8
9
final class LeetspeakMap implements CodeMap
10
{
11
    private const ENCODE_MAP = [
12
        'A' => ['4', '@', '∂'],
13
        'B' => ['8', 'ß'],
14
        'C' => ['(', '¢', '<', '[', '©'],
15
        'D' => ['∂'],
16
        'E' => ['3', '€', 'є'],
17
        'F' => ['ƒ'],
18
        'G' => ['6', '9'],
19
        'H' => ['#'],
20
        'I' => ['1', '!', '|', ':'],
21
        'J' => ['¿'],
22
        'K' => ['X'],
23
        'L' => ['1', '£', 'ℓ'],
24
        'O' => ['0', '°'],
25
        'R' => ['2', '®', 'Я'],
26
        'S' => ['5', '$', '§'],
27
        'T' => ['7', '†'],
28
        'U' => ['µ'],
29
        'W' => ['vv'],
30
        'X' => ['×'],
31
        'Y' => ['φ', '¥'],
32
        'Z' => ['2', '≥'],
33
    ];
34
35
    /**
36
     * @var array<int, array<string|int, array<string>>> Leetspeak code map.
37
     */
38
    private $codeMap = [];
39
40
    /**
41
     * @param bool $encode Whether the map should encode or decode Leetspeak.
42
     */
43 18
    public function __construct(bool $encode = false)
44
    {
45 18
        if ($encode) {
46 9
            foreach (self::ENCODE_MAP as $char => $codedChars) {
47 9
                $this->codeMap[mb_strlen($char)][mb_strtoupper($char)] = $codedChars;
48
            }
49
        } else {
50 9
            foreach (self::ENCODE_MAP as $char => $codedChars) {
51 9
                foreach ($codedChars as $codedChar) {
52 9
                    $this->codeMap[mb_strlen($codedChar)][mb_strtoupper($codedChar)][] = $char;
53
                }
54
            }
55
        }
56 18
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 2
    public function getLengths(): array
62
    {
63 2
        return array_keys($this->codeMap);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 16
    public function code(string $string): array
70
    {
71
        // Add coded characters.
72 16
        $codeMap = $this->codeMap[mb_strlen($string)][mb_strtoupper($string)] ?? [];
73
74
        // Add uncoded character.
75 16
        $codeMap[] = $string;
76
77 16
        return $codeMap;
78
    }
79
}
80