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 WhereConditions |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var 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 | View Code Duplication | public function orWhere($field, $condition = false, $operator = '=') |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Adds a where not condition to the query. |
||
| 54 | * |
||
| 55 | * @param string $field |
||
| 56 | * @param string $condition condition value (optional) |
||
| 57 | * |
||
| 58 | * @return self |
||
| 59 | */ |
||
| 60 | public function not($field, $condition = true) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Adds a where between condition to the query. |
||
| 69 | * |
||
| 70 | * @param string $field |
||
| 71 | * @param mixed $a first between value |
||
| 72 | * @param mixed $b second between value |
||
| 73 | * |
||
| 74 | * @return self |
||
| 75 | */ |
||
| 76 | public function between($field, $a, $b) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Adds a where not between condition to the query. |
||
| 85 | * |
||
| 86 | * @param string $field |
||
| 87 | * @param mixed $a first between value |
||
| 88 | * @param mixed $b second between value |
||
| 89 | * |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | public function notBetween($field, $a, $b) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Adds an exists condition to the query. |
||
| 101 | * |
||
| 102 | * @param callable $f |
||
| 103 | * |
||
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | public function exists(callable $f) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Adds a not exists condition to the query. |
||
| 115 | * |
||
| 116 | * @param callable $f |
||
| 117 | * |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | public function notExists(callable $f) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Gets the where statement for the query. |
||
| 129 | * |
||
| 130 | * @return WhereStatement |
||
| 131 | */ |
||
| 132 | public function getWhere() |
||
| 136 | } |
||
| 137 |
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.