Conditions | 14 |
Paths | 513 |
Total Lines | 55 |
Code Lines | 24 |
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 |
||
73 | public function generate( |
||
74 | bool $includeUppercase = true, |
||
75 | bool $includeLowercase = true, |
||
76 | bool $includeNumbers = true, |
||
77 | bool $includeSpecial = true |
||
78 | ): string { |
||
79 | if (!$includeUppercase && !$includeLowercase && !$includeNumbers && !$includeSpecial) { |
||
80 | throw new \InvalidArgumentException('At least one character type must be selected'); |
||
81 | } |
||
82 | |||
83 | $chars = []; |
||
84 | |||
85 | if ($includeUppercase) { |
||
86 | $chars = array_merge($chars, range('A', 'Z')); |
||
87 | } |
||
88 | |||
89 | if ($includeLowercase) { |
||
90 | $chars = array_merge($chars, range('a', 'z')); |
||
91 | } |
||
92 | |||
93 | if ($includeNumbers) { |
||
94 | $chars = array_merge($chars, array_map('strval', range(0, 9))); |
||
95 | } |
||
96 | |||
97 | if ($includeSpecial) { |
||
98 | $chars = array_merge($chars, str_split('!@#$%^&*()_+-=[]{}|;:,.<>?')); |
||
|
|||
99 | } |
||
100 | |||
101 | $length = random_int($this->minLength, $this->maxLength); |
||
102 | $password = ''; |
||
103 | |||
104 | // Ensure at least one character from each selected type |
||
105 | if ($includeUppercase) { |
||
106 | $password .= $this->getRandomCharacter(range('A', 'Z')); |
||
107 | } |
||
108 | |||
109 | if ($includeLowercase) { |
||
110 | $password .= $this->getRandomCharacter(range('a', 'z')); |
||
111 | } |
||
112 | |||
113 | if ($includeNumbers) { |
||
114 | $password .= $this->getRandomCharacter(array_map('strval', range(0, 9))); |
||
115 | } |
||
116 | |||
117 | if ($includeSpecial) { |
||
118 | $password .= $this->getRandomCharacter(str_split('!@#$%^&*()_+-=[]{}|;:,.<>?')); |
||
119 | } |
||
120 | |||
121 | // Fill the rest of the password with random characters |
||
122 | while (strlen($password) < $length) { |
||
123 | $password .= $this->getRandomCharacter($chars); |
||
124 | } |
||
125 | |||
126 | // Shuffle the password to ensure random distribution |
||
127 | return str_shuffle($password); |
||
128 | } |
||
141 |