Topology::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/*
3
    Password Strength Library
4
    Copyright (C) 2019 CustomerGauge
5
    [email protected]
6
7
    This program is free software; you can redistribute it and/or
8
    modify it under the terms of the GNU Lesser General Public
9
    License as published by the Free Software Foundation; either
10
    version 3 of the License, or (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
    Lesser General Public License for more details.
16
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with this program; if not, write to the Free Software Foundation,
19
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
*/
21
22
declare(strict_types=1);
23
24
namespace CustomerGauge\Password\Rule;
25
26
use CustomerGauge\Password\Exception\WeakTopology;
27
use CustomerGauge\Password\Rule;
28
29
use function in_array;
30
use function preg_replace;
31
32
final class Topology implements Rule
33
{
34
    /** @var string[] */
35
    private array $topologies;
36
37 5
    public function __construct(string ...$topologies)
38
    {
39 5
        $this->topologies = $topologies;
40 5
    }
41
42 5
    public function __invoke(string $password): void
43
    {
44 5
        $topology = $this->convertToTopology($password);
45
46 5
        if (in_array($topology, $this->topologies, true)) {
47 5
            throw WeakTopology::matches($topology);
48
        }
49
    }
50
51 5
    private function convertToTopology(string $password): ?string
52
    {
53 5
        $patterns     = ['/[a-z]/', '/[A-Z]/', '/[0-9]/', '/[^lud]/'];
54 5
        $replacements = ['l', 'u', 'd', 's'];
55
56 5
        return preg_replace($patterns, $replacements, $password);
57
    }
58
}
59