FasterTablesFilter::tablesPrint()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 73
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 73
rs 9.0675

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * Faster tables filter plugin
5
 * ===========================
6
 * Useful when there's way too many tables than it shoud be and Adminer Tables Filter is slow
7
 *
8
 * @author Martin Macko, https://github.com/linkedlist
9
 * @license http://http://opensource.org/licenses/MIT, The MIT License (MIT)
10
 */
11
class FasterTablesFilter {
12
13
	function tablesPrint($tables) { ?>
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
15
  <p class="jsonly"><input id="filter-field">
16
  <style>
17
    .select-text {
18
      margin-right: 5px;
19
    }
20
  </style>
21
  <p id='tables'></p>
22
  <script type="text/javascript">
23
  var filterf = function () {
24
    var spanProto = document.createElement('span');
25
    var aProto = document.createElement('a');
26
    var brProto = document.createElement('br');
27
28
    function appendTables() {
29
      var fragment = document.createDocumentFragment();
30
      var item;
31
      for (var i = 0, len = tempTables.length; i < len; i++) {
32
          item = tempTables[i];
33
          var span = spanProto.cloneNode();
34
35
          var aSelect = aProto.cloneNode();
36
          aSelect.href = hMe+"select="+item;
37
          aSelect.text = langSelect;
38
          aSelect.className = "select-text";
39
40
          var aName = aProto.cloneNode();
41
          aName.href = hMe+"table="+item;
42
          aName.text = item;
43
          span.appendChild(aSelect);
44
45
46
          span.appendChild(aName);
47
          var br = brProto.cloneNode();
48
          span.appendChild(br);
49
          fragment.appendChild(span);
50
      }
51
      tableDiv.appendChild(fragment);
52
    }
53
54
    var tables = [<?php foreach($tables as $table => $type) { echo "'".urlencode($table) ."'" . ',';}?>];
55
    var tempTables = tables;
56
    var hMe = "<?php echo h(ME) ?>";
57
    hMe = hMe.replace(/&amp;/g, '&');
58
    var langSelect = "<?php echo lang('select');?>";
59
60
61
    var filter = document.getElementById("filter-field");
62
    var tableDiv = document.getElementById("tables");
63
    filter.onkeyup = function(event) {
64
65
      while(tableDiv.firstChild) {
66
        tableDiv.removeChild(tableDiv.firstChild);
67
      }
68
69
      tempTables = [];
70
      var value = this.value.toLowerCase();
71
      var item;
72
      for (var i = 0, len = tables.length; i < len; i++) {
73
          item = tables[i];
74
          if(item.toLowerCase().indexOf(value) > -1) {
75
            tempTables.push(item);
76
          }
77
      }
78
      appendTables();
79
    };
80
81
    appendTables();
82
  }
83
  filterf();
84
</script>
85
<?php return true;}} ?>
86