PhpDebugBar.Widget.extend.render   F
last analyzed

Complexity

Conditions 11
Paths 320

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
rs 3.8181
cc 11
nc 320
nop 1

How to fix   Complexity   

Complexity

Complex classes like PhpDebugBar.Widget.extend.render 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
(function($) {
2
3
    var csscls = PhpDebugBar.utils.makecsscls('phpdebugbar-widgets-');
0 ignored issues
show
Bug introduced by
The variable PhpDebugBar seems to be never declared. If this is a global, consider adding a /** global: PhpDebugBar */ 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...
4
5
    /**
6
     * Widget for the displaying sql queries
7
     *
8
     * Options:
9
     *  - data
10
     */
11
    var SQLQueriesWidget = PhpDebugBar.Widgets.SQLQueriesWidget = PhpDebugBar.Widget.extend({
0 ignored issues
show
Unused Code introduced by
The assignment to variable SQLQueriesWidget seems to be never used. Consider removing it.
Loading history...
12
13
        className: csscls('sqlqueries'),
14
15
        onFilterClick: function(el) {
16
            $(el).toggleClass(csscls('excluded'));
17
18
            var excludedLabels = [];
19
            this.$toolbar.find(csscls('.filter') + csscls('.excluded')).each(function() {
20
                excludedLabels.push(this.rel);
21
            });
22
23
            this.$list.$el.find("li[connection=" + $(el).attr("rel") + "]").toggle();
24
25
            this.set('exclude', excludedLabels);
26
        },
27
28
        render: function() {
29
            this.$status = $('<div />').addClass(csscls('status')).appendTo(this.$el);
30
31
            this.$toolbar = $('<div></div>').addClass(csscls('toolbar')).appendTo(this.$el);
32
33
            var filters = [], self = this;
34
35
            this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, stmt) {
0 ignored issues
show
Bug introduced by
The variable PhpDebugBar seems to be never declared. If this is a global, consider adding a /** global: PhpDebugBar */ 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...
36
                $('<code />').addClass(csscls('sql')).html(PhpDebugBar.Widgets.highlight(stmt.sql, 'sql')).appendTo(li);
0 ignored issues
show
Bug introduced by
The variable PhpDebugBar seems to be never declared. If this is a global, consider adding a /** global: PhpDebugBar */ 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...
37
                if (stmt.duration_str) {
38
                    $('<span title="Duration" />').addClass(csscls('duration')).text(stmt.duration_str).appendTo(li);
39
                }
40
                if (stmt.memory_str) {
41
                    $('<span title="Memory usage" />').addClass(csscls('memory')).text(stmt.memory_str).appendTo(li);
42
                }
43
                if (typeof(stmt.row_count) != 'undefined') {
44
                    $('<span title="Row count" />').addClass(csscls('row-count')).text(stmt.row_count).appendTo(li);
45
                }
46
                if (typeof(stmt.stmt_id) != 'undefined' && stmt.stmt_id) {
47
                    $('<span title="Prepared statement ID" />').addClass(csscls('stmt-id')).text(stmt.stmt_id).appendTo(li);
48
                }
49
                if (stmt.connection) {
50
                    $('<span title="Connection" />').addClass(csscls('database')).text(stmt.connection).appendTo(li);
51
                    li.attr("connection",stmt.connection);
52
                    if ( $.inArray(stmt.connection, filters) == -1 ) {
53
                        filters.push(stmt.connection);
54
                        $('<a />')
55
                            .addClass(csscls('filter'))
56
                            .text(stmt.connection)
57
                            .attr('rel', stmt.connection)
58
                            .on('click', function() { self.onFilterClick(this); })
59
                            .appendTo(self.$toolbar);
60
                        if (filters.length>1) {
61
                            self.$toolbar.show();
62
                            self.$list.$el.css("margin-bottom","20px");
63
                        }
64
                    }
65
                }
66
                if (typeof(stmt.is_success) != 'undefined' && !stmt.is_success) {
67
                    li.addClass(csscls('error'));
68
                    li.append($('<span />').addClass(csscls('error')).text("[" + stmt.error_code + "] " + stmt.error_message));
69
                }
70
                if (stmt.params && !$.isEmptyObject(stmt.params)) {
71
                    var table = $('<table><tr><th colspan="2">Params</th></tr></table>').addClass(csscls('params')).appendTo(li);
72
                    for (var key in stmt.params) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
73
                        if (typeof stmt.params[key] !== 'function') {
74
                            table.append('<tr><td class="' + csscls('name') + '">' + key + '</td><td class="' + csscls('value') +
75
                            '">' + stmt.params[key] + '</td></tr>');
76
                        }
77
                    }
78
                    li.css('cursor', 'pointer').click(function() {
79
                        if (table.is(':visible')) {
80
                            table.hide();
81
                        } else {
82
                            table.show();
83
                        }
84
                    });
85
                }
86
            }});
87
            this.$list.$el.appendTo(this.$el);
88
89
            this.bindAttr('data', function(data) {
90
                this.$list.set('data', data.statements);
91
                this.$status.empty();
92
93
                // Search for duplicate statements.
94
                for (var sql = {}, duplicate = 0, i = 0; i < data.statements.length; i++) {
95
                    var stmt = data.statements[i].sql;
96
                    if (data.statements[i].params && !$.isEmptyObject(data.statements[i].params)) {
97
                        stmt += ' {' + $.param(data.statements[i].params, false) + '}';
98
                    }
99
                    sql[stmt] = sql[stmt] || { keys: [] };
100
                    sql[stmt].keys.push(i);
101
                }
102
                // Add classes to all duplicate SQL statements.
103
                for (var stmt in sql) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable stmt already seems to be declared on line 95. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
104
                    if (sql[stmt].keys.length > 1) {
105
                        duplicate++;
106
                        for (var i = 0; i < sql[stmt].keys.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 94. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
107
                            this.$list.$el.find('.' + csscls('list-item')).eq(sql[stmt].keys[i])
108
                                .addClass(csscls('sql-duplicate')).addClass(csscls('sql-duplicate-'+duplicate));
109
                        }
110
                    }
111
                }
112
113
                var t = $('<span />').text(data.nb_statements + " statements were executed").appendTo(this.$status);
114
                if (data.nb_failed_statements) {
115
                    t.append(", " + data.nb_failed_statements + " of which failed");
116
                }
117
                if (duplicate) {
118
                    t.append(", " + duplicate + " of which were duplicated");
119
                }
120
                if (data.accumulated_duration_str) {
121
                    this.$status.append($('<span title="Accumulated duration" />').addClass(csscls('duration')).text(data.accumulated_duration_str));
122
                }
123
                if (data.memory_usage_str) {
124
                    this.$status.append($('<span title="Memory usage" />').addClass(csscls('memory')).text(data.memory_usage_str));
125
                }
126
            });
127
        }
128
129
    });
130
131
})(PhpDebugBar.$);
0 ignored issues
show
Bug introduced by
The variable PhpDebugBar seems to be never declared. If this is a global, consider adding a /** global: PhpDebugBar */ 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...
132