| Conditions | 20 |
| Paths | 88 |
| Total Lines | 186 |
| 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 | /** |
||
| 30 | function CheckPage() |
||
| 31 | { |
||
| 32 | var step = $("#page_id").val(); |
||
| 33 | var data = ""; |
||
| 34 | var error = ""; |
||
| 35 | var index = ""; |
||
| 36 | var tasks = []; |
||
| 37 | var multiple = ""; |
||
| 38 | var error_msg = ""; |
||
|
|
|||
| 39 | $("#step_error").hide().html(""); |
||
| 40 | $("#res_"+step).html(""); |
||
| 41 | |||
| 42 | if (step === "2") { |
||
| 43 | // STEP 2 |
||
| 44 | if ($("#url_path").val() === "" || $("#root_path").val() === "") { |
||
| 45 | error = "Fields need to be filled in!"; |
||
| 46 | } else { |
||
| 47 | data = '{"root_path":"'+$("#root_path").val()+'", "url_path":"'+$("#url_path").val()+'"}'; |
||
| 48 | tasks = ["folder*install", "folder*includes", "folder*files", "folder*upload", "extension*mcrypt", "extension*mbstring", "extension*openssl", "extension*bcmath", "extension*iconv", "extension*gd", "function*mysqli_fetch_all", "version*php", "ini*max_execution_time", "folder*includes/avatars", "extension*xml", "folder*includes/libraries/csrfp/libs", "folder*includes/libraries/csrfp/js", "folder*includes/libraries/csrfp/log", "folder*includes/config", "extension*curl"]; |
||
| 49 | multiple = true; |
||
| 50 | $("#hid_abspath").val($("#root_path").val()); |
||
| 51 | $("#hid_url_path").val($("#url_path").val()); |
||
| 52 | } |
||
| 53 | } else if (step === "3") { |
||
| 54 | // STEP 3 |
||
| 55 | if ($("#db_host").val() === "" || $("#db_db").val() === "" || $("#db_login").val() === "" || $("#db_port").val() == "") { |
||
| 56 | error = "Paths need to be filled in!"; |
||
| 57 | } else if ($("#db_pw").val().indexOf('"') > -1) { |
||
| 58 | error = "Double quotes in password not allowed!"; |
||
| 59 | } else { |
||
| 60 | data = '{"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()+'", "abspath":"'+$("#hid_abspath").val()+'", "url_path":"'+$("#hid_url_path").val()+'"}'; |
||
| 61 | tasks = ["connection*test"]; |
||
| 62 | multiple = ""; |
||
| 63 | $("#hid_db_host").val($("#db_host").val()); |
||
| 64 | $("#hid_db_bdd").val($("#db_bdd").val()); |
||
| 65 | $("#hid_db_login").val($("#db_login").val()); |
||
| 66 | $("#hid_db_pwd").val($("#db_pw").val()); |
||
| 67 | $("#hid_db_port").val($("#db_port").val()); |
||
| 68 | } |
||
| 69 | } else if (step === "4") { |
||
| 70 | // STEP 4 |
||
| 71 | if ($("#admin_pwd").val() === "") { |
||
| 72 | error = "You must define a password for Admin account!"; |
||
| 73 | } else{ |
||
| 74 | data = '{"tbl_prefix":"'+sanitizeString($("#tbl_prefix").val())+'", "sk_path":"'+sanitizeString($("#sk_path").val())+'", "admin_pwd":"'+sanitizeString($("#admin_pwd").val())+'", "send_stats":""}'; |
||
| 75 | tasks = ["misc*preparation"]; |
||
| 76 | multiple = ""; |
||
| 77 | } |
||
| 78 | } else if (step === "5") { |
||
| 79 | // STEP 5 |
||
| 80 | data = ''; |
||
| 81 | tasks = ["table*items", "table*log_items", "table*misc", "table*nested_tree", "table*rights", "table*users", "populate*admin", "table*tags", "table*log_system", "table*files", "table*cache", "table*roles_title", "table*roles_values", "table*kb", "table*kb_categories", "table*kb_items", "table*restriction_to_roles", "table*languages", "table*emails", "table*automatic_del", "table*items_edition", "table*categories", "table*categories_items", "table*categories_folders", "table*api", "table*otv", "table*suggestion", "table*tokens", "table*items_change"]; |
||
| 82 | multiple = true; |
||
| 83 | } else if (step === "6") { |
||
| 84 | // STEP 6 |
||
| 85 | data = '{"url_path":"'+sanitizeString($("#hid_url_path").val())+'"}'; |
||
| 86 | tasks = ["file*settings.php", "file*sk.php", "file*security", "file*teampass-seckey", "file*csrfp-token"]; |
||
| 87 | multiple = true; |
||
| 88 | } else if (step === "7") { |
||
| 89 | // STEP 7 |
||
| 90 | data = ''; |
||
| 91 | tasks = ["file*deleteInstall"]; |
||
| 92 | multiple = true; |
||
| 93 | } |
||
| 94 | |||
| 95 | // launch query |
||
| 96 | if (error === "" && multiple === true) { |
||
| 97 | var globalResult = true; |
||
| 98 | var ajaxReqs = []; |
||
| 99 | var tsk; |
||
| 100 | |||
| 101 | $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">"); |
||
| 102 | $("#step_res").val("true"); |
||
| 103 | $('#pop_db').html(""); |
||
| 104 | |||
| 105 | for (index = 0; index < tasks.length; ++index) { |
||
| 106 | tsk = tasks[index].split("*"); |
||
| 107 | ajaxReqs.push($.ajax({ |
||
| 108 | url: "install.queries.php", |
||
| 109 | type : 'POST', |
||
| 110 | dataType : "json", |
||
| 111 | data : { |
||
| 112 | type: "step_"+step, |
||
| 113 | data: aes_encrypt(data), // |
||
| 114 | activity: aes_encrypt(tsk[0]), |
||
| 115 | task: aes_encrypt(tsk[1]), |
||
| 116 | db: aes_encrypt('{"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()+'"}'), |
||
| 117 | index: index, |
||
| 118 | multiple: multiple |
||
| 119 | }, |
||
| 120 | complete : function(data, statut){ |
||
| 121 | if (data.responseText == "") { |
||
| 122 | // stop error occured, PHP5.5 installed? |
||
| 123 | $("#step_result").html("[ERROR] Answer from server is empty. This may occur if PHP version is not at least 5.5. Please check this this fit your server configuration!"); |
||
| 124 | } else { |
||
| 125 | data = $.parseJSON(data.responseText); |
||
| 126 | if (data[0].error === "") { |
||
| 127 | if (step === "5") { |
||
| 128 | if (data[0].activity === "table") { |
||
| 129 | $('#pop_db').append('<li>Table <b>'+data[0].task+'</b> created</li>'); |
||
| 130 | } else if (data[0].activity === "entry") { |
||
| 131 | $('#pop_db').append('<li>Entries <b>'+data[0].task+'</b> were added</li>'); |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | $("#res"+step+"_check"+data[0].index).html("<img src=\"images/tick.png\">"); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (data[0].result !== undefined && data[0].result !== "" ) { |
||
| 138 | $("#step_result").html(data[0].result); |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | // ignore setting error if regarding setting permissions (step 6, index 2) |
||
| 142 | if (step+data[0].index !== "62") { |
||
| 143 | $("#step_res").val("false"); |
||
| 144 | } |
||
| 145 | $("#res"+step+"_check"+data[0].index).html("<img src=\"images/exclamation-red.png\"> <i>"+data[0].error+"</i>"); |
||
| 146 | $('#pop_db').append('<li><img src=\"images/exclamation-red.png\"> Error on task `<b>'+data[0].activity+' > '+data[0].task+'`</b>. <i>'+data[0].error+'</i></li>'); |
||
| 147 | if (data[0].result !== undefined && data[0].result !== "" ) { |
||
| 148 | $("#step_result").html(data[0].result); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | })); |
||
| 154 | } |
||
| 155 | $.when.apply($, ajaxReqs).done(function(data, statut) { |
||
| 156 | setTimeout(function(){ |
||
| 157 | // all requests are complete |
||
| 158 | if ($("#step_res").val() === "false") { |
||
| 159 | data = $.parseJSON(data.responseText); |
||
| 160 | $("#step_error").show().html("At least one task has failed! Please correct and relaunch. "); |
||
| 161 | $("#res_"+step).html("<img src=\"images/exclamation-red.png\">"); |
||
| 162 | } else { |
||
| 163 | $("#but_launch").prop("disabled", true); |
||
| 164 | $("#but_launch").hide(); |
||
| 165 | $("#but_next").prop("disabled", false); |
||
| 166 | $("#but_next").show(); |
||
| 167 | // Hide restart button at end of step 6 if successful |
||
| 168 | if (step === "7") { |
||
| 169 | $("#but_restart").prop("disabled", true); |
||
| 170 | $("#but_restart").hide(); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | $("#step_result").html(""); |
||
| 174 | }, 1000); |
||
| 175 | }); |
||
| 176 | } else if (error === "" && multiple === "") { |
||
| 177 | $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">"); |
||
| 178 | var tsk = tasks[0].split("*"); |
||
| 179 | $.ajax({ |
||
| 180 | url: "install.queries.php", |
||
| 181 | type : 'POST', |
||
| 182 | dataType : "json", |
||
| 183 | data : { |
||
| 184 | type: "step_"+step, |
||
| 185 | data: aes_encrypt(data), |
||
| 186 | activity: aes_encrypt(tsk[0]), |
||
| 187 | task: aes_encrypt(tsk[1]), |
||
| 188 | db: aes_encrypt('{"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()+'"}'), |
||
| 189 | index: index, |
||
| 190 | multiple: multiple |
||
| 191 | }, |
||
| 192 | complete : function(data, statut){ |
||
| 193 | data = $.parseJSON(data.responseText); |
||
| 194 | $("#step_result").html(""); |
||
| 195 | if (data[0].error !== "" ) { |
||
| 196 | $("#step_error").show().html("The next ERROR occurred: <i>'"+data[0].error+"'</i><br />Please correct and relaunch."); |
||
| 197 | $("#res_"+step).html("<img src=\"images/exclamation-red.png\">"); |
||
| 198 | } else { |
||
| 199 | if (data[0].result !== undefined && data[0].result !== "" ) { |
||
| 200 | $("#step_result").html("<span style=\"font-weight:bold; margin-right:20px;\">"+data[0].result+"</span>"); |
||
| 201 | } |
||
| 202 | $("#but_launch").prop("disabled", true); |
||
| 203 | $("#but_launch").hide(); |
||
| 204 | $("#but_next").prop("disabled", false); |
||
| 205 | $("#but_next").show(); |
||
| 206 | } |
||
| 207 | }, |
||
| 208 | error : function(resultat, statut, erreur){ |
||
| 209 | |||
| 210 | } |
||
| 211 | }); |
||
| 212 | } else { |
||
| 213 | $("#step_error").show().html(error); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 260 |