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 |
||
19 | class QuizAnswer 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="QuizQuestion", inversedBy="answers") |
||
32 | * |
||
33 | **/ |
||
34 | protected $question; |
||
35 | |||
36 | /** |
||
37 | * @Gedmo\Translatable |
||
38 | * @ORM\Column(type="text", nullable=true) |
||
39 | */ |
||
40 | protected $answer; |
||
41 | |||
42 | /** |
||
43 | * Explanation of the answer |
||
44 | * @Gedmo\Translatable |
||
45 | * @ORM\Column(type="text", nullable=true) |
||
46 | */ |
||
47 | protected $explanation; |
||
48 | |||
49 | /** |
||
50 | * @Gedmo\Translatable |
||
51 | * @ORM\Column(name="json_data", type="text", nullable=true) |
||
52 | */ |
||
53 | protected $jsonData; |
||
54 | |||
55 | /** |
||
56 | * @Gedmo\Translatable |
||
57 | * @ORM\Column(type="string", nullable=true) |
||
58 | */ |
||
59 | protected $video; |
||
60 | |||
61 | /** |
||
62 | * @Gedmo\Translatable |
||
63 | * @ORM\Column(type="string", nullable=true) |
||
64 | */ |
||
65 | protected $image; |
||
66 | |||
67 | /** |
||
68 | * The answer score in the game |
||
69 | * @ORM\Column(type="integer", nullable=true) |
||
70 | */ |
||
71 | protected $points = 0; |
||
72 | |||
73 | /** |
||
74 | * @ORM\Column(type="integer", nullable=false) |
||
75 | */ |
||
76 | protected $position = 0; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @ORM\Column(type="boolean", nullable=true) |
||
81 | */ |
||
82 | protected $correct = 0; |
||
83 | |||
84 | /** |
||
85 | * @ORM\Column(type="datetime") |
||
86 | */ |
||
87 | protected $created_at; |
||
88 | |||
89 | /** |
||
90 | * @ORM\Column(type="datetime") |
||
91 | */ |
||
92 | protected $updated_at; |
||
93 | |||
94 | /** @PrePersist */ |
||
95 | public function createChrono() |
||
100 | |||
101 | /** @PreUpdate */ |
||
102 | public function updateChrono() |
||
106 | |||
107 | /** |
||
108 | * @return the unknown_type |
||
109 | */ |
||
110 | public function getId() |
||
114 | |||
115 | /** |
||
116 | * @param unknown_type $id |
||
117 | */ |
||
118 | public function setId($id) |
||
124 | |||
125 | /** |
||
126 | * @return the unknown_type |
||
127 | */ |
||
128 | public function getQuestion() |
||
132 | |||
133 | /** |
||
134 | * @param unknown_type $question |
||
135 | */ |
||
136 | public function setQuestion($question) |
||
142 | |||
143 | /** |
||
144 | * @return the unknown_type |
||
145 | */ |
||
146 | public function getAnswer() |
||
150 | |||
151 | /** |
||
152 | * @param unknown_type $answer |
||
153 | */ |
||
154 | public function setAnswer($answer) |
||
160 | |||
161 | /** |
||
162 | * @return the unknown_type |
||
163 | */ |
||
164 | public function getExplanation() |
||
168 | |||
169 | /** |
||
170 | * @param unknown_type $explanation |
||
171 | */ |
||
172 | public function setExplanation($explanation) |
||
178 | |||
179 | /** |
||
180 | * @return the string |
||
181 | */ |
||
182 | public function getJsonData() |
||
186 | |||
187 | /** |
||
188 | * @param string $hint |
||
|
|||
189 | */ |
||
190 | public function setJsonData($jsonData) |
||
196 | |||
197 | /** |
||
198 | * @return the unknown_type |
||
199 | */ |
||
200 | public function getVideo() |
||
204 | |||
205 | /** |
||
206 | * @param unknown_type $video |
||
207 | */ |
||
208 | public function setVideo($video) |
||
214 | |||
215 | /** |
||
216 | * @return the unknown_type |
||
217 | */ |
||
218 | public function getImage() |
||
222 | |||
223 | /** |
||
224 | * @param unknown_type $image |
||
225 | */ |
||
226 | public function setImage($image) |
||
232 | |||
233 | /** |
||
234 | * @return integer unknown_type |
||
235 | */ |
||
236 | public function getPoints() |
||
240 | |||
241 | /** |
||
242 | * @param unknown_type $points |
||
243 | */ |
||
244 | public function setPoints($points) |
||
250 | |||
251 | /** |
||
252 | * @return integer unknown_type |
||
253 | */ |
||
254 | public function getPosition() |
||
258 | |||
259 | /** |
||
260 | * @param unknown_type $position |
||
261 | */ |
||
262 | public function setPosition($position) |
||
268 | |||
269 | /** |
||
270 | * @return integer unknown_type |
||
271 | */ |
||
272 | public function getCorrect() |
||
276 | |||
277 | /** |
||
278 | * @param unknown_type $correct |
||
279 | */ |
||
280 | public function setCorrect($correct) |
||
286 | |||
287 | /** |
||
288 | * @return the unknown_type |
||
289 | */ |
||
290 | public function getCreatedAt() |
||
294 | |||
295 | /** |
||
296 | * @param unknown_type $created_at |
||
297 | */ |
||
298 | public function setCreatedAt($created_at) |
||
304 | |||
305 | /** |
||
306 | * @return the unknown_type |
||
307 | */ |
||
308 | public function getUpdatedAt() |
||
312 | |||
313 | /** |
||
314 | * @param unknown_type $updated_at |
||
315 | */ |
||
316 | public function setUpdatedAt($updated_at) |
||
322 | |||
323 | /** |
||
324 | * Convert the object to an array. |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | public function getArrayCopy() |
||
334 | |||
335 | /** |
||
336 | * Populate from an array. |
||
337 | * |
||
338 | * @param array $data |
||
339 | */ |
||
340 | public function populate($data = array()) |
||
378 | |||
379 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
383 | |||
384 | public function getInputFilter() |
||
448 | } |
||
449 |
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.