StrengthChecker::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PasswordHelper;
6
7
class StrengthChecker
8
{
9
    /**
10
     * @var int Password strength score
11
     */
12
    protected $strength;
13
14
    /**
15
     * @var string[] Descriptions of levels of password strength
16
     */
17
    protected $levels = [
18
        'Very Weak', 'Weak', 'Good', 'Very Good', 'Strong', 'Very Strong'
19
    ];
20
21
    public function __construct()
22
    {
23
        $this->strength = 0;
24
    }
25
26
    /**
27
     * Determines the strength of a given password
28
     *
29
     * @param string $password
30
     *
31
     * @return string
32
     */
33
    public function checkStrength(string $password): string
34
    {
35
        $score = 0;
36
        $score += min([strlen($password), Policy::MINIMUM_LENGTH]);
37
        $score += (int) (preg_match_all('/[a-z]/i', $password, $matches) >= 2);
38
        $score += (int) (bool) preg_match_all('/\d/', $password, $matches);
39
        $score += (int) (bool) preg_match_all('/[A-Z]/', $password, $matches);
40
        $score += (int) (bool) preg_match_all('/[a-z]/', $password, $matches);
41
        $score += (int) (bool) preg_match_all('/[^a-z\d ]/i', $password, $matches);
42
        $score = min([(int) ($score / 3), 6]);
43
44
        return $this->levels[$score];
45
    }
46
}
47