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 QuizAnswer 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 QuizAnswer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class QuizAnswer implements InputFilterAwareInterface |
||
18 | { |
||
19 | protected $inputFilter; |
||
20 | |||
21 | /** |
||
22 | * @ORM\Id |
||
23 | * @ORM\Column(type="integer"); |
||
24 | * @ORM\GeneratedValue(strategy="AUTO") |
||
25 | */ |
||
26 | protected $id; |
||
27 | |||
28 | /** |
||
29 | * @ORM\ManyToOne(targetEntity="QuizQuestion", inversedBy="answers") |
||
30 | * |
||
31 | **/ |
||
32 | protected $question; |
||
33 | |||
34 | /** |
||
35 | * @ORM\Column(type="text", nullable=true) |
||
36 | */ |
||
37 | protected $answer; |
||
38 | |||
39 | /** |
||
40 | * Explanation of the answer |
||
41 | * @ORM\Column(type="text", nullable=true) |
||
42 | */ |
||
43 | protected $explanation; |
||
44 | |||
45 | /** |
||
46 | * @ORM\Column(name="json_data", type="text", nullable=true) |
||
47 | */ |
||
48 | protected $jsonData; |
||
49 | |||
50 | /** |
||
51 | * @ORM\Column(type="string", nullable=true) |
||
52 | */ |
||
53 | protected $video; |
||
54 | |||
55 | /** |
||
56 | * @ORM\Column(type="string", nullable=true) |
||
57 | */ |
||
58 | protected $image; |
||
59 | |||
60 | /** |
||
61 | * The answer score in the game |
||
62 | * @ORM\Column(type="integer", nullable=true) |
||
63 | */ |
||
64 | protected $points = 0; |
||
65 | |||
66 | /** |
||
67 | * @ORM\Column(type="integer", nullable=false) |
||
68 | */ |
||
69 | protected $position = 0; |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @ORM\Column(type="boolean", nullable=true) |
||
74 | */ |
||
75 | protected $correct = 0; |
||
76 | |||
77 | /** |
||
78 | * @ORM\Column(type="datetime") |
||
79 | */ |
||
80 | protected $created_at; |
||
81 | |||
82 | /** |
||
83 | * @ORM\Column(type="datetime") |
||
84 | */ |
||
85 | protected $updated_at; |
||
86 | |||
87 | /** @PrePersist */ |
||
88 | public function createChrono() |
||
93 | |||
94 | /** @PreUpdate */ |
||
95 | public function updateChrono() |
||
99 | |||
100 | /** |
||
101 | * @return the unknown_type |
||
102 | */ |
||
103 | public function getId() |
||
107 | |||
108 | /** |
||
109 | * @param unknown_type $id |
||
110 | */ |
||
111 | public function setId($id) |
||
117 | |||
118 | /** |
||
119 | * @return the unknown_type |
||
120 | */ |
||
121 | public function getQuestion() |
||
125 | |||
126 | /** |
||
127 | * @param unknown_type $question |
||
128 | */ |
||
129 | public function setQuestion($question) |
||
135 | |||
136 | /** |
||
137 | * @return the unknown_type |
||
138 | */ |
||
139 | public function getAnswer() |
||
143 | |||
144 | /** |
||
145 | * @param unknown_type $answer |
||
146 | */ |
||
147 | public function setAnswer($answer) |
||
153 | |||
154 | /** |
||
155 | * @return the unknown_type |
||
156 | */ |
||
157 | public function getExplanation() |
||
161 | |||
162 | /** |
||
163 | * @param unknown_type $explanation |
||
164 | */ |
||
165 | public function setExplanation($explanation) |
||
171 | |||
172 | /** |
||
173 | * @return the string |
||
174 | */ |
||
175 | public function getJsonData() |
||
179 | |||
180 | /** |
||
181 | * @param string $hint |
||
|
|||
182 | */ |
||
183 | public function setJsonData($jsonData) |
||
189 | |||
190 | /** |
||
191 | * @return the unknown_type |
||
192 | */ |
||
193 | public function getVideo() |
||
197 | |||
198 | /** |
||
199 | * @param unknown_type $video |
||
200 | */ |
||
201 | public function setVideo($video) |
||
207 | |||
208 | /** |
||
209 | * @return the unknown_type |
||
210 | */ |
||
211 | public function getImage() |
||
215 | |||
216 | /** |
||
217 | * @param unknown_type $image |
||
218 | */ |
||
219 | public function setImage($image) |
||
225 | |||
226 | /** |
||
227 | * @return integer unknown_type |
||
228 | */ |
||
229 | public function getPoints() |
||
233 | |||
234 | /** |
||
235 | * @param unknown_type $points |
||
236 | */ |
||
237 | public function setPoints($points) |
||
243 | |||
244 | /** |
||
245 | * @return integer unknown_type |
||
246 | */ |
||
247 | public function getPosition() |
||
251 | |||
252 | /** |
||
253 | * @param unknown_type $position |
||
254 | */ |
||
255 | public function setPosition($position) |
||
261 | |||
262 | /** |
||
263 | * @return integer unknown_type |
||
264 | */ |
||
265 | public function getCorrect() |
||
269 | |||
270 | /** |
||
271 | * @param unknown_type $correct |
||
272 | */ |
||
273 | public function setCorrect($correct) |
||
279 | |||
280 | /** |
||
281 | * @return the unknown_type |
||
282 | */ |
||
283 | public function getCreatedAt() |
||
287 | |||
288 | /** |
||
289 | * @param unknown_type $created_at |
||
290 | */ |
||
291 | public function setCreatedAt($created_at) |
||
297 | |||
298 | /** |
||
299 | * @return the unknown_type |
||
300 | */ |
||
301 | public function getUpdatedAt() |
||
305 | |||
306 | /** |
||
307 | * @param unknown_type $updated_at |
||
308 | */ |
||
309 | public function setUpdatedAt($updated_at) |
||
315 | |||
316 | /** |
||
317 | * Convert the object to an array. |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | public function getArrayCopy() |
||
327 | |||
328 | /** |
||
329 | * Populate from an array. |
||
330 | * |
||
331 | * @param array $data |
||
332 | */ |
||
333 | public function populate($data = array()) |
||
371 | |||
372 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
376 | |||
377 | public function getInputFilter() |
||
441 | } |
||
442 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.