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 |
||
| 25 | class MemoryCard implements InputFilterAwareInterface, \JsonSerializable |
||
| 26 | { |
||
| 27 | protected $inputFilter; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @ORM\Id |
||
| 31 | * @ORM\Column(type="integer"); |
||
| 32 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 33 | */ |
||
| 34 | protected $id; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @ORM\ManyToOne(targetEntity="Memory", inversedBy="cards") |
||
| 38 | * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 39 | */ |
||
| 40 | protected $game; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @Gedmo\Translatable |
||
| 44 | * @ORM\Column(type="string", length=255, nullable=false) |
||
| 45 | */ |
||
| 46 | protected $title; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @Gedmo\Translatable |
||
| 50 | * @ORM\Column(type="text", nullable=true) |
||
| 51 | */ |
||
| 52 | protected $description; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @Gedmo\Translatable |
||
| 56 | * @ORM\Column(name="image", type="string", length=255, nullable=true) |
||
| 57 | */ |
||
| 58 | protected $image; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ORM\Column(name="json_data", type="text", nullable=true) |
||
| 62 | */ |
||
| 63 | protected $jsonData; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @ORM\Column(name="created_at", type="datetime") |
||
| 67 | */ |
||
| 68 | protected $createdAt; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(name="updated_at",type="datetime") |
||
| 72 | */ |
||
| 73 | protected $updatedAt; |
||
| 74 | |||
| 75 | public function __construct() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @PrePersist |
||
| 82 | */ |
||
| 83 | public function createChrono() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @PreUpdate |
||
| 91 | */ |
||
| 92 | public function updateChrono() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Gets the value of id. |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function getId() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets the value of id. |
||
| 109 | * |
||
| 110 | * @param mixed $id the id |
||
| 111 | * |
||
| 112 | * @return self |
||
| 113 | */ |
||
| 114 | public function setId($id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets the value of title. |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getTitle() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Sets the value of title. |
||
| 133 | * |
||
| 134 | * @param mixed $title the title |
||
| 135 | * |
||
| 136 | * @return self |
||
| 137 | */ |
||
| 138 | public function setTitle($title) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Gets the value of description. |
||
| 147 | * |
||
| 148 | * @return mixed |
||
| 149 | */ |
||
| 150 | public function getDescription() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the value of description. |
||
| 157 | * |
||
| 158 | * @param mixed $description the description |
||
| 159 | * |
||
| 160 | * @return self |
||
| 161 | */ |
||
| 162 | public function setDescription($description) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the value of image. |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function getImage() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Sets the value of image. |
||
| 181 | * |
||
| 182 | * @param mixed $image the image |
||
| 183 | * |
||
| 184 | * @return self |
||
| 185 | */ |
||
| 186 | public function setImage($image) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Gets the value of game. |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function getGame() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets the value of game. |
||
| 205 | * |
||
| 206 | * @param mixed $game the game |
||
| 207 | * |
||
| 208 | * @return self |
||
| 209 | */ |
||
| 210 | public function setGame($game) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Gets the value of jsonData. |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | public function getJsonData() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Sets the value of jsonData. |
||
| 229 | * |
||
| 230 | * @param mixed $jsonData the json data |
||
| 231 | * |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | public function setJsonData($jsonData) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Gets the value of createdAt. |
||
| 243 | * |
||
| 244 | * @return mixed |
||
| 245 | */ |
||
| 246 | public function getCreatedAt() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Sets the value of createdAt. |
||
| 253 | * |
||
| 254 | * @param mixed $createdAt the created at |
||
| 255 | * |
||
| 256 | * @return self |
||
| 257 | */ |
||
| 258 | public function setCreatedAt($createdAt) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Gets the value of updatedAt. |
||
| 267 | * |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public function getUpdatedAt() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Sets the value of updatedAt. |
||
| 277 | * |
||
| 278 | * @param mixed $updatedAt the updated at |
||
| 279 | * |
||
| 280 | * @return self |
||
| 281 | */ |
||
| 282 | public function setUpdatedAt($updatedAt) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Convert the object to an array. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getArrayCopy() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Convert the object to json. |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function jsonSerialize() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Populate from an array. |
||
| 322 | * |
||
| 323 | * @param array $data |
||
| 324 | */ |
||
| 325 | public function populate($data = array()) |
||
| 343 | |||
| 344 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
| 348 | |||
| 349 | public function getInputFilter() |
||
| 360 | } |
||
| 361 |
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.