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 |
||
| 12 | class MatchPhrase extends AbstractQuery |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @param string $field |
||
| 16 | * @param mixed $values |
||
| 17 | */ |
||
| 18 | public function __construct($field = null, $values = null) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Sets a param for the message array. |
||
| 27 | * |
||
| 28 | * @param string $field |
||
| 29 | * @param mixed $values |
||
| 30 | * |
||
| 31 | * @return $this |
||
| 32 | */ |
||
| 33 | public function setField($field, $values) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Sets a param for the given field. |
||
| 40 | * |
||
| 41 | * @param string $field |
||
| 42 | * @param string $key |
||
| 43 | * @param string $value |
||
| 44 | * |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function setFieldParam($field, $key, $value) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Sets the query string. |
||
| 60 | * |
||
| 61 | * @param string $field |
||
| 62 | * @param string $query |
||
| 63 | * |
||
| 64 | * @return $this |
||
| 65 | */ |
||
| 66 | public function setFieldQuery($field, $query) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set field analyzer. |
||
| 73 | * |
||
| 74 | * @param string $field |
||
| 75 | * @param string $analyzer |
||
| 76 | * |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | public function setFieldAnalyzer($field, $analyzer) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set field boost value. |
||
| 86 | * |
||
| 87 | * If not set, defaults to 1.0. |
||
| 88 | * |
||
| 89 | * @param string $field |
||
| 90 | * @param float $boost |
||
| 91 | * |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function setFieldBoost($field, $boost = 1.0) |
||
| 98 | } |
||
| 99 |
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.