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