| Conditions | 12 |
| Paths | 1152 |
| Total Lines | 77 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 7 | static public function evaluateStrength(string $password): int |
||
| 8 | { |
||
| 9 | $score = 0; |
||
| 10 | $maxScore = 100; |
||
| 11 | |||
| 12 | // Gewichtungen (anpassbar) |
||
| 13 | $lengthWeight = 30; |
||
| 14 | $lowercaseWeight = 10; |
||
| 15 | $uppercaseWeight = 10; |
||
| 16 | $numberWeight = 15; |
||
| 17 | $symbolWeight = 15; |
||
| 18 | $variationWeight = 10; |
||
| 19 | $commonPenalty = -30; |
||
| 20 | |||
| 21 | $length = strlen($password); |
||
| 22 | |||
| 23 | // Mindestlänge |
||
| 24 | if ($length >= 12) { |
||
| 25 | $score += $lengthWeight; |
||
| 26 | } elseif ($length >= 8) { |
||
| 27 | $score += $lengthWeight / 2; |
||
| 28 | } |
||
| 29 | |||
| 30 | // Kleinbuchstaben |
||
| 31 | if (preg_match('/[a-z]/', $password)) { |
||
| 32 | $score += $lowercaseWeight; |
||
| 33 | } |
||
| 34 | |||
| 35 | // Großbuchstaben |
||
| 36 | if (preg_match('/[A-Z]/', $password)) { |
||
| 37 | $score += $uppercaseWeight; |
||
| 38 | } |
||
| 39 | |||
| 40 | // Zahlen |
||
| 41 | if (preg_match('/[0-9]/', $password)) { |
||
| 42 | $score += $numberWeight; |
||
| 43 | } |
||
| 44 | |||
| 45 | // Sonderzeichen |
||
| 46 | if (preg_match('/[\W_]/', $password)) { |
||
| 47 | $score += $symbolWeight; |
||
| 48 | } |
||
| 49 | |||
| 50 | // Zeichenvielfalt (mind. 3 Kategorien) |
||
| 51 | $types = 0; |
||
| 52 | $types += preg_match('/[a-z]/', $password); |
||
| 53 | $types += preg_match('/[A-Z]/', $password); |
||
| 54 | $types += preg_match('/[0-9]/', $password); |
||
| 55 | $types += preg_match('/[\W_]/', $password); |
||
| 56 | if ($types >= 3) { |
||
| 57 | $score += $variationWeight; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Wiederholungen |
||
| 61 | if (preg_match('/(.)\1{3,}/', $password)) { |
||
| 62 | $score -= 10; |
||
| 63 | } |
||
| 64 | |||
| 65 | // Häufige oder unsichere Passwörter |
||
| 66 | $common = ['123456', 'password', 'qwerty', 'admin', 'letmein']; |
||
| 67 | if (in_array(strtolower($password), $common)) { |
||
| 68 | $score += $commonPenalty; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Begrenzung des Scores |
||
| 72 | $score = max(0, min($score, $maxScore)); |
||
| 73 | |||
| 74 | // Bewertung |
||
| 75 | if ($score >= 80) { |
||
| 76 | $rating = 'strong'; |
||
|
|
|||
| 77 | } elseif ($score >= 50) { |
||
| 78 | $rating = 'medium'; |
||
| 79 | } else { |
||
| 80 | $rating = 'weak'; |
||
| 81 | } |
||
| 82 | |||
| 83 | return $score; |
||
| 84 | } |
||
| 85 | } |