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