| Conditions | 12 |
| Paths | 152 |
| Total Lines | 54 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 53 | public static function randomPassword($length = 16, int $flags = null) |
||
| 54 | { |
||
| 55 | if ($flags === null) { |
||
| 56 | $flags = self::FLAG_PASSWORD_SPECIAL | self::FLAG_PASSWORD_NUMBER | self::FLAG_PASSWORD_STRENGTH; |
||
| 57 | } |
||
| 58 | |||
| 59 | $useSpecial = ($flags & self::FLAG_PASSWORD_SPECIAL) > 0; |
||
| 60 | $useNumbers = ($flags & self::FLAG_PASSWORD_NUMBER) > 0; |
||
| 61 | |||
| 62 | $alphabet = self::CHARS . strtoupper(self::CHARS); |
||
| 63 | |||
| 64 | if ($useSpecial) { |
||
| 65 | $alphabet .= self::CHARS_SPECIAL; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($useNumbers) { |
||
| 69 | $alphabet .= self::CHARS_NUMBER; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | $passGen = function () use ($alphabet, $length) { |
||
| 76 | $pass = []; |
||
| 77 | $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache |
||
| 78 | |||
| 79 | for ($i = 0; $i < $length; $i++) { |
||
| 80 | $n = mt_rand(0, $alphaLength); |
||
| 81 | $pass[] = $alphabet[$n]; |
||
| 82 | } |
||
| 83 | |||
| 84 | return $pass; |
||
| 85 | }; |
||
| 86 | |||
| 87 | if ($flags & self::FLAG_PASSWORD_STRENGTH) { |
||
| 88 | do { |
||
| 89 | $pass = $passGen(); |
||
| 90 | $strength = self::checkStrength($pass); |
||
| 91 | |||
| 92 | $res = $strength['lower'] > 0 && $strength['upper'] > 0; |
||
| 93 | |||
| 94 | if ($useSpecial === true) { |
||
| 95 | $res = $res && $strength['special'] > 0; |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($useNumbers === true) { |
||
| 99 | $res = $res && $strength['number'] > 0; |
||
| 100 | } |
||
| 101 | } while ($res === false); |
||
| 102 | |||
| 103 | return implode('', $pass); |
||
| 104 | } |
||
| 105 | |||
| 106 | return implode($passGen()); |
||
| 107 | } |
||
| 141 | } |