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 |
||
| 11 | class Parenthesis implements ParenthesisInterface |
||
| 12 | { |
||
| 13 | /** Where conditions get concatenated with space |
||
| 14 | * @var string[] */ |
||
| 15 | protected $where = []; |
||
| 16 | |||
| 17 | /** Callback to close the parenthesis |
||
| 18 | * @var callable */ |
||
| 19 | protected $onClose; |
||
| 20 | |||
| 21 | /** Parent parenthesis or query |
||
| 22 | * @var ParenthesisInterface */ |
||
| 23 | protected $parent; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor |
||
| 27 | * |
||
| 28 | * Create a parenthesis inside another parenthesis or a query. |
||
| 29 | * |
||
| 30 | * @param callable $onClose Callable that gets executed when the parenthesis get closed |
||
| 31 | * @param ParenthesisInterface $parent Parent where createWhereCondition get executed |
||
| 32 | */ |
||
| 33 | 43 | public function __construct( |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Create the where condition |
||
| 43 | * |
||
| 44 | * Calls createWhereCondition() from parent if there is a parent. |
||
| 45 | * |
||
| 46 | * @param string $column Column or expression with placeholders |
||
| 47 | * @param string $operator Operator, value or array of values |
||
| 48 | * @param string $value Value (required when used with operator) |
||
| 49 | * @return string |
||
| 50 | * @internal |
||
| 51 | */ |
||
| 52 | 43 | public function createWhereCondition($column, $operator = null, $value = null) |
|
| 56 | |||
| 57 | /** {@inheritdoc} */ |
||
| 58 | 122 | public function where($column, $operator = null, $value = null) |
|
| 62 | |||
| 63 | /** {@inheritdoc} */ |
||
| 64 | 122 | public function andWhere($column, $operator = null, $value = null) |
|
| 70 | |||
| 71 | /** {@inheritdoc} */ |
||
| 72 | 11 | public function orWhere($column, $operator = null, $value = null) |
|
| 78 | |||
| 79 | /** {@inheritdoc} */ |
||
| 80 | 11 | public function parenthesis() |
|
| 84 | |||
| 85 | /** {@inheritdoc} */ |
||
| 86 | 30 | View Code Duplication | public function andParenthesis() |
| 97 | 30 | ||
| 98 | /** {@inheritdoc} */ |
||
| 99 | View Code Duplication | public function orParenthesis() |
|
| 110 | |||
| 111 | /** {@inheritdoc} */ |
||
| 112 | 9 | public function close() |
|
| 116 | 43 | ||
| 117 | /** {@inheritdoc} */ |
||
| 118 | 43 | public function getExpression() |
|
| 122 | } |
||
| 123 |
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.