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 |
||
19 | class QuizQuestion implements InputFilterAwareInterface |
||
20 | { |
||
21 | protected $inputFilter; |
||
22 | |||
23 | /** |
||
24 | * @ORM\Id |
||
25 | * @ORM\Column(type="integer"); |
||
26 | * @ORM\GeneratedValue(strategy="AUTO") |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * @ORM\ManyToOne(targetEntity="Quiz", inversedBy="questions") |
||
32 | */ |
||
33 | protected $quiz; |
||
34 | |||
35 | /** |
||
36 | * @ORM\OneToMany(targetEntity="QuizAnswer", mappedBy="question", cascade={"persist","remove"}) |
||
37 | */ |
||
38 | private $answers; |
||
39 | |||
40 | /** |
||
41 | * values : |
||
42 | * 0 : closed (you can select only one answer) |
||
43 | * 1 : opened (you can select many answers), |
||
44 | * 2: No answer to select. You write a text in a textarea |
||
45 | * |
||
46 | * @ORM\Column(type="integer", nullable=false) |
||
47 | */ |
||
48 | protected $type = 0; |
||
49 | |||
50 | /** |
||
51 | * @ORM\Column(type="text", nullable=false) |
||
52 | */ |
||
53 | protected $question; |
||
54 | |||
55 | /** |
||
56 | * @ORM\Column(type="integer", nullable=false) |
||
57 | */ |
||
58 | protected $position = 0; |
||
59 | |||
60 | /** |
||
61 | * @ORM\Column(type="string", nullable=true) |
||
62 | */ |
||
63 | protected $video; |
||
64 | |||
65 | /** |
||
66 | * @ORM\Column(type="string", nullable=true) |
||
67 | */ |
||
68 | protected $image; |
||
69 | |||
70 | /** |
||
71 | * @ORM\Column(type="integer", nullable=true) |
||
72 | */ |
||
73 | protected $audio = 0; |
||
74 | |||
75 | /** |
||
76 | * @ORM\Column(type="boolean", nullable=true) |
||
77 | */ |
||
78 | protected $autoplay = 0; |
||
79 | |||
80 | /** |
||
81 | * @ORM\Column(type="text", nullable=true) |
||
82 | */ |
||
83 | protected $hint; |
||
84 | |||
85 | /** |
||
86 | * @ORM\Column(name="json_data", type="text", nullable=true) |
||
87 | */ |
||
88 | protected $jsonData; |
||
89 | |||
90 | /** |
||
91 | * @ORM\Column(type="boolean", nullable=true) |
||
92 | */ |
||
93 | protected $prediction = 0; |
||
94 | |||
95 | /** |
||
96 | * @ORM\Column(type="boolean", nullable=true) |
||
97 | */ |
||
98 | protected $timer = 0; |
||
99 | |||
100 | /** |
||
101 | * @ORM\Column(type="integer", nullable=true) |
||
102 | */ |
||
103 | protected $timer_duration = 0; |
||
104 | |||
105 | /** |
||
106 | * The question weight in the game |
||
107 | * @ORM\Column(type="integer", nullable=false) |
||
108 | */ |
||
109 | protected $weight = 1; |
||
110 | |||
111 | /** |
||
112 | * @ORM\Column(type="integer", nullable=true) |
||
113 | */ |
||
114 | protected $max_points = 0; |
||
115 | |||
116 | /** |
||
117 | * @ORM\Column(type="integer", nullable=true) |
||
118 | */ |
||
119 | protected $max_correct_answers = 0; |
||
120 | |||
121 | /** |
||
122 | * @ORM\Column(type="datetime") |
||
123 | */ |
||
124 | protected $created_at; |
||
125 | |||
126 | /** |
||
127 | * @ORM\Column(type="datetime") |
||
128 | */ |
||
129 | protected $updated_at; |
||
130 | |||
131 | public function __construct() |
||
135 | |||
136 | /** |
||
137 | * @PrePersist |
||
138 | */ |
||
139 | public function createChrono() |
||
144 | |||
145 | /** |
||
146 | * @PreUpdate |
||
147 | */ |
||
148 | public function updateChrono() |
||
152 | |||
153 | /** |
||
154 | * @return the unknown_type |
||
155 | */ |
||
156 | public function getId() |
||
160 | |||
161 | /** |
||
162 | * @param unknown_type $id |
||
163 | */ |
||
164 | public function setId($id) |
||
170 | |||
171 | /** |
||
172 | * @return the unknown_type |
||
173 | */ |
||
174 | public function getQuiz() |
||
178 | |||
179 | /** |
||
180 | * @param unknown_type $quiz |
||
181 | */ |
||
182 | public function setQuiz($quiz) |
||
189 | |||
190 | /** |
||
191 | * @return ArrayCollection unknown_type |
||
192 | */ |
||
193 | public function getAnswers() |
||
197 | |||
198 | /** |
||
199 | * frm collection solution |
||
200 | * @param ArrayCollection $answers |
||
201 | */ |
||
202 | public function setAnswers(ArrayCollection $answers) |
||
208 | |||
209 | public function addAnswers(ArrayCollection $answers) |
||
216 | |||
217 | public function removeAnswers(ArrayCollection $answers) |
||
224 | |||
225 | /** |
||
226 | * Add an answer to the quiz. |
||
227 | * |
||
228 | * @param QuizAnswer $answer |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | public function addAnswer($answer) |
||
236 | |||
237 | /** |
||
238 | * @return integer unknown_type |
||
239 | */ |
||
240 | public function getType() |
||
244 | |||
245 | /** |
||
246 | * @param unknown_type $type |
||
247 | */ |
||
248 | public function setType($type) |
||
254 | |||
255 | /** |
||
256 | * @return the unknown_type |
||
257 | */ |
||
258 | public function getVideo() |
||
262 | |||
263 | /** |
||
264 | * @param unknown_type $video |
||
265 | */ |
||
266 | public function setVideo($video) |
||
272 | |||
273 | /** |
||
274 | * @return the unknown_type |
||
275 | */ |
||
276 | public function getImage() |
||
280 | |||
281 | /** |
||
282 | * @param string $image |
||
283 | */ |
||
284 | public function setImage($image) |
||
290 | |||
291 | /** |
||
292 | * @return integer unknown_type |
||
293 | */ |
||
294 | public function getAudio() |
||
298 | |||
299 | /** |
||
300 | * @param unknown_type audio |
||
301 | */ |
||
302 | public function setAudio($audio) |
||
308 | |||
309 | /** |
||
310 | * @return boolean unknown_type |
||
311 | */ |
||
312 | public function getAutoplay() |
||
316 | |||
317 | /** |
||
318 | * @param unknown_type autoplay |
||
319 | */ |
||
320 | public function setAutoplay($autoplay) |
||
326 | |||
327 | /** |
||
328 | * @return the unknown_type |
||
329 | */ |
||
330 | public function getHint() |
||
334 | |||
335 | /** |
||
336 | * @param unknown_type $hint |
||
337 | */ |
||
338 | public function setHint($hint) |
||
344 | |||
345 | /** |
||
346 | * @return the string |
||
347 | */ |
||
348 | public function getJsonData() |
||
352 | |||
353 | /** |
||
354 | * @param string $hint |
||
355 | */ |
||
356 | public function setJsonData($jsonData) |
||
362 | |||
363 | /** |
||
364 | * @return boolean unknown_type |
||
365 | */ |
||
366 | public function getTimer() |
||
370 | |||
371 | /** |
||
372 | * @param unknown_type $timer |
||
373 | */ |
||
374 | public function setTimer($timer) |
||
380 | |||
381 | /** |
||
382 | * @return integer unknown_type |
||
383 | */ |
||
384 | public function getPrediction() |
||
388 | |||
389 | /** |
||
390 | * @param unknown_type $prediction |
||
391 | */ |
||
392 | public function setPrediction($prediction) |
||
398 | |||
399 | /** |
||
400 | * @return integer unknown_type |
||
401 | */ |
||
402 | public function getTimerDuration() |
||
406 | |||
407 | /** |
||
408 | * @param unknown_type $timer_duration |
||
409 | */ |
||
410 | public function setTimerDuration($timer_duration) |
||
416 | |||
417 | /** |
||
418 | * @return integer unknown_type |
||
419 | */ |
||
420 | public function getWeight() |
||
424 | |||
425 | /** |
||
426 | * @param unknown_type $weight |
||
427 | */ |
||
428 | public function setWeight($weight) |
||
434 | |||
435 | /** |
||
436 | * @return integer |
||
437 | */ |
||
438 | public function getMaxPoints() |
||
442 | |||
443 | /** |
||
444 | * @param integer max_points |
||
445 | */ |
||
446 | public function setMaxPoints($max_points) |
||
452 | |||
453 | /** |
||
454 | * @return integer |
||
455 | */ |
||
456 | public function getMaxCorrectAnswers() |
||
460 | |||
461 | /** |
||
462 | * @param integer max_correct_answers |
||
463 | */ |
||
464 | public function setMaxCorrectAnswers($max_correct_answers) |
||
470 | |||
471 | /** |
||
472 | * @return the unknown_type |
||
473 | */ |
||
474 | public function getCreatedAt() |
||
478 | |||
479 | /** |
||
480 | * @param unknown_type $created_at |
||
481 | */ |
||
482 | public function setCreatedAt($created_at) |
||
488 | |||
489 | /** |
||
490 | * @return the unknown_type |
||
491 | */ |
||
492 | public function getUpdatedAt() |
||
496 | |||
497 | /** |
||
498 | * @param unknown_type $updated_at |
||
499 | */ |
||
500 | public function setUpdatedAt($updated_at) |
||
506 | |||
507 | /** |
||
508 | * @return string |
||
509 | */ |
||
510 | public function getQuestion() |
||
514 | |||
515 | /** |
||
516 | * @param string $question |
||
517 | */ |
||
518 | public function setQuestion($question) |
||
524 | |||
525 | /** |
||
526 | * @return integer |
||
527 | */ |
||
528 | public function getPosition() |
||
532 | |||
533 | /** |
||
534 | * @param string $position |
||
535 | */ |
||
536 | public function setPosition($position) |
||
542 | |||
543 | /** |
||
544 | * Convert the object to an array. |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | public function getArrayCopy() |
||
554 | |||
555 | /** |
||
556 | * Populate from an array. |
||
557 | * |
||
558 | * @param array $data |
||
559 | */ |
||
560 | public function populate($data = array()) |
||
618 | |||
619 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
623 | |||
624 | public function getInputFilter() |
||
741 | } |
||
742 |
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..