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