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 QuizQuestion 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 QuizQuestion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class QuizQuestion implements InputFilterAwareInterface |
||
| 22 | { |
||
| 23 | protected $inputFilter; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @ORM\Id |
||
| 27 | * @ORM\Column(type="integer"); |
||
| 28 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 29 | */ |
||
| 30 | protected $id; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @ORM\ManyToOne(targetEntity="Quiz", inversedBy="questions", cascade={"persist"}) |
||
| 34 | * @ORM\JoinColumn(name="quiz_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 35 | */ |
||
| 36 | protected $quiz; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @ORM\OneToMany(targetEntity="QuizAnswer", mappedBy="question", cascade={"persist","remove"}) |
||
| 40 | */ |
||
| 41 | private $answers; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * values : |
||
| 45 | * 0 : closed (you can select only one answer) |
||
| 46 | * 1 : opened (you can select many answers), |
||
| 47 | * 2: No answer to select. You write a text in a textarea |
||
| 48 | * |
||
| 49 | * @ORM\Column(type="integer", nullable=false) |
||
| 50 | */ |
||
| 51 | protected $type = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @Gedmo\Translatable |
||
| 55 | * @ORM\Column(type="text", nullable=false) |
||
| 56 | */ |
||
| 57 | protected $question; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\Column(type="integer", nullable=false) |
||
| 61 | */ |
||
| 62 | protected $position = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @Gedmo\Translatable |
||
| 66 | * @ORM\Column(type="string", nullable=true) |
||
| 67 | */ |
||
| 68 | protected $video; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(type="string", nullable=true) |
||
| 72 | */ |
||
| 73 | protected $image; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @Gedmo\Translatable |
||
| 77 | * @ORM\Column(type="integer", nullable=true) |
||
| 78 | */ |
||
| 79 | protected $audio = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @ORM\Column(type="boolean", nullable=true) |
||
| 83 | */ |
||
| 84 | protected $autoplay = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @Gedmo\Translatable |
||
| 88 | * @ORM\Column(type="text", nullable=true) |
||
| 89 | */ |
||
| 90 | protected $hint; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @Gedmo\Translatable |
||
| 94 | * @ORM\Column(name="json_data", type="text", nullable=true) |
||
| 95 | */ |
||
| 96 | protected $jsonData; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @ORM\Column(type="boolean", nullable=true) |
||
| 100 | */ |
||
| 101 | protected $prediction = 0; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @ORM\Column(type="boolean", nullable=true) |
||
| 105 | */ |
||
| 106 | protected $timer = 0; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @ORM\Column(type="integer", nullable=true) |
||
| 110 | */ |
||
| 111 | protected $timer_duration = 0; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The question weight in the game |
||
| 115 | * @ORM\Column(type="integer", nullable=false) |
||
| 116 | */ |
||
| 117 | protected $weight = 1; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @ORM\Column(type="integer", nullable=true) |
||
| 121 | */ |
||
| 122 | protected $max_points = 0; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @ORM\Column(type="integer", nullable=true) |
||
| 126 | */ |
||
| 127 | protected $max_correct_answers = 0; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @ORM\Column(type="datetime") |
||
| 131 | */ |
||
| 132 | protected $created_at; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @ORM\Column(type="datetime") |
||
| 136 | */ |
||
| 137 | protected $updated_at; |
||
| 138 | |||
| 139 | public function __construct() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @PrePersist |
||
| 146 | */ |
||
| 147 | public function createChrono() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @PreUpdate |
||
| 155 | */ |
||
| 156 | public function updateChrono() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return the unknown_type |
||
| 163 | */ |
||
| 164 | public function getId() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param unknown_type $id |
||
| 171 | */ |
||
| 172 | public function setId($id) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return the unknown_type |
||
| 181 | */ |
||
| 182 | public function getQuiz() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param unknown_type $quiz |
||
| 189 | */ |
||
| 190 | public function setQuiz($quiz) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return ArrayCollection unknown_type |
||
| 200 | */ |
||
| 201 | public function getAnswers() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * frm collection solution |
||
| 208 | * @param ArrayCollection $answers |
||
| 209 | */ |
||
| 210 | public function setAnswers(ArrayCollection $answers) |
||
| 216 | |||
| 217 | public function addAnswers(ArrayCollection $answers) |
||
| 224 | |||
| 225 | public function removeAnswers(ArrayCollection $answers) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Add an answer to the quiz. |
||
| 235 | * |
||
| 236 | * @param QuizAnswer $answer |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function addAnswer($answer) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return integer unknown_type |
||
| 247 | */ |
||
| 248 | public function getType() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param unknown_type $type |
||
| 255 | */ |
||
| 256 | public function setType($type) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return the unknown_type |
||
| 265 | */ |
||
| 266 | public function getVideo() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param unknown_type $video |
||
| 273 | */ |
||
| 274 | public function setVideo($video) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return the unknown_type |
||
| 283 | */ |
||
| 284 | public function getImage() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $image |
||
| 291 | */ |
||
| 292 | public function setImage($image) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return integer unknown_type |
||
| 301 | */ |
||
| 302 | public function getAudio() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param unknown_type audio |
||
| 309 | */ |
||
| 310 | public function setAudio($audio) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return boolean unknown_type |
||
| 319 | */ |
||
| 320 | public function getAutoplay() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param unknown_type autoplay |
||
| 327 | */ |
||
| 328 | public function setAutoplay($autoplay) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return the unknown_type |
||
| 337 | */ |
||
| 338 | public function getHint() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param unknown_type $hint |
||
| 345 | */ |
||
| 346 | public function setHint($hint) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return the string |
||
| 355 | */ |
||
| 356 | public function getJsonData() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $hint |
||
| 363 | */ |
||
| 364 | public function setJsonData($jsonData) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return boolean unknown_type |
||
| 373 | */ |
||
| 374 | public function getTimer() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param unknown_type $timer |
||
| 381 | */ |
||
| 382 | public function setTimer($timer) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return integer unknown_type |
||
| 391 | */ |
||
| 392 | public function getPrediction() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param unknown_type $prediction |
||
| 399 | */ |
||
| 400 | public function setPrediction($prediction) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return integer unknown_type |
||
| 409 | */ |
||
| 410 | public function getTimerDuration() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param unknown_type $timer_duration |
||
| 417 | */ |
||
| 418 | public function setTimerDuration($timer_duration) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return integer unknown_type |
||
| 427 | */ |
||
| 428 | public function getWeight() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param unknown_type $weight |
||
| 435 | */ |
||
| 436 | public function setWeight($weight) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return integer |
||
| 445 | */ |
||
| 446 | public function getMaxPoints() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param integer max_points |
||
| 453 | */ |
||
| 454 | public function setMaxPoints($max_points) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return integer |
||
| 463 | */ |
||
| 464 | public function getMaxCorrectAnswers() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param integer max_correct_answers |
||
| 471 | */ |
||
| 472 | public function setMaxCorrectAnswers($max_correct_answers) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return the unknown_type |
||
| 481 | */ |
||
| 482 | public function getCreatedAt() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param unknown_type $created_at |
||
| 489 | */ |
||
| 490 | public function setCreatedAt($created_at) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return the unknown_type |
||
| 499 | */ |
||
| 500 | public function getUpdatedAt() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param unknown_type $updated_at |
||
| 507 | */ |
||
| 508 | public function setUpdatedAt($updated_at) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function getQuestion() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $question |
||
| 525 | */ |
||
| 526 | public function setQuestion($question) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @return integer |
||
| 535 | */ |
||
| 536 | public function getPosition() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param string $position |
||
| 543 | */ |
||
| 544 | public function setPosition($position) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Convert the object to an array. |
||
| 553 | * |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | public function getArrayCopy() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Populate from an array. |
||
| 565 | * |
||
| 566 | * @param array $data |
||
| 567 | */ |
||
| 568 | public function populate($data = array()) |
||
| 626 | |||
| 627 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
| 631 | |||
| 632 | public function getInputFilter() |
||
| 749 | } |
||
| 750 |
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..