Completed
Push — master ( 07eb26...46e6ae )
by Jared
02:23
created

DeleteQuery   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 259
Duplicated Lines 10.42 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 19
c 9
b 0
f 1
lcom 1
cbo 6
dl 27
loc 259
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A from() 0 6 1
A where() 10 10 2
A orWhere() 10 10 2
A not() 0 6 1
A between() 0 6 1
A notBetween() 0 6 1
A exists() 0 6 1
A notExists() 0 6 1
A limit() 0 6 1
A orderBy() 0 6 1
A getFrom() 0 4 1
A getWhere() 0 4 1
A getLimit() 0 4 1
A getOrderBy() 0 4 1
A build() 0 14 1
A __clone() 0 7 1

How to fix   Duplicated Code   

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:

1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Query;
12
13
use JAQB\Operations\Executable;
14
use JAQB\Statement\FromStatement;
15
use JAQB\Statement\LimitStatement;
16
use JAQB\Statement\OrderStatement;
17
use JAQB\Statement\WhereStatement;
18
19
class DeleteQuery extends AbstractQuery
20
{
21
    use Executable;
22
23
    /**
24
     * @var FromStatement
25
     */
26
    protected $from;
27
28
    /**
29
     * @var WhereStatement
30
     */
31
    protected $where;
32
33
    /**
34
     * @var OrderStatement
35
     */
36
    protected $orderBy;
37
38
    /**
39
     * @var LimitStatement
40
     */
41
    protected $limit;
42
43 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44
    {
45
        $this->from = new FromStatement();
46
        $this->where = new WhereStatement();
47
        $this->orderBy = new OrderStatement();
48
        $this->limit = new LimitStatement();
49
    }
50
51
    /**
52
     * Sets the table for the query.
53
     *
54
     * @param string $table table name
55
     *
56
     * @return self
57
     */
58
    public function from($table)
59
    {
60
        $this->from->addTable($table);
61
62
        return $this;
63
    }
64
65
    /**
66
     * Sets the where conditions for the query.
67
     *
68
     * @param array|string $field
69
     * @param string|bool  $condition condition value (optional)
70
     * @param string       $operator  operator (optional)
71
     *
72
     * @return self
73
     */
74 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
75
    {
76
        if (func_num_args() >= 2) {
77
            $this->where->addCondition($field, $condition, $operator);
78
        } else {
79
            $this->where->addCondition($field);
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Adds a where or condition to the query.
87
     *
88
     * @param array|string $field
89
     * @param string       $condition condition value (optional)
90
     * @param string       $operator  operator (optional)
91
     *
92
     * @return self
93
     */
94 View Code Duplication
    public function orWhere($field, $condition = false, $operator = '=')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
95
    {
96
        if (func_num_args() >= 2) {
97
            $this->where->addConditionOr($field, $condition, $operator);
98
        } else {
99
            $this->where->addConditionOr($field);
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * Adds a where not condition to the query.
107
     *
108
     * @param string $field
109
     * @param string $condition condition value (optional)
110
     *
111
     * @return self
112
     */
113
    public function not($field, $condition = true)
114
    {
115
        $this->where->addCondition($field, $condition, '<>');
116
117
        return $this;
118
    }
119
120
    /**
121
     * Adds a where between condition to the query.
122
     *
123
     * @param string $field
124
     * @param mixed  $a     first between value
125
     * @param mixed  $b     second between value
126
     *
127
     * @return self
128
     */
129
    public function between($field, $a, $b)
130
    {
131
        $this->where->addBetweenCondition($field, $a, $b);
132
133
        return $this;
134
    }
135
136
    /**
137
     * Adds a where not between condition to the query.
138
     *
139
     * @param string $field
140
     * @param mixed  $a     first between value
141
     * @param mixed  $b     second between value
142
     *
143
     * @return self
144
     */
145
    public function notBetween($field, $a, $b)
146
    {
147
        $this->where->addNotBetweenCondition($field, $a, $b);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Adds an exists condition to the query.
154
     *
155
     * @param callable $f
156
     *
157
     * @return self
158
     */
159
    public function exists(callable $f)
160
    {
161
        $this->where->addExistsCondition($f);
162
163
        return $this;
164
    }
165
166
    /**
167
     * Adds a not exists condition to the query.
168
     *
169
     * @param callable $f
170
     *
171
     * @return self
172
     */
173
    public function notExists(callable $f)
174
    {
175
        $this->where->addNotExistsCondition($f);
176
177
        return $this;
178
    }
179
180
    /**
181
     * Sets the limit for the query.
182
     *
183
     * @param int $limit
184
     * @param int $offset
185
     *
186
     * @return self
187
     */
188
    public function limit($limit, $offset = 0)
189
    {
190
        $this->limit->setLimit($limit, $offset);
191
192
        return $this;
193
    }
194
195
    /**
196
     * Sets the order for the query.
197
     *
198
     * @param string|array $fields
199
     * @param string       $direction
200
     *
201
     * @return self
202
     */
203
    public function orderBy($fields, $direction = false)
204
    {
205
        $this->orderBy->addFields($fields, $direction);
206
207
        return $this;
208
    }
209
210
    /**
211
     * Gets the from statement for the query.
212
     *
213
     * @return FromStatement
214
     */
215
    public function getFrom()
216
    {
217
        return $this->from;
218
    }
219
220
    /**
221
     * Gets the where statement for the query.
222
     *
223
     * @return WhereStatement
224
     */
225
    public function getWhere()
226
    {
227
        return $this->where;
228
    }
229
230
    /**
231
     * Gets the limit statement for the query.
232
     *
233
     * @return LimitStatement
234
     */
235
    public function getLimit()
236
    {
237
        return $this->limit;
238
    }
239
240
    /**
241
     * Gets the order by statement for the query.
242
     *
243
     * @return OrderByStatement
244
     */
245
    public function getOrderBy()
246
    {
247
        return $this->orderBy;
248
    }
249
250
    /**
251
     * Generates the raw SQL string for the query.
252
     *
253
     * @return string
254
     */
255
    public function build()
256
    {
257
        $sql = [
258
            'DELETE',
259
            $this->from->build(),
260
            $this->where->build(),
261
            $this->orderBy->build(),
262
            $this->limit->build(),
263
        ];
264
265
        $this->values = $this->where->getValues();
266
267
        return implode(' ', array_filter($sql));
268
    }
269
270
    public function __clone()
271
    {
272
        $this->from = clone $this->from;
273
        $this->where = clone $this->where;
274
        $this->orderBy = clone $this->orderBy;
275
        $this->limit = clone $this->limit;
276
    }
277
}
278