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 declare(strict_types = 1); |
||
| 8 | class Expression |
||
| 9 | { |
||
| 10 | /** @var array */ |
||
| 11 | private $filters; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Expression constructor. |
||
| 15 | */ |
||
| 16 | 24 | public function __construct() |
|
| 20 | |||
| 21 | /** |
||
| 22 | * Adds and filter |
||
| 23 | * |
||
| 24 | * @param array|Expression $expression |
||
| 25 | * |
||
| 26 | * @return $this|Expression |
||
| 27 | */ |
||
| 28 | View Code Duplication | public function and($expression): Expression |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $operator |
||
| 42 | */ |
||
| 43 | 19 | private function prepareFilterIndex(string $operator) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param $expressions |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | 10 | private function mapExpressions($expressions): array |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Returns the filters generated by the expression |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | 24 | public function getExpressionFilters(): array |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Adds or filter |
||
| 77 | * |
||
| 78 | * @param array|Expression $expression |
||
| 79 | * |
||
| 80 | * @return $this|Expression |
||
| 81 | */ |
||
| 82 | 3 | View Code Duplication | public function or ($expression): Expression |
| 93 | |||
| 94 | /** |
||
| 95 | * Adds eq filter |
||
| 96 | * |
||
| 97 | * @param string $field |
||
| 98 | * @param string $value |
||
| 99 | * |
||
| 100 | * @return $this|Expression |
||
| 101 | */ |
||
| 102 | 2 | public function equal(string $field, $value): Expression |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Adds ne filter |
||
| 113 | * |
||
| 114 | * @param string $field |
||
| 115 | * @param string $value |
||
| 116 | * |
||
| 117 | * @return $this|Expression |
||
| 118 | */ |
||
| 119 | 4 | public function notEqual(string $field, $value): Expression |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Adds gt filter |
||
| 130 | * |
||
| 131 | * @param string $field |
||
| 132 | * @param string $value |
||
| 133 | * |
||
| 134 | * @return $this|Expression |
||
| 135 | */ |
||
| 136 | |||
| 137 | public function greaterThan(string $field, $value): Expression |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Adds gte filter |
||
| 148 | * |
||
| 149 | * @param string $field |
||
| 150 | * @param string $value |
||
| 151 | * |
||
| 152 | * @return $this|Expression |
||
| 153 | */ |
||
| 154 | |||
| 155 | public function greaterEqualThan(string $field, $value): Expression |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Adds lt filter |
||
| 166 | * |
||
| 167 | * @param string $field |
||
| 168 | * @param string $value |
||
| 169 | * |
||
| 170 | * @return $this|Expression |
||
| 171 | */ |
||
| 172 | |||
| 173 | public function lowerThan(string $field, $value): Expression |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adds lte filter |
||
| 184 | * |
||
| 185 | * @param string $field |
||
| 186 | * @param string $value |
||
| 187 | * |
||
| 188 | * @return $this|Expression |
||
| 189 | */ |
||
| 190 | |||
| 191 | public function lowerEqualThan(string $field, $value): Expression |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Adds in filter |
||
| 202 | * |
||
| 203 | * @param string $field |
||
| 204 | * @param array $values |
||
| 205 | * |
||
| 206 | * @return $this|Expression |
||
| 207 | */ |
||
| 208 | 2 | public function in(string $field, array $values): Expression |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Adds notIn filter |
||
| 219 | * |
||
| 220 | * @param string $field |
||
| 221 | * @param array $values |
||
| 222 | * |
||
| 223 | * @return $this|Expression |
||
| 224 | */ |
||
| 225 | 2 | public function notIn(string $field, array $values): Expression |
|
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $operation |
||
| 237 | * @param mixed $value |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 10 | private function operationExpression(string $operation, $value): array |
|
| 245 | } |
||
| 246 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.