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 Quiz 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 Quiz, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Quiz extends Game implements InputFilterAwareInterface |
||
17 | { |
||
18 | const CLASSTYPE = 'quiz'; |
||
19 | |||
20 | /** |
||
21 | * Automatic Draw |
||
22 | * @ORM\Column(name="draw_auto", type="boolean", nullable=false) |
||
23 | */ |
||
24 | protected $drawAuto = 0; |
||
25 | |||
26 | /** |
||
27 | * @ORM\Column(type="integer", nullable=false) |
||
28 | */ |
||
29 | protected $winners = 0; |
||
30 | |||
31 | /** |
||
32 | * @ORM\Column(type="integer", nullable=false) |
||
33 | */ |
||
34 | protected $substitutes = 0; |
||
35 | |||
36 | /** |
||
37 | * display the stats about the quiz (how many entries + distribution of each answer |
||
38 | * ) |
||
39 | * entry : After each entry |
||
40 | * game : At the end of the game |
||
41 | * never : ... |
||
42 | * |
||
43 | * @ORM\Column(name="display_stats", type="string", nullable=false) |
||
44 | */ |
||
45 | protected $displayStats = 'never'; |
||
46 | |||
47 | /** |
||
48 | * display the good answers |
||
49 | * |
||
50 | * question : After the answer to the question |
||
51 | * entry : after each entry |
||
52 | * game : at the end of the game |
||
53 | * never : ... |
||
54 | * |
||
55 | * @ORM\Column(name="display_good_answers", type="string", nullable=false) |
||
56 | */ |
||
57 | protected $displayGoodAnswers = 'never'; |
||
58 | |||
59 | /** |
||
60 | * @ORM\Column(type="boolean", nullable=false) |
||
61 | */ |
||
62 | protected $timer = 0; |
||
63 | |||
64 | /** |
||
65 | * @ORM\Column(name="timer_duration", type="integer", nullable=false) |
||
66 | */ |
||
67 | protected $timerDuration = 0; |
||
68 | |||
69 | /** |
||
70 | * @ORM\Column(name="question_grouping", type="integer", nullable=false) |
||
71 | */ |
||
72 | protected $questionGrouping = 0; |
||
73 | |||
74 | /** |
||
75 | * @ORM\Column(name="victory_conditions", type="integer", nullable=false) |
||
76 | */ |
||
77 | protected $victoryConditions = 0; |
||
78 | |||
79 | /** |
||
80 | * @ORM\Column(name="max_points", type="integer", nullable=true) |
||
81 | */ |
||
82 | protected $maxPoints = 0; |
||
83 | |||
84 | /** |
||
85 | * @ORM\Column(name="max_correct_answers", type="integer", nullable=true) |
||
86 | */ |
||
87 | protected $maxCorrectAnswers = 0; |
||
88 | |||
89 | /** |
||
90 | * @ORM\OneToMany(targetEntity="QuizQuestion", mappedBy="quiz", cascade={"persist","remove"}) |
||
91 | * @ORM\OrderBy({"position" = "ASC"}) |
||
92 | **/ |
||
93 | private $questions; |
||
94 | |||
95 | public function __construct() |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getDisplayStats() |
||
109 | |||
110 | /** |
||
111 | * @param string $displayStats |
||
112 | */ |
||
113 | public function setDisplayStats($displayStats) |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getDisplayGoodAnswers() |
||
127 | |||
128 | /** |
||
129 | * @param string $displayGoodAnswers |
||
130 | */ |
||
131 | public function setDisplayGoodAnswers($displayGoodAnswers) |
||
137 | |||
138 | /** |
||
139 | * @return integer |
||
140 | */ |
||
141 | public function getDrawAuto() |
||
145 | |||
146 | /** |
||
147 | * @param integer $drawAuto |
||
148 | */ |
||
149 | public function setDrawAuto($drawAuto) |
||
155 | |||
156 | /** |
||
157 | * @return integer |
||
158 | */ |
||
159 | public function getWinners() |
||
163 | |||
164 | /** |
||
165 | * @param integer $winners |
||
166 | */ |
||
167 | public function setWinners($winners) |
||
173 | |||
174 | /** |
||
175 | * @return integer |
||
176 | */ |
||
177 | public function getSubstitutes() |
||
181 | |||
182 | /** |
||
183 | * @param integer $substitutes |
||
184 | */ |
||
185 | public function setSubstitutes($substitutes) |
||
191 | |||
192 | /** |
||
193 | * @return integer unknown_type |
||
194 | */ |
||
195 | public function getTimer() |
||
199 | |||
200 | /** |
||
201 | * @param unknown_type $timer |
||
202 | */ |
||
203 | public function setTimer($timer) |
||
209 | |||
210 | /** |
||
211 | * @return integer unknown_type |
||
212 | */ |
||
213 | public function getTimerDuration() |
||
217 | |||
218 | /** |
||
219 | * @param unknown_type $timerDuration |
||
220 | */ |
||
221 | public function setTimerDuration($timerDuration) |
||
227 | |||
228 | public function getVictoryConditions() |
||
232 | |||
233 | /** |
||
234 | */ |
||
235 | public function setVictoryConditions($victoryConditions) |
||
241 | |||
242 | /** |
||
243 | * @return integer unknown_type |
||
244 | */ |
||
245 | public function getQuestionGrouping() |
||
249 | |||
250 | /** |
||
251 | * @param unknown_type $questionGrouping |
||
252 | */ |
||
253 | public function setQuestionGrouping($questionGrouping) |
||
259 | |||
260 | /** |
||
261 | * @param unknown_type $questions |
||
262 | */ |
||
263 | public function setQuestions($questions) |
||
269 | |||
270 | /** |
||
271 | * @return integer |
||
272 | */ |
||
273 | public function getMaxPoints() |
||
277 | |||
278 | /** |
||
279 | * @param integer maxPoints |
||
280 | */ |
||
281 | public function setMaxPoints($maxPoints) |
||
287 | |||
288 | /** |
||
289 | * @return integer |
||
290 | */ |
||
291 | public function getMaxCorrectAnswers() |
||
295 | |||
296 | /** |
||
297 | * @param integer maxCorrectAnswers |
||
298 | */ |
||
299 | public function setMaxCorrectAnswers($maxCorrectAnswers) |
||
305 | |||
306 | /** |
||
307 | * Get question. |
||
308 | * |
||
309 | * @return \Doctrine\Common\Collections\Collection |
||
310 | */ |
||
311 | public function getQuestions() |
||
315 | |||
316 | /** |
||
317 | * Add a question to the quiz. |
||
318 | * |
||
319 | * @param QuizQuestion $question |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | public function addQuestion($question) |
||
327 | |||
328 | /** |
||
329 | * Convert the object to an array. |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | public function getArrayCopy() |
||
340 | |||
341 | /** |
||
342 | * Populate from an array. |
||
343 | * |
||
344 | * @param array $data |
||
345 | */ |
||
346 | public function populate($data = array()) |
||
380 | |||
381 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
385 | |||
386 | public function getInputFilter() |
||
489 | } |
||
490 |
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..