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 |
||
80 | abstract class AbstractWhere extends QueryBuilder |
||
81 | { |
||
82 | /** |
||
83 | * Tokens for nested OR and AND conditions. |
||
84 | */ |
||
85 | const TOKEN_AND = "@AND"; |
||
86 | const TOKEN_OR = "@OR"; |
||
87 | |||
88 | /** |
||
89 | * Set of generated where tokens, format must be supported by QueryCompilers. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $whereTokens = []; |
||
94 | |||
95 | /** |
||
96 | * Parameters collected while generating WHERE tokens, must be in a same order as parameters |
||
97 | * in resulted query. |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $whereParameters = []; |
||
102 | |||
103 | /** |
||
104 | * Simple WHERE condition with various set of arguments. |
||
105 | * |
||
106 | * @see AbstractWhere |
||
107 | * @param string|mixed $identifier Column or expression. |
||
108 | * @param mixed $variousA Operator or value. |
||
109 | * @param mixed $variousB Value, if operator specified. |
||
110 | * @param mixed $variousC Required only in between statements. |
||
111 | * @return $this |
||
112 | * @throws BuilderException |
||
113 | */ |
||
114 | public function where($identifier, $variousA = null, $variousB = null, $variousC = null) |
||
120 | |||
121 | /** |
||
122 | * Simple AND WHERE condition with various set of arguments. |
||
123 | * |
||
124 | * @see AbstractWhere |
||
125 | * @param string|mixed $identifier Column or expression. |
||
126 | * @param mixed $variousA Operator or value. |
||
127 | * @param mixed $variousB Value, if operator specified. |
||
128 | * @param mixed $variousC Required only in between statements. |
||
129 | * @return $this |
||
130 | * @throws BuilderException |
||
131 | */ |
||
132 | public function andWhere($identifier, $variousA = null, $variousB = null, $variousC = null) |
||
138 | |||
139 | /** |
||
140 | * Simple OR WHERE condition with various set of arguments. |
||
141 | * |
||
142 | * @see AbstractWhere |
||
143 | * @param string|mixed $identifier Column or expression. |
||
144 | * @param mixed $variousA Operator or value. |
||
145 | * @param mixed $variousB Value, if operator specified. |
||
146 | * @param mixed $variousC Required only in between statements. |
||
147 | * @return $this |
||
148 | * @throws BuilderException |
||
149 | */ |
||
150 | public function orWhere($identifier, $variousA = [], $variousB = null, $variousC = null) |
||
156 | |||
157 | /** |
||
158 | * Convert various amount of where function arguments into valid where token. |
||
159 | * |
||
160 | * @see AbstractWhere |
||
161 | * @param string $joiner Boolean joiner (AND | OR). |
||
162 | * @param array $parameters Set of parameters collected from where functions. |
||
163 | * @param array $tokens Array to aggregate compiled tokens. Reference. |
||
164 | * @param callable $wrapper Callback or closure used to wrap/collect every potential |
||
165 | * parameter. |
||
166 | * @throws BuilderException |
||
167 | */ |
||
168 | protected function whereToken($joiner, array $parameters, &$tokens = [], callable $wrapper) |
||
236 | |||
237 | /** |
||
238 | * Convert simplified where definition into valid set of where tokens. |
||
239 | * |
||
240 | * @see AbstractWhere |
||
241 | * @param string $grouper Grouper type (see self::TOKEN_AND, self::TOKEN_OR). |
||
242 | * @param array $where Simplified where definition. |
||
243 | * @param array $tokens Array to aggregate compiled tokens. Reference. |
||
244 | * @param callable $wrapper Callback or closure used to wrap/collect every potential |
||
245 | * parameter. |
||
246 | * @throws BuilderException |
||
247 | */ |
||
248 | private function arrayWhere($grouper, array $where, &$tokens, callable $wrapper) |
||
293 | |||
294 | /** |
||
295 | * Build set of conditions for specified identifier. |
||
296 | * |
||
297 | * @param string $innerJoiner Inner boolean joiner. |
||
298 | * @param string $key Column identifier. |
||
299 | * @param array $where Operations associated with identifier. |
||
300 | * @param array $tokens Array to aggregate compiled tokens. Reference. |
||
301 | * @param callable $wrapper Callback or closure used to wrap/collect every potential |
||
302 | * parameter. |
||
303 | * @return array |
||
304 | */ |
||
305 | private function builtConditions($innerJoiner, $key, $where, &$tokens, callable $wrapper) |
||
338 | |||
339 | /** |
||
340 | * Applied to every potential parameter while where tokens generation. Used to prepare and |
||
341 | * collect where parameters. |
||
342 | * |
||
343 | * @return \Closure |
||
344 | */ |
||
345 | View Code Duplication | private function whereWrapper() |
|
364 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.