PasswordMinimumNumberValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
eloc 7
dl 0
loc 24
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 9 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Porthou\Password\Validators;
5
6
use Porthou\Password\PasswordException;
7
use Porthou\Password\Validator;
8
9
class PasswordMinimumNumberValidator implements Validator
10
{
11
    /** @var int $minimum */
12
    private $minimum;
13
14
    /**
15
     * PasswordMinimumNumberValidator constructor.
16
     * @param int $minimum The minimum number of numbers that should be in the password
17
     */
18
    public function __construct(int $minimum = 1)
19
    {
20
        $this->minimum = $minimum;
21
    }
22
23
    /** {@inheritdoc} */
24
    public function validate(string $password): bool
25
    {
26
        $numbers = preg_match_all('/\d/', $password);
27
28
        if ($numbers >= $this->minimum) {
29
            return true;
30
        }
31
32
        throw new PasswordException('Password must contain at least ' . $this->minimum . ' numbers.');
33
    }
34
}
35