Passed
Push — development ( 22fbae...7d1ffd )
by Nils
09:59
created

upgrade.js ➔ httpRequest   B

Complexity

Conditions 5
Paths 18

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 22
Bugs 0 Features 0
Metric Value
cc 5
eloc 37
nc 18
nop 3
dl 0
loc 61
rs 8.5253
c 22
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C upgrade.js ➔ ... ➔ xhrObject.onreadystatechange 0 32 10

How to fix   Long Method   

Long Method

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:

1
/**
2
 * @file 		  upgrade.js
3
 * @author        Nils Laumaillé <[email protected]>
4
 * @version       2.1.27
5
 * @copyright     (c) 2009-2011 Nils Laumaillé
6
 * @license       GNU GPL-3.0
7
 * @link          https://www.teampass.net
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 */
13
14
// Function - do a pause during javascript execution
15
function pauseInExecution(millis)
16
{
17
    var date = new Date();
18
    var curDate = null;
0 ignored issues
show
Unused Code introduced by
The assignment to curDate seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
19
20
    do {
21
        curDate = new Date();
22
    } while(curDate-date < millis);
23
}
24
25
/**
26
 * Execute a file
27
 * @param  {[type]} file [description]
28
 * @param  {[type]} data [description]
29
 * @param  {[type]} type [description]
30
 * @return {[type]}      [description]
31
 */
32
function httpRequest(file, data, type) {
33
    var xhrObject = null;
34
    var isChrome = navigator.userAgent.toLowerCase().indexOf("chrome") > -1;
35
36
    if (document.getElementById("menu_action") !== null) {
37
        document.getElementById("menu_action").value = "action";
38
    }
39
40
    if(window.XMLHttpRequest) {
41
        // Firefox
42
        xhrObject = new XMLHttpRequest();
43
    } else if(window.ActiveXObject) {
44
        // Internet Explorer
45
        xhrObject = new ActiveXObject("Microsoft.XMLHTTP");
46
    } else {
47
        // XMLHttpRequest not supported by browser
48
        alert("Your browser does not support XMLHTTPRequest objects ...");
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
49
        return;
50
    }
51
52
    if (type === "GET") {
53
        xhrObject.open("GET", file + "?" + data, true);
54
        xhrObject.send(null);
55
    } else {
56
        xhrObject.open("POST", file, true);
57
        xhrObject.onreadystatechange = function() {
58
            if(xhrObject.readyState === 4) {
59
                eval(xhrObject.responseText);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
60
                //Check if query is for user identification. If yes, then reload page.
61
                if (data !== "" && data !== undefined && data.indexOf("ype=identify_user") > 0 ) {
62
                    if (isChrome === true ) {
63
                        // Needed pause for Chrome
64
                        pauseInExecution(100);
65
                    }
66
                    if (type === "") {
67
                        if (document.getElementById("erreur_connexion").style.display === "") {
68
                            //rise an error in url. This in order to display the eror after refreshing
69
                            window.location.href = encodeURI("index.php?error=rised");
70
                        } else {
71
                            window.location.href = encodeURI("index.php");
72
                        }
73
                    } else {
74
                        if (type === "?error=rised") {
75
                            if (document.getElementById("erreur_connexion").style.display === "none") {
76
                                // Clean error in url
77
                                type = "";
78
                            } else {
79
                                // Maintain the ERROR
80
                                type = "?error=rised";
81
                            }
82
                        }
83
                        // Redirection
84
                        window.location.href = encodeURI("index.php" + type);
85
                    }
86
                }
87
            }
88
        }
89
        xhrObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
90
        xhrObject.send(data);
91
    }
92
}