Blacklist::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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\InBlacklist;
27
use CustomerGauge\Password\Rule;
28
29
use function mb_strpos;
30
use function mb_strtolower;
31
32
final class Blacklist implements Rule
33
{
34
    /** @var string[] */
35
    private array $words;
36
37
    private string $encoding;
38
39
    /**
40
     * @param string[] $words
41
     */
42 2
    public function __construct(array $words, string $encoding = 'utf8')
43
    {
44 2
        $this->words    = $words;
45 2
        $this->encoding = $encoding;
46 2
    }
47
48 2
    public function __invoke(string $password): void
49
    {
50 2
        $password = mb_strtolower($password, $this->encoding);
51 2
        foreach ($this->words as $word) {
52 2
            $word = mb_strtolower($word, $this->encoding);
53 2
            if (mb_strpos($password, $word, 0, $this->encoding) !== false) {
54 2
                throw InBlacklist::contains($word);
55
            }
56
        }
57
    }
58
}
59