Passed
Push — v4 ( 258440...dc0afb )
by Benjamin
05:58
created

T_IDENTIFIER ➔ ... ➔ $.proxy   D

Complexity

Conditions 10
Paths 4

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 4
nop 2
dl 0
loc 1
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like T_IDENTIFIER ➔ ... ➔ $.proxy 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
/** global: Analytics */
2
/** global: google */
3
/**
4
 * Utils
5
 */
6
Analytics.Utils = {
0 ignored issues
show
Bug introduced by
The variable Analytics seems to be never declared. If this is a global, consider adding a /** global: Analytics */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
7
    responseToDataTable: function(response, localeDefinition) {
8
        var dataTable = new google.visualization.DataTable();
0 ignored issues
show
Bug introduced by
The variable google seems to be never declared. If this is a global, consider adding a /** global: google */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
10
11
        // Columns
12
13
        $.each(response.cols, function(key, column) {
14
            var dataTableColumnType;
15
16
            switch (column.type) {
17
                case 'date':
18
                    dataTableColumnType = 'date';
19
                    break;
20
                case 'percent':
21
                case 'time':
22
                case 'integer':
23
                case 'currency':
24
                case 'float':
25
                    dataTableColumnType = 'number';
26
                    break;
27
28
                default:
29
                    dataTableColumnType = 'string';
30
            }
31
32
            dataTable.addColumn({
33
                type: dataTableColumnType,
34
                label: column.label,
35
                id: column.id,
36
            });
37
        });
38
39
40
        // Rows
41
42
        $.each(response.rows, $.proxy(function(keyRow, row) {
43
44
            var dataTableRow = [];
45
46
            $.each(response.cols, $.proxy(function(keyColumn, column) {
47
                switch (column.type) {
48
                    case 'date':
49
                        dataTableRow[keyColumn] = Analytics.Utils.formatByType(localeDefinition, column.type, row[keyColumn]);
0 ignored issues
show
Bug introduced by
The variable Analytics seems to be never declared. If this is a global, consider adding a /** global: Analytics */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
50
                        break;
51
52
                    case 'float':
53
                        dataTableRow[keyColumn] = +row[keyColumn];
54
                        break;
55
56
                    case 'integer':
57
                    case 'currency':
58
                    case 'percent':
59
                    case 'time':
60
                    case 'continent':
61
                    case 'subContinent':
62
                        dataTableRow[keyColumn] = {
63
                            v: Analytics.Utils.formatRawValueByType(localeDefinition, column.type, row[keyColumn]),
64
                            f: Analytics.Utils.formatByType(localeDefinition, column.type, row[keyColumn])
65
                        };
66
                        break;
67
68
                    default:
69
                        dataTableRow[keyColumn] = row[keyColumn];
70
                }
71
            }, this));
72
73
            dataTable.addRow(dataTableRow);
74
75
        }, this));
76
77
        return dataTable;
78
    },
79
80
    formatRawValueByType: function(localeDefinition, type, value) {
81
        switch (type) {
82
            case 'integer':
83
            case 'currency':
84
            case 'percent':
85
            case 'time':
86
            case 'float':
87
                return +value;
88
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
89
90
            default:
91
                return value;
92
        }
93
    },
94
95
    formatByType: function(localeDefinition, type, value) {
96
        switch (type) {
97
            case 'continent':
98
                return Analytics.Metadata.getContinentByCode(value);
0 ignored issues
show
Bug introduced by
The variable Analytics seems to be never declared. If this is a global, consider adding a /** global: Analytics */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
99
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
100
101
            case 'subContinent':
102
                return Analytics.Metadata.getSubContinentByCode(value);
103
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
104
105
            case 'currency':
106
                return Analytics.Utils.formatCurrency(localeDefinition, +value);
107
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
108
109
            case 'float':
110
                return +value;
111
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
112
113
            case 'integer':
114
                return Analytics.Utils.formatInteger(localeDefinition, +value);
115
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
116
117
            case 'time':
118
                return Analytics.Utils.formatDuration(+value);
119
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
120
121
            case 'percent':
122
                return Analytics.Utils.formatPercent(localeDefinition, +value);
123
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
124
125
            case 'date':
126
                $dateString = value;
0 ignored issues
show
Bug introduced by
The variable $dateString seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$dateString.
Loading history...
127
128
                if ($dateString.length == 8) {
129
                    // 20150101
130
131
                    $year = eval($dateString.substr(0, 4));
0 ignored issues
show
Bug introduced by
The variable $year seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$year.
Loading history...
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...
132
                    $month = eval($dateString.substr(4, 2)) - 1;
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...
Bug introduced by
The variable $month seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$month.
Loading history...
133
                    $day = eval($dateString.substr(6, 2));
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...
Bug introduced by
The variable $day seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$day.
Loading history...
134
135
                    $date = new Date($year, $month, $day);
0 ignored issues
show
Bug introduced by
The variable $date seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$date.
Loading history...
136
137
                    return $date;
138
                } else if ($dateString.length == 6) {
139
                    // 201501
140
141
                    $year = eval($dateString.substr(0, 4));
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...
142
                    $month = eval($dateString.substr(4, 2)) - 1;
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...
143
144
                    $date = new Date($year, $month, '01');
145
146
                    return $date;
147
                }
148
                break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
149
150
            default:
151
                return value;
152
                break;
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
153
        }
154
    },
155
156
    formatCurrency: function(localeDefinition, value) {
157
        var d3Locale = this.getD3Locale(localeDefinition);
158
        var formatter = d3Locale.format(Craft.charts.BaseChart.defaults.formats.currencyFormat);
0 ignored issues
show
Bug introduced by
The variable Craft seems to be never declared. If this is a global, consider adding a /** global: Craft */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
159
160
        return formatter(value);
161
    },
162
163
    formatDuration: function(_seconds) {
164
        var sec_num = parseInt(_seconds, 10); // don't forget the second param
165
        var hours = Math.floor(sec_num / 3600);
166
        var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
167
        var seconds = sec_num - (hours * 3600) - (minutes * 60);
168
169
        if (hours < 10) {
170
            hours = "0" + hours;
171
        }
172
        if (minutes < 10) {
173
            minutes = "0" + minutes;
174
        }
175
        if (seconds < 10) {
176
            seconds = "0" + seconds;
177
        }
178
        return hours + ':' + minutes + ':' + seconds;
179
    },
180
181
    formatInteger: function(localeDefinition, value) {
182
        return this.getD3Locale(localeDefinition).format(",")(value);
183
    },
184
185
    formatPercent: function(localeDefinition, value) {
186
        return this.getD3Locale(localeDefinition).format(Craft.charts.BaseChart.defaults.formats.percentFormat)(value / 100);
0 ignored issues
show
Bug introduced by
The variable Craft seems to be never declared. If this is a global, consider adding a /** global: Craft */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
187
    },
188
189
    getD3Locale: function(localeDefinition) {
190
        return d3.formatLocale(localeDefinition);
0 ignored issues
show
Bug introduced by
The variable d3 seems to be never declared. If this is a global, consider adding a /** global: d3 */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
191
    },
192
};
193