Passed
Push — master ( 83071b...9cc500 )
by Ahmet
02:54
created

qb   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 229
Duplicated Lines 3.93 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 41
c 1
b 0
f 0
lcom 1
cbo 0
dl 9
loc 229
rs 8.2769

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A resetTables() 0 6 1
A setTable() 0 7 1
A table() 0 6 1
A resetWrite() 0 8 1
A set() 0 8 1
A resetConditions() 0 6 1
A and() 0 6 1
A or() 0 6 1
A c() 0 8 2
A resetReadFields() 0 6 1
A select() 0 6 1
A groupBy() 0 6 1
A orderBy() 0 6 1
C getSelect() 3 41 12
B getUpdate() 3 24 6
A getInsert() 0 18 3
A getDelete() 3 12 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like qb 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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.

While breaking up the class, it is a good idea to analyze how other classes use qb, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace ahmetertem;
4
5
/**
6
 * Query builder.
7
 */
8
class qb
9
{
10
    private $_conditions = array();
11
    private $_table_names = array();
12
    private $_read_fields = array();
13
    private $_write_fields = array();
14
    private $_write_values = array();
15
    private $_write_field_types = array();
16
    private $_order_fields = array();
17
    private $_group_fields = array();
18
    public $limit = 100;
19
    public $limit_offset = -1;
20
    public static $default_limit = 100;
21
22
    public function __construct($table_name = null)
23
    {
24
        $this->limit = self::$default_limit;
25
        if (!is_null($table_name)) {
26
            $this->table($table_name);
27
        }
28
    }
29
30
    public function resetTables()
31
    {
32
        $this->_table_names = array();
33
34
        return true;
35
    }
36
37
    public function setTable($table_name)
38
    {
39
        $this->resetTables();
40
        $this->table($table_name);
41
42
        return $this;
43
    }
44
45
    public function table($table_name)
46
    {
47
        $this->_table_names[] = $table_name;
48
49
        return $this;
50
    }
51
52
    public function resetWrite()
53
    {
54
        $this->_write_fields = array();
55
        $this->_write_values = array();
56
        $this->_write_field_types = array();
57
58
        return $this;
59
    }
60
61
    /**
62
     * $type :
63
     *        - 0 = string
64
     *        - 1 = integer
65
     *        - 2 = raw.
66
     */
67
    public function set($field, $value, $type = 0)
68
    {
69
        $this->_write_fields[] = $field;
70
        $this->_write_values[] = $value;
71
        $this->_write_field_types[] = $type;
72
73
        return $this;
74
    }
75
76
    public function resetConditions()
77
    {
78
        $this->_conditions = array();
79
80
        return $this;
81
    }
82
83
    public function and($field, $value = null, $operator = '=')
84
    {
85
        $this->_conditions[] = $this->c($field, $value, $operator);
86
87
        return $this;
88
    }
89
90
    /**
91
     * Adds "<strong><em>or</em></strong>" condition to current and condition
92
     * array.
93
     */
94
    public function or()
95
    {
96
        $this->_conditions[] = '('.implode(' or ', func_get_args()).')';
97
98
        return $this;
99
    }
100
101
    public static function c($field, $value = null, $operator = '=')
102
    {
103
        if (is_null($value)) {
104
            return $field;
105
        } else {
106
            return "{$field} {$operator} {$value}";
107
        }
108
    }
109
110
    public function resetReadFields()
111
    {
112
        $this->_read_fields = array();
113
114
        return $this;
115
    }
116
117
    public function select($field)
118
    {
119
        $this->_read_fields[] = $field;
120
121
        return $this;
122
    }
123
124
    public function groupBy($field)
125
    {
126
        $this->_group_fields[] = $field;
127
128
        return $this;
129
    }
130
131
    public function orderBy($field, $asc = true)
132
    {
133
        $this->_order_fields[$field] = array($field, $asc);
134
135
        return $this;
136
    }
137
138
    public function getSelect()
139
    {
140
        $_read_fields = $this->_read_fields;
141
        if (count($_read_fields) == 0) {
142
            $_read_fields[] = '*';
143
        }
144
        $limit = null;
145 View Code Duplication
        if ($this->limit > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
147
        }
148
        $group = null;
149
        if (count($this->_group_fields) > 0) {
150
            $group = ' group by '.implode(', ', $this->_group_fields).' ';
151
        }
152
        $order = null;
153
        if (count($this->_order_fields) > 0) {
154
            $order = ' order by ';
155
            $i = 0;
156
            foreach ($this->_order_fields as $of) {
157
                $order .= ($i > 0 ? ', ' : null);
158
                if (!is_null($of[1])) {
159
                    $pos = strpos($of[0], '.');
160
                    if ($pos !== false) {
161
                        $t = explode('.', $of[0]);
162
                        $t[1] = '`'.$t[1].'`';
163
                        $of[0] = implode('.', $t);
164
                        $order .= $of[0];
165
                    } else {
166
                        $order .= '`'.$of[0].'`';
167
                    }
168
                    $order .= ' '.($of[1] ? 'asc' : 'desc');
169
                } else {
170
                    $order .= $of[0];
171
                }
172
                ++$i;
173
            }
174
        }
175
        $string = sprintf('select %1$s from %2$s%3$s%6$s%4$s%5$s', implode(', ', $_read_fields), implode(', ', $this->_table_names), count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null, $order, $limit, $group);
176
177
        return $string;
178
    }
179
180
    public function getUpdate()
181
    {
182
        $updates = array();
183
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
184
            $t = $this->_write_fields[$d].'=';
185
            switch ($this->_write_field_types[$d]) {
186
            case 0:
187
              $t .= "'".$this->_write_values[$d]."'";
188
              break;
189
          default:
190
              $t .= $this->_write_values[$d];
191
          }
192
            $updates[] = $t;
193
        }
194
        $update_fields = implode(', ', $updates);
195
        $limit = null;
196 View Code Duplication
        if ($this->limit > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
198
        }
199
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
200
        $string = sprintf('update %1$s set %2$s %3$s %4$s', $this->_table_names[0], $update_fields, $where, $limit);
201
202
        return $string;
203
    }
204
205
    public function getInsert()
206
    {
207
        $table = $this->_table_names[0];
208
        $fields = '`'.implode('`, `', $this->_write_fields).'`';
209
        $values = array();
210
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
211
            switch ($this->_write_field_types[$d]) {
212
            case 0:
213
              $values[] = "'".$this->_write_values[$d]."'";
214
              break;
215
            default:
216
              $values[] = $this->_write_values[$d];
217
          }
218
        }
219
        $string = sprintf('insert into %1$s (%2$s) values(%3$s)', $table, $fields, implode(', ', $values));
220
221
        return $string;
222
    }
223
224
    public function getDelete()
225
    {
226
        $table = $this->_table_names[0];
227
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
228
        $limit = null;
229 View Code Duplication
        if ($this->limit > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
231
        }
232
        $string = sprintf('delete from %1$s %2$s %3$s', $table, $where, $limit);
233
234
        return $string;
235
    }
236
}
237