Conditions | 7 |
Paths | 33 |
Total Lines | 107 |
Code Lines | 44 |
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 |
||
113 | public function getPasswordCheckerJs(string $passwordInputId): ?string |
||
114 | { |
||
115 | $checkPass = api_get_setting('allow_strength_pass_checker'); |
||
116 | $useStrengthPassChecker = 'true' === $checkPass; |
||
117 | |||
118 | if (false === $useStrengthPassChecker) { |
||
119 | return null; |
||
120 | } |
||
121 | |||
122 | $minRequirements = Security::getPasswordRequirements()['min']; |
||
123 | |||
124 | $options = [ |
||
125 | 'rules' => [], |
||
126 | ]; |
||
127 | |||
128 | if ($minRequirements['length'] > 0) { |
||
129 | $options['rules'][] = [ |
||
130 | 'minChar' => $minRequirements['length'], |
||
131 | 'pattern' => '.', |
||
132 | 'helpText' => sprintf( |
||
133 | get_lang('Minimum %s characters in total'), |
||
134 | $minRequirements['length'] |
||
135 | ), |
||
136 | ]; |
||
137 | } |
||
138 | |||
139 | if ($minRequirements['lowercase'] > 0) { |
||
140 | $options['rules'][] = [ |
||
141 | 'minChar' => $minRequirements['lowercase'], |
||
142 | 'pattern' => '[a-z]', |
||
143 | 'helpText' => sprintf( |
||
144 | get_lang('Minimum %s lowercase characters'), |
||
145 | $minRequirements['lowercase'] |
||
146 | ), |
||
147 | ]; |
||
148 | } |
||
149 | |||
150 | if ($minRequirements['uppercase'] > 0) { |
||
151 | $options['rules'][] = [ |
||
152 | 'minChar' => $minRequirements['uppercase'], |
||
153 | 'pattern' => '[A-Z]', |
||
154 | 'helpText' => sprintf( |
||
155 | get_lang('Minimum %s uppercase characters'), |
||
156 | $minRequirements['uppercase'] |
||
157 | ), |
||
158 | ]; |
||
159 | } |
||
160 | |||
161 | if ($minRequirements['numeric'] > 0) { |
||
162 | $options['rules'][] = [ |
||
163 | 'minChar' => $minRequirements['numeric'], |
||
164 | 'pattern' => '[0-9]', |
||
165 | 'helpText' => sprintf( |
||
166 | get_lang('Minimum %s numerical (0-9) characters'), |
||
167 | $minRequirements['numeric'] |
||
168 | ), |
||
169 | ]; |
||
170 | } |
||
171 | |||
172 | if ($minRequirements['specials'] > 0) { |
||
173 | $options['rules'][] = [ |
||
174 | 'minChar' => $minRequirements['specials'], |
||
175 | 'pattern' => '[!"#$%&\'()*+,\-./\\\:;<=>?@[\\]^_`{|}~]', |
||
176 | 'helpText' => sprintf( |
||
177 | get_lang('Minimum %s special characters'), |
||
178 | $minRequirements['specials'] |
||
179 | ), |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | return "<script> |
||
184 | (function($) { |
||
185 | $.fn.passwordCheckerChange = function(options) { |
||
186 | var settings = $.extend({ |
||
187 | rules: [] |
||
188 | }, options ); |
||
189 | |||
190 | return this.each(function() { |
||
191 | var \$passwordInput = $(this); |
||
192 | var \$requirements = $('#password-requirements'); |
||
193 | |||
194 | function validatePassword(password) { |
||
195 | var html = ''; |
||
196 | |||
197 | settings.rules.forEach(function(rule) { |
||
198 | var isValid = new RegExp(rule.pattern).test(password) && password.length >= rule.minChar; |
||
199 | var color = isValid ? 'green' : 'red'; |
||
200 | html += '<li style=\"color:' + color + '\">' + rule.helpText + '</li>'; |
||
201 | }); |
||
202 | |||
203 | \$requirements.html(html); |
||
204 | } |
||
205 | |||
206 | \$passwordInput.on('input', function() { |
||
207 | validatePassword(\$passwordInput.val()); |
||
208 | \$requirements.show(); |
||
209 | }); |
||
210 | |||
211 | \$passwordInput.on('blur', function() { |
||
212 | \$requirements.hide(); |
||
213 | }); |
||
214 | }); |
||
215 | }; |
||
216 | }(jQuery)); |
||
217 | |||
218 | $(function() { |
||
219 | $('".$passwordInputId."').passwordCheckerChange(".json_encode($options).'); |
||
220 | }); |
||
247 |