js/lib/API/runtime.js   B
last analyzed

Complexity

Total Complexity 43
Complexity/F 1.34

Size

Lines of Code 150
Function Count 32

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 150
rs 8.3157
wmc 43
mnd 2
bc 52
fnc 32
bpm 1.625
cpm 1.3436
noi 0

15 Functions

Rating   Name   Duplication   Size   Complexity  
A API.runtime.setUninstallURL 0 16 2
A API.runtime.sendNativeMessage 0 12 2
A API.runtime.getURL 0 3 1
A API.runtime.getBackgroundPage 0 12 2
A API.runtime.getPackageDirectoryEntry 0 12 2
A API.runtime.reload 0 3 1
A API.runtime.requestUpdateCheck 0 12 2
A API.runtime.connect 0 3 1
A API.runtime.sendMessage 0 12 2
A API.runtime.getBrowserInfo 0 11 2
A API.runtime.connectNative 0 3 1
A API.runtime.getManifest 0 3 1
A API.runtime.openOptionsPage 0 12 2
A API.runtime.restart 0 5 2
A API.runtime.getPlatformInfo 0 12 2

How to fix   Complexity   

Complexity

Complex classes like js/lib/API/runtime.js 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
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
7
/* global API */
8
9
API.runtime = {
10
    getBackgroundPage: function() {
11
        if (API.promise) {
12
            return API.api.runtime.getBackgroundPage();
13
        }
14
        else {
15
            return new C_Promise(function() {
16
                API.api.runtime.getBackgroundPage((function(backgroundPage){
17
                    this.call_then(backgroundPage);
18
                }).bind(this));
19
            });
20
        }
21
    },
22
    openOptionsPage: function() {
23
        if (API.promise) {
24
            return API.api.runtime.openOptionsPage();
25
        }
26
        else {
27
            return new C_Promise(function() {
28
                API.api.runtime.openOptionsPage((function() {
29
                    this.call_then();
30
                }).bind(this));
31
            });
32
        }
33
    },
34
    getManifest: function() {
35
        return API.api.runtime.getManifest();
36
    },
37
    getURL: function(url) {
38
        return API.api.runtime.getURL(url);
39
    },
40
    setUninstallURL: function(url) {
41
        if (API.promise) {
42
            return API.api.runtime.setUninstallURL(url);
43
        }
44
        else {
45
            return new C_Promise(function() {
46
                API.api.runtime.setUninstallURL(url, function() {
47
                    if (typeof API.api.runtime.lastError !== "undefined") {
48
                        this.call_error(API.api.runtime.lastError);
49
                        return;
50
                    }
51
                    this.call_then();
52
                });
53
            });
54
        }
55
    },
56
    reload: function() {
57
        API.api.runtime.reload();
58
    },
59
    requestUpdateCheck: function() {
60
        if (API.promise) {
61
            return API.api.runtime.requestUpdateCheck();
62
        }
63
        else {
64
            return new C_Promise(function() {
65
                API.api.runtime.requestUpdateCheck((function(status, details) {
66
                    this.call_then(status, details);
67
                }).bind(this));
68
            });
69
        }
70
    },
71
    restart: function() {
72
        if ( ! API.promise) {
73
            API.api.runtime.restart();
74
        }
75
    },
76
    connect: function(extensionId, connectionInfo) {
77
        return API.api.runtime.connect(extensionId, connectionInfo);
78
    },
79
    connectNative: function(application) {
80
        return API.api.runtime.connectNative(application);
81
    },
82
    sendMessage: function(extensionId, message, options) {
83
        if (API.promise) {
84
            return API.api.runtime.sendMessage(extensionId, message, options);
85
        }
86
        else {
87
            return new C_Promise(function(){
88
                API.api.runtime.sendMessage(extensionId, message, options, (function(response) {
89
                    this.call_then(response);
90
                }).bind(this));
91
            });
92
        }
93
    },
94
    sendNativeMessage: function(application, message) {
95
        if (API.promise) {
96
            return API.api.runtime.sendNativeMessage(application, message);
97
        }
98
        else {
99
            return new C_Promise(function() {
100
                API.api.runtime.sendNativeMessage(application, message, (function(response) {
101
                    this.call_then(response);
102
                }).bind(this));
103
            });
104
        }
105
    },
106
    getPlatformInfo: function() {
107
        if (API.promise) {
108
            return API.api.runtime.getPlatformInfo();
109
        }
110
        else {
111
            return new C_Promise(function() {
112
                API.api.runtime.getPlatformInfo((function(info) {
113
                    this.call_then(info);
114
                }).bind(this));
115
            });
116
        }
117
    },
118
    getBrowserInfo: function() {
119
        if (API.promise) {
120
            return API.api.runtime.getBrowserInfo();
121
        }
122
        else {
123
            return new C_Promise(function() {
124
                //  Not implemented in chrome
125
                this.call_then(null);
126
            });
127
        }
128
    },
129
    getPackageDirectoryEntry: function() {
130
        if (API.promise) {
131
            return API.api.runtime.getPackageDirectoryEntry();
132
        }
133
        else {
134
            return new C_Promise(function() {
135
                API.api.runtime.getPackageDirectoryEntry((function(directoryEntry) {
136
                    this.call_then(directoryEntry);
137
                }).bind(this));
138
            });
139
        }
140
    },
141
142
    id: API.api.runtime.id,
143
    
144
    /**
145
     * Events from now on!
146
     */
147
    onStartup: API.api.runtime.onStartup,
148
    onInstalled: API.api.runtime.onInstalled,
149
    onSuspend: API.api.runtime.onSuspend,
150
    onSuspendCanceled: API.api.runtime.onSuspendCanceled,
151
    onUpdateAvailable: API.api.runtime.onUpdateAvailable,
152
    onBrowserUpdateAvailable: API.api.runtime.onBrowserUpdateAvailable,
153
    onConnect: API.api.runtime.onConnect,
154
    onConnectExternal: API.api.runtime.onConnectExternal,
155
    onMessage: API.api.runtime.onMessage,
156
    onMessageExternal: API.api.runtime.onMessageExternal,
157
    onRestartRequired: API.api.runtime.onRestartRequired
158
};
159