Conditions | 15 |
Paths | 512 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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($) { |
||
35 | this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, stmt) { |
||
36 | $('<code />').addClass(csscls('sql')).html(PhpDebugBar.Widgets.highlight(stmt.sql, 'sql')).appendTo(li); |
||
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) { |
||
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); |
||
132 |
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.