| Conditions | 21 |
| Total Lines | 201 |
| Code Lines | 138 |
| 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:
Complex classes like install.js ➔ checkPage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 54 | function checkPage() |
||
| 55 | { |
||
| 56 | step = $("#step").val(); |
||
| 57 | dataToUse = ""; |
||
| 58 | error = ""; |
||
| 59 | index = ""; |
||
| 60 | tasks = []; |
||
| 61 | dbInfo = []; |
||
| 62 | multiple = ""; |
||
| 63 | tsk = ""; |
||
| 64 | $("#step_error").addClass("hidden").html(""); |
||
| 65 | $("#res_"+step).html(""); |
||
| 66 | |||
| 67 | alertify |
||
| 68 | .message('Working on it... <i class="fas fa-cog fa-spin fa-2x"></i>', 0) |
||
| 69 | .dismissOthers(); |
||
| 70 | |||
| 71 | if (step === "2") { |
||
| 72 | // STEP 2 |
||
| 73 | if ($("#url_path").val() === "" || $("#absolute_path").val() === "") { |
||
| 74 | error = "Fields need to be filled in!"; |
||
| 75 | } else { |
||
| 76 | jsonValues = {"absolute_path":$("#absolute_path").val(), "url_path":$("#url_path").val()}; |
||
| 77 | dataToUse = JSON.stringify(jsonValues); |
||
| 78 | tasks = ["folder*install", "folder*includes", "folder*includes/config", "folder*includes/avatars", "folder*includes/libraries/csrfp/libs", "folder*includes/libraries/csrfp/js", "folder*includes/libraries/csrfp/log", "extension*mbstring", "extension*openssl", "extension*bcmath", "extension*iconv", "extension*gd", "extension*xml", "extension*curl", "version*php", "ini*max_execution_time", "extension*gmp", "folder*files", "folder*upload"]; |
||
| 79 | multiple = true; |
||
| 80 | $("#hid_absolute_path").val($("#absolute_path").val()); |
||
| 81 | $("#hid_url_path").val($("#url_path").val()); |
||
| 82 | } |
||
| 83 | } else if (step === "3") { |
||
| 84 | // STEP 3 |
||
| 85 | if ($("#db_host").val() === "" || $("#db_db").val() === "" || $("#db_login").val() === "" || $("#db_port").val() === "") { |
||
| 86 | error = "Fields need to be filled in!"; |
||
| 87 | } else if ($("#db_pw").val().indexOf('"') > -1) { |
||
| 88 | error = "Double quotes in password not allowed!"; |
||
| 89 | } else { |
||
| 90 | jsonValues = {"db_host":$("#db_host").val(), "db_bdd":$("#db_bdd").val(), "db_login":$("#db_login").val(), "db_pw":$("#db_pw").val(), "db_port":$("#db_port").val(), "absolute_path":$("#hid_absolute_path").val(), "url_path":$("#hid_url_path").val()}; |
||
| 91 | dataToUse = JSON.stringify(jsonValues); |
||
| 92 | tasks = ["connection*test"]; |
||
| 93 | multiple = ""; |
||
| 94 | $("#hid_db_host").val($("#db_host").val()); |
||
| 95 | $("#hid_db_bdd").val($("#db_bdd").val()); |
||
| 96 | $("#hid_db_login").val($("#db_login").val()); |
||
| 97 | $("#hid_db_pwd").val($("#db_pw").val()); |
||
| 98 | $("#hid_db_port").val($("#db_port").val()); |
||
| 99 | } |
||
| 100 | } else if (step === "4") { |
||
| 101 | // STEP 4 |
||
| 102 | if ($("#admin_pwd").val() === "") { |
||
| 103 | alertify |
||
| 104 | .error('<i class="fas fa-ban mr-2"></i>You must define a password for Administrator account.', 10) |
||
| 105 | .dismissOthers(); |
||
| 106 | return false; |
||
| 107 | } else if ($("#admin_pwd_confirm").val() === "") { |
||
| 108 | alertify |
||
| 109 | .error('<i class="fas fa-ban mr-2"></i>You must confirm the password for Administrator account.', 10) |
||
| 110 | .dismissOthers(); |
||
| 111 | return false; |
||
| 112 | } else if ($("#admin_pwd_confirm").val() !== $("#admin_pwd").val()) { |
||
| 113 | alertify |
||
| 114 | .error('<i class="fas fa-ban mr-2"></i>Administrator passwords are not similar.', 10) |
||
| 115 | .dismissOthers(); |
||
| 116 | return false; |
||
| 117 | } else{ |
||
| 118 | $("#hid_db_pre").val($("#tbl_prefix").val()); |
||
| 119 | jsonValues = {"tbl_prefix":sanitizeString($("#tbl_prefix").val()), "sk_path":sanitizeString($("#sk_path").val()), "admin_pwd":sanitizeString($("#admin_pwd").val()), "send_stats":""}; |
||
| 120 | dataToUse = JSON.stringify(jsonValues); |
||
| 121 | tasks = ["misc*preparation"]; |
||
| 122 | multiple = ""; |
||
| 123 | } |
||
| 124 | } else if (step === "5") { |
||
| 125 | // STEP 5 |
||
| 126 | dataToUse = ""; |
||
| 127 | tasks = ["table*utf8", "table*api", "table*automatic_del", "table*cache", "table*cache_tree", "table*categories", "table*categories_folders", "table*categories_items", "table*defuse_passwords", "table*emails", "table*export", "table*files", "table*items", "table*items_change", "table*items_edition", "table*items_otp", "table*kb", "table*kb_categories", "table*kb_items", "table*ldap_groups_roles", "table*languages", "table*log_items", "table*log_system", "table*misc", "table*nested_tree", "table*notification", "table*otv", "table*processes", "table*processes_tasks", "table*processes_logs", "table*restriction_to_roles", "table*rights", "table*roles_title", "table*roles_values", "table*sharekeys_fields", "table*sharekeys_files", "table*sharekeys_items", "table*sharekeys_logs", "table*sharekeys_suggestions", "table*suggestion", "table*tags", "table*templates", "table*tokens", "table*users"]; |
||
| 128 | multiple = true; |
||
| 129 | } else if (step === "6") { |
||
| 130 | // STEP 6 |
||
| 131 | jsonValues = {"url_path":sanitizeString($("#hid_url_path").val())}; |
||
| 132 | dataToUse = JSON.stringify(jsonValues); |
||
| 133 | tasks = ["install*init", "file*security", "file*settings.php", "file*csrfp-token", "install*cleanup", "install*cronJob"]; |
||
| 134 | multiple = true; |
||
| 135 | } |
||
| 136 | |||
| 137 | // launch query |
||
| 138 | if (error === "" && multiple === true) { |
||
| 139 | global_error_on_query = false; |
||
| 140 | index = 0; |
||
| 141 | dbInfo = {"db_host" : $("#hid_db_host").val(), "db_bdd" : $("#hid_db_bdd").val(), "db_login" : $("#hid_db_login").val(), "db_pw" : $("#hid_db_pwd").val(), "db_port" : $("#hid_db_port").val(), "db_pre" : $("#hid_db_pre").val()}; |
||
| 142 | |||
| 143 | $("#step_res").val("true"); |
||
| 144 | $("#pop_db").html(""); |
||
| 145 | |||
| 146 | var promise = tasks.slice(1) |
||
| 147 | .reduce( |
||
| 148 | (a,b) => a.then(doGetJson.bind(null, b)), |
||
| 149 | doGetJson( |
||
| 150 | tasks[0] |
||
| 151 | ) |
||
| 152 | ); |
||
| 153 | |||
| 154 | promise.then(function(){ |
||
| 155 | // do something when all requests are ready |
||
| 156 | // all requests are complete |
||
| 157 | if ($("#step_res").val() === "false" || global_error_on_query === true) { |
||
| 158 | alertify |
||
| 159 | .error('<i class="fas fa-ban mr-2"></i>At least one task has failed! Please correct and relaunch.', 0) |
||
| 160 | .dismissOthers(); |
||
| 161 | return false; |
||
| 162 | } else { |
||
| 163 | alertify |
||
| 164 | .success('<i class="fas fa-check text-success mr-2"></i><b>Done</b>.<br>Click next to continue', 1) |
||
| 165 | .dismissOthers(); |
||
| 166 | |||
| 167 | $("#but_launch") |
||
| 168 | .prop("disabled", true) |
||
| 169 | .addClass("hidden"); |
||
| 170 | $("#but_next") |
||
| 171 | .prop("disabled", false) |
||
| 172 | .removeClass("hidden"); |
||
| 173 | // Hide restart button at end of step 6 if successful |
||
| 174 | if (step === "6") { |
||
| 175 | $("#but_restart") |
||
| 176 | .prop("disabled", true) |
||
| 177 | .addClass("hidden"); |
||
| 178 | } |
||
| 179 | |||
| 180 | /* |
||
| 181 | * Removing automatic action |
||
| 182 | // Go to next step |
||
| 183 | if (step <= 6) { |
||
| 184 | setTimeout( |
||
| 185 | function(){ |
||
| 186 | $('#but_next').trigger('click'); |
||
| 187 | }, |
||
| 188 | 1000 |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | */ |
||
| 192 | } |
||
| 193 | }); |
||
| 194 | } else if (error === "" && multiple === "") { |
||
| 195 | tsk = tasks[0].split("*"); |
||
| 196 | |||
| 197 | dbInfo = {"db_host" : $("#hid_db_host").val(), "db_bdd" : $("#hid_db_bdd").val(), "db_login" : $("#hid_db_login").val(), "db_pw" : $("#hid_db_pwd").val(), "db_port" : $("#hid_db_port").val()}; |
||
| 198 | |||
| 199 | dataToUse = { |
||
| 200 | type: "step_"+step, |
||
| 201 | data: aesEncrypt(dataToUse), |
||
| 202 | activity: aesEncrypt(tsk[0]), |
||
| 203 | task: aesEncrypt(tsk[1]), |
||
| 204 | db: aesEncrypt(JSON.stringify(dbInfo)), |
||
| 205 | index: index, |
||
| 206 | multiple: multiple, |
||
| 207 | info: tsk[0]+"-"+tsk[1], |
||
| 208 | } |
||
| 209 | |||
| 210 | $.ajax({ |
||
| 211 | url: "install.queries.php", |
||
| 212 | type : 'POST', |
||
| 213 | dataType : "json", |
||
| 214 | data : dataToUse, |
||
| 215 | complete : function(data){ |
||
| 216 | data = $.parseJSON(data.responseText); |
||
| 217 | |||
| 218 | if (data[0].error !== "" ) { |
||
| 219 | alertify |
||
| 220 | .error('<i class="fas fa-ban mr-2"></i>Next ERROR occurred: <i>' + data[0].error + '</i><br />Please correct and relaunch.', 0) |
||
| 221 | .dismissOthers(); |
||
| 222 | return false; |
||
| 223 | } else { |
||
| 224 | if (data[0].result !== undefined && data[0].result !== "" ) { |
||
| 225 | alertify |
||
| 226 | .success('<i class="fas fa-check text-success mr-2"></i>' + data[0].result + '.<br>Click next to continue', 0) |
||
| 227 | .dismissOthers(); |
||
| 228 | } |
||
| 229 | $("#but_launch") |
||
| 230 | .prop("disabled", true) |
||
| 231 | .addClass("hidden"); |
||
| 232 | $("#but_next") |
||
| 233 | .prop("disabled", false) |
||
| 234 | .removeClass("hidden"); |
||
| 235 | } |
||
| 236 | |||
| 237 | /* |
||
| 238 | * Removing automatic action |
||
| 239 | // Go to next step |
||
| 240 | if (step <= 6) { |
||
| 241 | setTimeout( |
||
| 242 | function(){ |
||
| 243 | $('#but_next').trigger('click'); |
||
| 244 | }, |
||
| 245 | 1000 |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | */ |
||
| 249 | } |
||
| 250 | }); |
||
| 251 | } else { |
||
| 252 | $("#step_error").removeClass("hidden").html(error); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 363 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.