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 in filter |
||
| 130 | * |
||
| 131 | * @param string $field |
||
| 132 | * @param array $values |
||
| 133 | * |
||
| 134 | * @return $this|Expression |
||
| 135 | */ |
||
| 136 | 2 | public function in(string $field, array $values): Expression |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Adds notIn filter |
||
| 147 | * |
||
| 148 | * @param string $field |
||
| 149 | * @param array $values |
||
| 150 | * |
||
| 151 | * @return $this|Expression |
||
| 152 | */ |
||
| 153 | 2 | public function notIn(string $field, array $values): Expression |
|
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $operation |
||
| 165 | * @param mixed $value |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 10 | private function operationExpression(string $operation, $value): array |
|
| 173 | } |
||
| 174 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.