Passed
Push — master ( 8f3532...76fa7a )
by Ahmet
02:21
created

qb   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 227
Duplicated Lines 3.96 %

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 227
rs 8.2769

18 Methods

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