Passed
Push — development ( b3bbd1...d832f6 )
by Nils
05:00 queued 01:04
created

T_FUNCTION ➔ ... ➔ xhrObject.onreadystatechange   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 16
Bugs 0 Features 0
Metric Value
cc 10
c 16
b 0
f 0
nc 12
nop 0
dl 0
loc 1
rs 4.8196

How to fix   Complexity   

Complexity

Complex classes like T_FUNCTION ➔ ... ➔ xhrObject.onreadystatechange 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
/**
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
}