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 |
||
| 50 | trait JoinsTrait |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Name/id of last join, every ON and ON WHERE call will be associated with this join. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $activeJoin = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set of join tokens with on and on where conditions associated, must be supported by |
||
| 61 | * QueryCompilers. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $joinTokens = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Parameters collected while generating ON WHERE tokens, must be in a same order as parameters |
||
| 69 | * in resulted query. Parameters declared in ON methods will be converted into expressions and |
||
| 70 | * will not be aggregated. |
||
| 71 | * |
||
| 72 | * @see AbstractWhere |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $onParameters = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Register new JOIN with specified type with set of on conditions (linking one table to |
||
| 79 | * another, no parametric on conditions allowed here). |
||
| 80 | * |
||
| 81 | * @param string $type Join type. Allowed values, LEFT, RIGHT, INNER and etc. |
||
| 82 | * @param string $table Joined table name (without prefix), may include AS statement. |
||
| 83 | * @param mixed $on Simplified on definition linking table names (no parameters allowed) or |
||
| 84 | * closure. |
||
| 85 | * @return $this |
||
| 86 | * @throws BuilderException |
||
| 87 | */ |
||
| 88 | public function join($type, $table, $on = null) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Register new INNER JOIN with set of on conditions (linking one table to another, no |
||
| 97 | * parametric on conditions allowed here). |
||
| 98 | * |
||
| 99 | * @link http://www.w3schools.com/sql/sql_join_inner.asp |
||
| 100 | * @see join() |
||
| 101 | * @param string $table Joined table name (without prefix), may include AS statement. |
||
| 102 | * @param mixed $on Simplified on definition linking table names (no parameters allowed) or |
||
| 103 | * closure. |
||
| 104 | * @return $this |
||
| 105 | * @throws BuilderException |
||
| 106 | */ |
||
| 107 | public function innerJoin($table, $on = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Register new RIGHT JOIN with set of on conditions (linking one table to another, no |
||
| 116 | * parametric on conditions allowed here). |
||
| 117 | * |
||
| 118 | * @link http://www.w3schools.com/sql/sql_join_right.asp |
||
| 119 | * @see join() |
||
| 120 | * @param string $table Joined table name (without prefix), may include AS statement. |
||
| 121 | * @param mixed $on Simplified on definition linking table names (no parameters allowed) or |
||
| 122 | * closure. |
||
| 123 | * @return $this |
||
| 124 | * @throws BuilderException |
||
| 125 | */ |
||
| 126 | public function rightJoin($table, $on = null) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Register new LEFT JOIN with set of on conditions (linking one table to another, no parametric |
||
| 135 | * on conditions allowed here). |
||
| 136 | * |
||
| 137 | * @link http://www.w3schools.com/sql/sql_join_left.asp |
||
| 138 | * @see join() |
||
| 139 | * @param string $table Joined table name (without prefix), may include AS statement. |
||
| 140 | * @param mixed $on Simplified on definition linking table names (no parameters allowed) or |
||
| 141 | * closure. |
||
| 142 | * @return $this |
||
| 143 | * @throws BuilderException |
||
| 144 | */ |
||
| 145 | public function leftJoin($table, $on = null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Register new FULL JOIN with set of on conditions (linking one table to another, no parametric |
||
| 154 | * on conditions allowed here). |
||
| 155 | * |
||
| 156 | * @link http://www.w3schools.com/sql/sql_join_full.asp |
||
| 157 | * @see join() |
||
| 158 | * @param string $table Joined table name (without prefix), may include AS statement. |
||
| 159 | * @param mixed $on Simplified on definition linking table names (no parameters allowed) or |
||
| 160 | * closure. |
||
| 161 | * @return $this |
||
| 162 | * @throws BuilderException |
||
| 163 | */ |
||
| 164 | public function fullJoin($table, $on = null) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Simple ON condition with various set of arguments. Can only be used to link column values |
||
| 173 | * together, no parametric values allowed. |
||
| 174 | * |
||
| 175 | * @param mixed $joined Joined column name or expression. |
||
| 176 | * @param mixed $operator Foreign column name, if operator specified. |
||
| 177 | * @param mixed $outer Foreign column name. |
||
| 178 | * @return $this |
||
| 179 | * @throws BuilderException |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function on($joined = null, $operator = null, $outer = null) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Simple AND ON condition with various set of arguments. Can only be used to link column values |
||
| 193 | * together, no parametric values allowed. |
||
| 194 | * |
||
| 195 | * @param mixed $joined Joined column name or expression. |
||
| 196 | * @param mixed $operator Foreign column name, if operator specified. |
||
| 197 | * @param mixed $outer Foreign column name. |
||
| 198 | * @return $this |
||
| 199 | * @throws BuilderException |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function andOn($joined = null, $operator = null, $outer = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Simple OR ON condition with various set of arguments. Can only be used to link column values |
||
| 213 | * together, no parametric values allowed. |
||
| 214 | * |
||
| 215 | * @param mixed $joined Joined column name or expression. |
||
| 216 | * @param mixed $operator Foreign column name, if operator specified. |
||
| 217 | * @param mixed $outer Foreign column name. |
||
| 218 | * @return $this |
||
| 219 | * @throws BuilderException |
||
| 220 | */ |
||
| 221 | View Code Duplication | public function orOn($joined = null, $operator = null, $outer = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Simple ON WHERE condition with various set of arguments. You can use parametric values in |
||
| 232 | * such methods. |
||
| 233 | * |
||
| 234 | * @see AbstractWhere |
||
| 235 | * @param string|mixed $joined Joined column or expression. |
||
| 236 | * @param mixed $variousA Operator or value. |
||
| 237 | * @param mixed $variousB Value, if operator specified. |
||
| 238 | * @param mixed $variousC Required only in between statements. |
||
| 239 | * @return $this |
||
| 240 | * @throws BuilderException |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function onWhere($joined, $variousA = null, $variousB = null, $variousC = null) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Simple AND ON WHERE condition with various set of arguments. You can use parametric values in |
||
| 254 | * such methods. |
||
| 255 | * |
||
| 256 | * @see AbstractWhere |
||
| 257 | * @param string|mixed $joined Joined column or expression. |
||
| 258 | * @param mixed $variousA Operator or value. |
||
| 259 | * @param mixed $variousB Value, if operator specified. |
||
| 260 | * @param mixed $variousC Required only in between statements. |
||
| 261 | * @return $this |
||
| 262 | * @throws BuilderException |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function andOnWhere($joined, $variousA = null, $variousB = null, $variousC = null) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Simple OR ON WHERE condition with various set of arguments. You can use parametric values in |
||
| 276 | * such methods. |
||
| 277 | * |
||
| 278 | * @see AbstractWhere |
||
| 279 | * @param string|mixed $joined Joined column or expression. |
||
| 280 | * @param mixed $variousA Operator or value. |
||
| 281 | * @param mixed $variousB Value, if operator specified. |
||
| 282 | * @param mixed $variousC Required only in between statements. |
||
| 283 | * @return $this |
||
| 284 | * @throws BuilderException |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function orOnWhere($joined, $variousA = null, $variousB = null, $variousC = null) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Convert various amount of where function arguments into valid where token. |
||
| 298 | * |
||
| 299 | * @see AbstractWhere |
||
| 300 | * @param string $joiner Boolean joiner (AND | OR). |
||
| 301 | * @param array $parameters Set of parameters collected from where functions. |
||
| 302 | * @param array $tokens Array to aggregate compiled tokens. Reference. |
||
| 303 | * @param \Closure|null $wrapper Callback or closure used to wrap/collect every potential |
||
| 304 | * parameter. |
||
| 305 | * @throws BuilderException |
||
| 306 | */ |
||
| 307 | abstract protected function whereToken( |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Convert parameters used in JOIN ON statements into sql expressions. |
||
| 316 | * |
||
| 317 | * @return \Closure |
||
| 318 | */ |
||
| 319 | private function onWrapper() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Applied to every potential parameter while ON WHERE tokens generation. |
||
| 332 | * |
||
| 333 | * @return \Closure |
||
| 334 | */ |
||
| 335 | View Code Duplication | private function whereWrapper() |
|
| 362 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.