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:
Complex classes like Prize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Prize, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Prize implements \JsonSerializable |
||
| 17 | { |
||
| 18 | protected $inputFilter; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @ORM\Id |
||
| 22 | * @ORM\Column(type="integer"); |
||
| 23 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 24 | */ |
||
| 25 | protected $id; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @ORM\ManyToOne(targetEntity="Game", inversedBy="prizes") |
||
| 29 | * |
||
| 30 | **/ |
||
| 31 | protected $game; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @ORM\Column(type="string", length=255, nullable=false) |
||
| 35 | */ |
||
| 36 | protected $title; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @ORM\Column(type="string", length=255, nullable=false) |
||
| 40 | */ |
||
| 41 | protected $identifier; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @ORM\ManyToOne(targetEntity="PrizeCategory") |
||
| 45 | * @ORM\JoinColumn(name="prize_category_id", referencedColumnName="id") |
||
| 46 | **/ |
||
| 47 | protected $prizeCategory; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @ORM\Column(name="prize_content",type="text", nullable=true) |
||
| 51 | */ |
||
| 52 | protected $prizeContent; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The points you win when you grab this prize |
||
| 56 | * @ORM\Column(type="integer", nullable=true) |
||
| 57 | */ |
||
| 58 | protected $points = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ORM\Column(type="integer", nullable=false) |
||
| 62 | */ |
||
| 63 | protected $qty = 0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @ORM\Column(name="unit_price", type="float", nullable=false) |
||
| 67 | */ |
||
| 68 | protected $unitPrice = 1; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(type="string", length=10, nullable=true) |
||
| 72 | */ |
||
| 73 | protected $currency; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @ORM\Column(name="created_at", type="datetime") |
||
| 77 | */ |
||
| 78 | protected $createdAt; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @ORM\Column(name="updated_at", type="datetime") |
||
| 82 | */ |
||
| 83 | protected $updatedAt; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 87 | */ |
||
| 88 | protected $picture; |
||
| 89 | |||
| 90 | /** @PrePersist */ |
||
| 91 | public function createChrono() |
||
| 96 | |||
| 97 | /** @PreUpdate */ |
||
| 98 | public function updateChrono() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return the $id |
||
| 105 | */ |
||
| 106 | public function getId() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param field_type $id |
||
| 113 | */ |
||
| 114 | public function setId($id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return the unknown_type |
||
| 123 | */ |
||
| 124 | public function getGame() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param unknown_type $game |
||
| 131 | */ |
||
| 132 | public function setGame($game) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return the $title |
||
| 141 | */ |
||
| 142 | public function getTitle() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param field_type $title |
||
| 149 | */ |
||
| 150 | public function setTitle($title) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return the $identifier |
||
| 159 | */ |
||
| 160 | public function getIdentifier() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param field_type $identifier |
||
| 167 | */ |
||
| 168 | public function setIdentifier($identifier) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return the $prizeCategory |
||
| 177 | */ |
||
| 178 | public function getPrizeCategory() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param unknown $prizeCategory |
||
| 185 | */ |
||
| 186 | public function setPrizeCategory($prizeCategory) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return the $prizeContent |
||
| 195 | */ |
||
| 196 | public function getPrizeContent() |
||
| 200 | |||
| 201 | /** |
||
| 202 | */ |
||
| 203 | public function setPrizeContent($prizeContent) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return integer unknown_type |
||
| 212 | */ |
||
| 213 | public function getPoints() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param unknown_type $points |
||
| 220 | */ |
||
| 221 | public function setPoints($points) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return integer $qty |
||
| 230 | */ |
||
| 231 | public function getQty() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param number $qty |
||
| 238 | */ |
||
| 239 | public function setQty($qty) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return integer $unitPrice |
||
| 248 | */ |
||
| 249 | public function getUnitPrice() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param number $unitPrice |
||
| 256 | */ |
||
| 257 | public function setUnitPrice($unitPrice) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return the $currency |
||
| 266 | */ |
||
| 267 | public function getCurrency() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param field_type $currency |
||
| 274 | */ |
||
| 275 | public function setCurrency($currency) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return \DateTime $created_at |
||
| 284 | */ |
||
| 285 | public function getCreatedAt() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param \DateTime $createdAt |
||
| 292 | */ |
||
| 293 | public function setCreatedAt($createdAt) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return \DateTime $updatedAt |
||
| 302 | */ |
||
| 303 | public function getUpdatedAt() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param \DateTime $updatedAt |
||
| 310 | */ |
||
| 311 | public function setUpdatedAt($updatedAt) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return the $picture |
||
| 320 | */ |
||
| 321 | public function getPicture() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param field_type $picture |
||
| 328 | */ |
||
| 329 | public function setPicture($picture) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Convert the object to an array. |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function getArrayCopy() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Convert the object to json. |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function jsonSerialize() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Populate from an array. |
||
| 360 | * |
||
| 361 | * @param array $data |
||
| 362 | */ |
||
| 363 | public function populate($data = array()) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return InputFilter $inputFilter |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function getInputFilter() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param InputFilterInterface $inputFilter |
||
| 437 | */ |
||
| 438 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
| 442 | } |
||
| 443 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..