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 | ||
| 19 | abstract class AbstractDocument implements DocumentInterface | ||
| 20 | { | ||
| 21 | /** | ||
| 22 | * @var string | ||
| 23 | */ | ||
| 24 | private $id; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @var string | ||
| 28 | */ | ||
| 29 | private $score; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @var string | ||
| 33 | */ | ||
| 34 | private $parent; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * @var string | ||
| 38 | */ | ||
| 39 | private $ttl; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * @var DocumentHighlight | ||
| 43 | */ | ||
| 44 | private $highlight; | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Legacy property support. | ||
| 48 | * | ||
| 49 | * @param string $property | ||
| 50 | * | ||
| 51 | * @return null|string | ||
| 52 | */ | ||
| 53 | View Code Duplication | public function __get($property) | |
|  | |||
| 54 |     { | ||
| 55 |         switch ($property) { | ||
| 56 | case '_id': | ||
| 57 | return $this->id; | ||
| 58 | case '_score': | ||
| 59 | return $this->score; | ||
| 60 | case '_ttl': | ||
| 61 | return $this->ttl; | ||
| 62 | case '_parent': | ||
| 63 | return $this->parent; | ||
| 64 | default: | ||
| 65 | return null; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Legacy property support and some special properties. | ||
| 71 | * | ||
| 72 | * @param string $property | ||
| 73 | * @param mixed $value | ||
| 74 | */ | ||
| 75 | View Code Duplication | public function __set($property, $value) | |
| 95 | |||
| 96 | /** | ||
| 97 | * When document is cloned id is set to null. | ||
| 98 | */ | ||
| 99 | public function __clone() | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Sets document unique id. | ||
| 106 | * | ||
| 107 | * @param string $documentId | ||
| 108 | * | ||
| 109 | * @return $this | ||
| 110 | */ | ||
| 111 | public function setId($documentId) | ||
| 117 | |||
| 118 | /** | ||
| 119 | * Returns document id. | ||
| 120 | * | ||
| 121 | * @return string | ||
| 122 | */ | ||
| 123 | public function getId() | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Sets document score. | ||
| 130 | * | ||
| 131 | * @param string $documentScore | ||
| 132 | * | ||
| 133 | * @return $this | ||
| 134 | */ | ||
| 135 | public function setScore($documentScore) | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Gets document score. | ||
| 144 | * | ||
| 145 | * @return string | ||
| 146 | */ | ||
| 147 | public function getScore() | ||
| 151 | |||
| 152 | /** | ||
| 153 | * Sets parent document id. | ||
| 154 | * | ||
| 155 | * @param string $parent | ||
| 156 | * | ||
| 157 | * @return $this | ||
| 158 | */ | ||
| 159 | public function setParent($parent) | ||
| 165 | |||
| 166 | /** | ||
| 167 | * Returns parent document id. | ||
| 168 | * | ||
| 169 | * @return null|string | ||
| 170 | */ | ||
| 171 | public function getParent() | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Checks if document has a parent. | ||
| 178 | * | ||
| 179 | * @return bool | ||
| 180 | */ | ||
| 181 | public function hasParent() | ||
| 185 | |||
| 186 | /** | ||
| 187 | * Sets highlight. | ||
| 188 | * | ||
| 189 | * @param DocumentHighlight $highlight | ||
| 190 | */ | ||
| 191 | public function setHighlight(DocumentHighlight $highlight) | ||
| 195 | |||
| 196 | /** | ||
| 197 | * Returns highlight. | ||
| 198 | * | ||
| 199 | * @throws \UnderflowException | ||
| 200 | * | ||
| 201 | * @return DocumentHighlight | ||
| 202 | */ | ||
| 203 | public function getHighLight() | ||
| 211 | |||
| 212 | /** | ||
| 213 | * Sets time to live timestamp. | ||
| 214 | * | ||
| 215 | * @param string $ttl | ||
| 216 | * | ||
| 217 | * @return $this | ||
| 218 | */ | ||
| 219 | public function setTtl($ttl) | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Returns time to live value. | ||
| 228 | * | ||
| 229 | * @return int | ||
| 230 | */ | ||
| 231 | public function getTtl() | ||
| 235 | } | ||
| 236 | 
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.