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 Prize 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 Prize, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Prize implements \JsonSerializable |
||
17 | { |
||
18 | protected $inputFilter; |
||
19 | |||
20 | /** |
||
21 | * @ORM\Id |
||
22 | * @ORM\Column(type="integer"); |
||
23 | * @ORM\GeneratedValue(strategy="AUTO") |
||
24 | */ |
||
25 | protected $id; |
||
26 | |||
27 | /** |
||
28 | * @ORM\ManyToOne(targetEntity="Game", inversedBy="prizes") |
||
29 | * |
||
30 | **/ |
||
31 | protected $game; |
||
32 | |||
33 | /** |
||
34 | * @ORM\Column(type="string", length=255, nullable=false) |
||
35 | */ |
||
36 | protected $title; |
||
37 | |||
38 | /** |
||
39 | * @ORM\Column(type="string", length=255, nullable=false) |
||
40 | */ |
||
41 | protected $identifier; |
||
42 | |||
43 | /** |
||
44 | * @ORM\ManyToOne(targetEntity="PrizeCategory") |
||
45 | * @ORM\JoinColumn(name="prize_category_id", referencedColumnName="id") |
||
46 | **/ |
||
47 | protected $prizeCategory; |
||
48 | |||
49 | /** |
||
50 | * @ORM\Column(name="prize_content",type="text", nullable=true) |
||
51 | */ |
||
52 | protected $prizeContent; |
||
53 | |||
54 | /** |
||
55 | * @ORM\Column(type="integer", nullable=false) |
||
56 | */ |
||
57 | protected $qty = 0; |
||
58 | |||
59 | /** |
||
60 | * @ORM\Column(name="unit_price", type="float", nullable=false) |
||
61 | */ |
||
62 | protected $unitPrice = 1; |
||
63 | |||
64 | /** |
||
65 | * @ORM\Column(type="string", length=10, nullable=true) |
||
66 | */ |
||
67 | protected $currency; |
||
68 | |||
69 | /** |
||
70 | * @ORM\Column(name="created_at", type="datetime") |
||
71 | */ |
||
72 | protected $createdAt; |
||
73 | |||
74 | /** |
||
75 | * @ORM\Column(name="updated_at", type="datetime") |
||
76 | */ |
||
77 | protected $updatedAt; |
||
78 | |||
79 | /** |
||
80 | * @ORM\Column(type="string", length=255, nullable=true) |
||
81 | */ |
||
82 | protected $picture; |
||
83 | |||
84 | /** @PrePersist */ |
||
85 | public function createChrono() |
||
86 | { |
||
87 | $this->createdAt = new \DateTime("now"); |
||
88 | $this->updatedAt = new \DateTime("now"); |
||
89 | } |
||
90 | |||
91 | /** @PreUpdate */ |
||
92 | public function updateChrono() |
||
96 | |||
97 | /** |
||
98 | * @return the $id |
||
99 | */ |
||
100 | public function getId() |
||
104 | |||
105 | /** |
||
106 | * @param field_type $id |
||
107 | */ |
||
108 | public function setId($id) |
||
114 | |||
115 | /** |
||
116 | * @return the unknown_type |
||
117 | */ |
||
118 | public function getGame() |
||
122 | |||
123 | /** |
||
124 | * @param unknown_type $game |
||
125 | */ |
||
126 | public function setGame($game) |
||
132 | |||
133 | /** |
||
134 | * @return the $title |
||
135 | */ |
||
136 | public function getTitle() |
||
140 | |||
141 | /** |
||
142 | * @param field_type $title |
||
143 | */ |
||
144 | public function setTitle($title) |
||
150 | |||
151 | /** |
||
152 | * @return the $identifier |
||
153 | */ |
||
154 | public function getIdentifier() |
||
158 | |||
159 | /** |
||
160 | * @param field_type $identifier |
||
161 | */ |
||
162 | public function setIdentifier($identifier) |
||
168 | |||
169 | /** |
||
170 | * @return the $prizeCategory |
||
171 | */ |
||
172 | public function getPrizeCategory() |
||
176 | |||
177 | /** |
||
178 | * @param unknown $prizeCategory |
||
179 | */ |
||
180 | public function setPrizeCategory($prizeCategory) |
||
186 | |||
187 | /** |
||
188 | * @return the $prizeContent |
||
189 | */ |
||
190 | public function getPrizeContent() |
||
194 | |||
195 | /** |
||
196 | */ |
||
197 | public function setPrizeContent($prizeContent) |
||
203 | |||
204 | /** |
||
205 | * @return integer $qty |
||
206 | */ |
||
207 | public function getQty() |
||
211 | |||
212 | /** |
||
213 | * @param number $qty |
||
214 | */ |
||
215 | public function setQty($qty) |
||
221 | |||
222 | /** |
||
223 | * @return integer $unitPrice |
||
224 | */ |
||
225 | public function getUnitPrice() |
||
229 | |||
230 | /** |
||
231 | * @param number $unitPrice |
||
232 | */ |
||
233 | public function setUnitPrice($unitPrice) |
||
239 | |||
240 | /** |
||
241 | * @return the $currency |
||
242 | */ |
||
243 | public function getCurrency() |
||
247 | |||
248 | /** |
||
249 | * @param field_type $currency |
||
250 | */ |
||
251 | public function setCurrency($currency) |
||
257 | |||
258 | /** |
||
259 | * @return \DateTime $created_at |
||
260 | */ |
||
261 | public function getCreatedAt() |
||
265 | |||
266 | /** |
||
267 | * @param \DateTime $createdAt |
||
268 | */ |
||
269 | public function setCreatedAt($createdAt) |
||
275 | |||
276 | /** |
||
277 | * @return \DateTime $updatedAt |
||
278 | */ |
||
279 | public function getUpdatedAt() |
||
283 | |||
284 | /** |
||
285 | * @param \DateTime $updatedAt |
||
286 | */ |
||
287 | public function setUpdatedAt($updatedAt) |
||
293 | |||
294 | /** |
||
295 | * @return the $picture |
||
296 | */ |
||
297 | public function getPicture() |
||
301 | |||
302 | /** |
||
303 | * @param field_type $picture |
||
304 | */ |
||
305 | public function setPicture($picture) |
||
311 | |||
312 | /** |
||
313 | * Convert the object to an array. |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public function getArrayCopy() |
||
323 | |||
324 | /** |
||
325 | * Convert the object to json. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | public function jsonSerialize() |
||
333 | |||
334 | /** |
||
335 | * Populate from an array. |
||
336 | * |
||
337 | * @param array $data |
||
338 | */ |
||
339 | public function populate($data = array()) |
||
340 | { |
||
341 | if (isset($data['prizeContent']) && $data['prizeContent'] !== null) { |
||
342 | $this->prizeContent = $data['prizeContent']; |
||
343 | } |
||
344 | |||
345 | View Code Duplication | if (isset($data['title']) && $data['title'] !== null) { |
|
346 | $this->title = $data['title']; |
||
347 | } |
||
348 | |||
349 | View Code Duplication | if (isset($data['qty']) && $data['qty'] !== null) { |
|
350 | $this->qty = $data['qty']; |
||
351 | } |
||
352 | |||
353 | View Code Duplication | if (isset($data['identifier']) && $data['identifier'] !== null) { |
|
354 | $this->identifier = $data['identifier']; |
||
355 | } |
||
356 | |||
357 | if (isset($data['unitPrice']) && $data['unitPrice'] !== null) { |
||
358 | $this->unitPrice = $data['unitPrice']; |
||
359 | } |
||
360 | |||
361 | if (isset($data['currency']) && $data['currency'] !== null) { |
||
362 | $this->currency = $data['currency']; |
||
363 | } |
||
364 | |||
365 | if (isset($data['picture']) && $data['picture'] !== null) { |
||
366 | $this->picture = $data['picture']; |
||
367 | } |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @return InputFilter $inputFilter |
||
372 | */ |
||
373 | View Code Duplication | public function getInputFilter() |
|
410 | |||
411 | /** |
||
412 | * @param InputFilterInterface $inputFilter |
||
413 | */ |
||
414 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
418 | } |
||
419 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.