PasswordUtil::evaluateStrength()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 59
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 31
c 3
b 0
f 0
nc 128
nop 1
dl 0
loc 59
rs 7.9928

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Startwind\Inventorio\Util;
4
5
abstract class PasswordUtil
6
{
7
    static public function evaluateStrength(string $password): array
8
    {
9
        $rules = [];
10
11
        if (strlen($password) >= 10) {
12
            $rules['passwordTooShort'] = false;
13
        } else {
14
            $rules['passwordTooShort'] = true;
15
        }
16
17
        if (preg_match('/[a-z]/', $password)) {
18
            $rules['noLowercase'] = false;
19
        } else {
20
            $rules['noLowercase'] = true;
21
        }
22
23
        if (preg_match('/[A-Z]/', $password)) {
24
            $rules['noUppercase'] = false;
25
        } else {
26
            $rules['noUppercase'] = true;
27
        }
28
29
        if (preg_match('/[0-9]/', $password)) {
30
            $rules['noNumber'] = false;
31
        } else {
32
            $rules['noNumber'] = true;
33
        }
34
35
        if (preg_match('/[\W_]/', $password)) {
36
            $rules['noSymbol'] = false;
37
        } else {
38
            $rules['noSymbol'] = true;
39
        }
40
41
        /*
42
        $types = 0;
43
        $types += preg_match('/[a-z]/', $password);
44
        $types += preg_match('/[A-Z]/', $password);
45
        $types += preg_match('/[0-9]/', $password);
46
        $types += preg_match('/[\W_]/', $password);
47
        if ($types >= 3) {
48
            $score += $variationWeight;
49
        }
50
        */
51
52
        if (preg_match('/(.)\1{3,}/', $password)) {
53
            $rules['passwordRepeating'] = true;
54
        } else {
55
            $rules['passwordRepeating'] = false;
56
        }
57
58
        $common = ['123456', 'password', 'qwerty', 'admin', 'letmein'];
59
        if (in_array(strtolower($password), $common)) {
60
            $rules['commonPassword'] = true;
61
        } else {
62
            $rules['commonPassword'] = false;
63
        }
64
65
        return $rules;
66
    }
67
}