Completed
Push — master ( 2ae423...cd04f7 )
by Ahmet
02:54
created

qb   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 254
Duplicated Lines 3.54 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 48
c 4
b 0
f 2
lcom 1
cbo 0
dl 9
loc 254
rs 8.4864

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resetTable() 0 6 1
A setTable() 0 7 1
A table() 0 6 1
A resetSet() 0 8 1
A set() 0 8 1
A resetWhere() 0 6 1
A where() 0 6 1
A whereOr() 0 6 1
A c() 0 8 2
A resetSelect() 0 6 1
A select() 0 6 1
A groupBy() 0 6 1
A orderBy() 0 6 1
A setLimit() 0 9 2
C getSelect() 3 44 13
C getUpdate() 3 30 8
B getInsert() 0 24 5
B getDelete() 3 15 5

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()
23
    {
24
        $this->limit = self::$default_limit;
25
    }
26
27
    public function resetTable()
28
    {
29
        $this->_table_names = array();
30
31
        return true;
32
    }
33
34
    public function setTable($table_name)
35
    {
36
        $this->resetTable();
37
        $this->table($table_name);
38
39
        return $this;
40
    }
41
42
    public function table($table_name)
43
    {
44
        $this->_table_names[] = $table_name;
45
46
        return $this;
47
    }
48
49
    public function resetSet()
50
    {
51
        $this->_write_fields = array();
52
        $this->_write_values = array();
53
        $this->_write_field_types = array();
54
55
        return $this;
56
    }
57
58
    /**
59
     * $type :
60
     *        - 0 = string
61
     *        - 1 = integer
62
     *        - 2 = raw.
63
     */
64
    public function set($field, $value, $type = 0)
65
    {
66
        $this->_write_fields[] = $field;
67
        $this->_write_values[] = $value;
68
        $this->_write_field_types[] = $type;
69
70
        return $this;
71
    }
72
73
    public function resetWhere()
74
    {
75
        $this->_conditions = array();
76
77
        return $this;
78
    }
79
80
    public function where($field, $value = null, $operator = '=')
81
    {
82
        $this->_conditions[] = $this->c($field, $value, $operator);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Adds "<strong><em>or</em></strong>" condition to current and condition
89
     * array.
90
     */
91
    public function whereOr()
92
    {
93
        $this->_conditions[] = '('.implode(' or ', func_get_args()).')';
94
95
        return $this;
96
    }
97
98
    public static function c($field, $value = null, $operator = '=')
99
    {
100
        if (is_null($value)) {
101
            return $field;
102
        } else {
103
            return "{$field} {$operator} {$value}";
104
        }
105
    }
106
107
    public function resetSelect()
108
    {
109
        $this->_read_fields = array();
110
111
        return $this;
112
    }
113
114
    public function select($field)
115
    {
116
        $this->_read_fields[] = $field;
117
118
        return $this;
119
    }
120
121
    public function groupBy($field)
122
    {
123
        $this->_group_fields[] = $field;
124
125
        return $this;
126
    }
127
128
    public function orderBy($field, $asc = true)
129
    {
130
        $this->_order_fields[$field] = array($field, $asc);
131
132
        return $this;
133
    }
134
135
    public function setLimit($limit, $limit_offset = -1)
136
    {
137
        $this->limit = $limit;
138
        if ($limit_offset != -1) {
139
            $this->limit_offset = $limit_offset;
140
        }
141
142
        return $this;
143
    }
144
145
    public function getSelect()
146
    {
147
        if (count($this->_table_names) == 0) {
148
            throw new \Exception('Specify at least 1 table name');
149
        }
150
        $_read_fields = $this->_read_fields;
151
        if (count($_read_fields) == 0) {
152
            $_read_fields[] = '*';
153
        }
154
        $limit = null;
155 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...
156
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
157
        }
158
        $group = null;
159
        if (count($this->_group_fields) > 0) {
160
            $group = ' group by '.implode(', ', $this->_group_fields).' ';
161
        }
162
        $order = null;
163
        if (count($this->_order_fields) > 0) {
164
            $order = ' order by ';
165
            $i = 0;
166
            foreach ($this->_order_fields as $of) {
167
                $order .= ($i > 0 ? ', ' : null);
168
                if (!is_null($of[1])) {
169
                    $pos = strpos($of[0], '.');
170
                    if ($pos !== false) {
171
                        $t = explode('.', $of[0]);
172
                        $t[1] = '`'.$t[1].'`';
173
                        $of[0] = implode('.', $t);
174
                        $order .= $of[0];
175
                    } else {
176
                        $order .= '`'.$of[0].'`';
177
                    }
178
                    $order .= ' '.($of[1] ? 'asc' : 'desc');
179
                } else {
180
                    $order .= $of[0];
181
                }
182
                ++$i;
183
            }
184
        }
185
        $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);
186
187
        return $string;
188
    }
189
190
    public function getUpdate()
191
    {
192
        if (count($this->_table_names) == 0) {
193
            throw new \Exception('Specify at least 1 table name');
194
        }
195
        if (count($this->_write_fields) == 0) {
196
            throw new \Exception('Specify at least 1 write field (set function)');
197
        }
198
        $updates = array();
199
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
200
            $t = $this->_write_fields[$d].'=';
201
            switch ($this->_write_field_types[$d]) {
202
            case 0:
203
              $t .= "'".$this->_write_values[$d]."'";
204
              break;
205
          default:
206
              $t .= $this->_write_values[$d];
207
          }
208
            $updates[] = $t;
209
        }
210
        $update_fields = implode(', ', $updates);
211
        $limit = null;
212 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...
213
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
214
        }
215
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
216
        $string = sprintf('update %1$s set %2$s %3$s %4$s', $this->_table_names[0], $update_fields, $where, $limit);
217
218
        return $string;
219
    }
220
221
    public function getInsert()
222
    {
223
        if (count($this->_table_names) == 0) {
224
            throw new \Exception('Specify at least 1 table name');
225
        }
226
        if (count($this->_write_fields) == 0) {
227
            throw new \Exception('Specify at least 1 write field (set function)');
228
        }
229
        $table = $this->_table_names[0];
230
        $fields = '`'.implode('`, `', $this->_write_fields).'`';
231
        $values = array();
232
        for ($d = 0, $m = count($this->_write_fields); $d < $m; ++$d) {
233
            switch ($this->_write_field_types[$d]) {
234
            case 0:
235
              $values[] = "'".$this->_write_values[$d]."'";
236
              break;
237
            default:
238
              $values[] = $this->_write_values[$d];
239
          }
240
        }
241
        $string = sprintf('insert into %1$s (%2$s) values(%3$s)', $table, $fields, implode(', ', $values));
242
243
        return $string;
244
    }
245
246
    public function getDelete()
247
    {
248
        if (count($this->_table_names) == 0) {
249
            throw new \Exception('Specify at least 1 table name');
250
        }
251
        $table = $this->_table_names[0];
252
        $where = count($this->_conditions) > 0 ? (' where '.implode(' and ', $this->_conditions)) : null;
253
        $limit = null;
254 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...
255
            $limit = ' limit '.($this->limit_offset != -1 ? $this->limit_offset.', ' : null).$this->limit;
256
        }
257
        $string = sprintf('delete from %1$s %2$s %3$s', $table, $where, $limit);
258
259
        return $string;
260
    }
261
}
262