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 |
||
5 | trait Where |
||
6 | { |
||
7 | /** |
||
8 | * @var \JAQB\Statement\WhereStatement |
||
9 | */ |
||
10 | protected $where; |
||
11 | |||
12 | /** |
||
13 | * Sets the where conditions for the query. |
||
14 | * |
||
15 | * @param array|string $field |
||
16 | * @param string|bool $condition condition value (optional) |
||
17 | * @param string $operator operator (optional) |
||
18 | * |
||
19 | * @return self |
||
20 | */ |
||
21 | View Code Duplication | public function where($field, $condition = false, $operator = '=') |
|
31 | |||
32 | /** |
||
33 | * Adds a where or condition to the query. |
||
34 | * |
||
35 | * @param array|string $field |
||
36 | * @param string $condition condition value (optional) |
||
37 | * @param string $operator operator (optional) |
||
38 | * |
||
39 | * @return self |
||
40 | */ |
||
41 | public function orWhere($field, $condition = false, $operator = '=') |
||
51 | |||
52 | /** |
||
53 | * Sets the where conditions for the query with infix style arguments. |
||
54 | * |
||
55 | * @param array|string $field |
||
56 | * @param string $operator operator (optional) |
||
57 | * @param string|bool $condition condition value (optional) |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | View Code Duplication | public function whereInfix($field, $operator = '=', $condition = false) |
|
74 | |||
75 | /** |
||
76 | * Adds a where or condition to the query with infix style arguments. |
||
77 | * |
||
78 | * @param array|string $field |
||
79 | * @param string $operator operator (optional) |
||
80 | * @param string $condition condition value (optional) |
||
81 | * |
||
82 | * @return self |
||
83 | */ |
||
84 | View Code Duplication | public function orWhereInfix($field, $operator = '=', $condition = false) |
|
97 | |||
98 | /** |
||
99 | * Adds a where not condition to the query. |
||
100 | * |
||
101 | * @param string $field |
||
102 | * @param string $condition condition value (optional) |
||
103 | * |
||
104 | * @return self |
||
105 | */ |
||
106 | public function not($field, $condition = true) |
||
112 | |||
113 | /** |
||
114 | * Adds a where between condition to the query. |
||
115 | * |
||
116 | * @param string $field |
||
117 | * @param mixed $a first between value |
||
118 | * @param mixed $b second between value |
||
119 | * |
||
120 | * @return self |
||
121 | */ |
||
122 | public function between($field, $a, $b) |
||
128 | |||
129 | /** |
||
130 | * Adds a where not between condition to the query. |
||
131 | * |
||
132 | * @param string $field |
||
133 | * @param mixed $a first between value |
||
134 | * @param mixed $b second between value |
||
135 | * |
||
136 | * @return self |
||
137 | */ |
||
138 | public function notBetween($field, $a, $b) |
||
144 | |||
145 | /** |
||
146 | * Adds an exists condition to the query. |
||
147 | * |
||
148 | * @param callable $f |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | public function exists(callable $f) |
||
158 | |||
159 | /** |
||
160 | * Adds a not exists condition to the query. |
||
161 | * |
||
162 | * @param callable $f |
||
163 | * |
||
164 | * @return self |
||
165 | */ |
||
166 | public function notExists(callable $f) |
||
172 | |||
173 | /** |
||
174 | * Gets the where statement for the query. |
||
175 | * |
||
176 | * @return \JAQB\Statement\WhereStatement |
||
177 | */ |
||
178 | public function getWhere() |
||
182 | } |
||
183 |