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