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 Insert extends Query |
||
| 12 | { |
||
| 13 | protected $data = []; |
||
| 14 | protected $duplications; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Set the data to insert. |
||
| 18 | * |
||
| 19 | * @param array $data |
||
| 20 | * @param array|null $prepared |
||
| 21 | * |
||
| 22 | * @return self |
||
| 23 | */ |
||
| 24 | View Code Duplication | public function data(array $data, array &$prepared = null) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Set true to handle duplications. |
||
| 39 | * |
||
| 40 | * @param bool $handle |
||
| 41 | * |
||
| 42 | * @return self |
||
| 43 | */ |
||
| 44 | public function duplications($handle = true) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Run the query and return the id. |
||
| 53 | * |
||
| 54 | * @return int |
||
| 55 | */ |
||
| 56 | public function run() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function __invoke() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function __toString() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Generates the data part of a UPDATE query. |
||
| 110 | * |
||
| 111 | * @param array $fields |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | View Code Duplication | protected static function buildFields(array $fields) |
|
| 125 | } |
||
| 126 |