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 TradingCardModel 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 TradingCardModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class TradingCardModel implements InputFilterAwareInterface, \JsonSerializable |
||
23 | { |
||
24 | protected $inputFilter; |
||
25 | |||
26 | /** |
||
27 | * @ORM\Id |
||
28 | * @ORM\Column(type="integer"); |
||
29 | * @ORM\GeneratedValue(strategy="AUTO") |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * @ORM\ManyToOne(targetEntity="TradingCard", inversedBy="models") |
||
35 | * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE") |
||
36 | */ |
||
37 | protected $game; |
||
38 | |||
39 | /** |
||
40 | * @ORM\OneToMany(targetEntity="TradingCardCard", mappedBy="model", cascade={"persist","remove"}) |
||
41 | */ |
||
42 | private $cards; |
||
43 | |||
44 | /** |
||
45 | * @Gedmo\Translatable |
||
46 | * @ORM\Column(type="string", length=255, nullable=false) |
||
47 | */ |
||
48 | protected $title; |
||
49 | |||
50 | /** |
||
51 | * @Gedmo\Translatable |
||
52 | * @ORM\Column(type="text", nullable=true) |
||
53 | */ |
||
54 | protected $description; |
||
55 | |||
56 | /** |
||
57 | * @Gedmo\Translatable |
||
58 | * @ORM\Column(name="image", type="string", length=255, nullable=true) |
||
59 | */ |
||
60 | protected $image; |
||
61 | |||
62 | /** |
||
63 | * The type of the card : collector, standard, ... |
||
64 | * @Gedmo\Translatable |
||
65 | * @ORM\Column(type="string", length=255, nullable=true) |
||
66 | */ |
||
67 | protected $type; |
||
68 | |||
69 | /** |
||
70 | * a card can be part of a set of cards |
||
71 | * @Gedmo\Translatable |
||
72 | * @ORM\Column(type="string", length=255, nullable=true) |
||
73 | */ |
||
74 | protected $family; |
||
75 | |||
76 | /** |
||
77 | * @ORM\Column(type="integer", nullable=true) |
||
78 | */ |
||
79 | protected $points = 0; |
||
80 | |||
81 | /** |
||
82 | * @ORM\Column(type="float", nullable=false) |
||
83 | */ |
||
84 | protected $distribution = 1; |
||
85 | |||
86 | /** |
||
87 | * datetime when this model will be available |
||
88 | * @ORM\Column(type="datetime", nullable=true) |
||
89 | */ |
||
90 | protected $availability; |
||
91 | |||
92 | /** |
||
93 | * @ORM\Column(name="json_data", type="text", nullable=true) |
||
94 | */ |
||
95 | protected $jsonData; |
||
96 | |||
97 | /** |
||
98 | * @ORM\Column(name="created_at", type="datetime") |
||
99 | */ |
||
100 | protected $createdAt; |
||
101 | |||
102 | /** |
||
103 | * @ORM\Column(name="updated_at",type="datetime") |
||
104 | */ |
||
105 | protected $updatedAt; |
||
106 | |||
107 | public function __construct() |
||
111 | |||
112 | /** |
||
113 | * @PrePersist |
||
114 | */ |
||
115 | public function createChrono() |
||
120 | |||
121 | /** |
||
122 | * @PreUpdate |
||
123 | */ |
||
124 | public function updateChrono() |
||
128 | |||
129 | /** |
||
130 | * Gets the value of id. |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public function getId() |
||
138 | |||
139 | /** |
||
140 | * Sets the value of id. |
||
141 | * |
||
142 | * @param mixed $id the id |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | public function setId($id) |
||
152 | |||
153 | /** |
||
154 | * Gets the value of title. |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function getTitle() |
||
162 | |||
163 | /** |
||
164 | * Sets the value of title. |
||
165 | * |
||
166 | * @param mixed $title the title |
||
167 | * |
||
168 | * @return self |
||
169 | */ |
||
170 | public function setTitle($title) |
||
176 | |||
177 | /** |
||
178 | * Gets the value of description. |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function getDescription() |
||
186 | |||
187 | /** |
||
188 | * Sets the value of description. |
||
189 | * |
||
190 | * @param mixed $description the description |
||
191 | * |
||
192 | * @return self |
||
193 | */ |
||
194 | public function setDescription($description) |
||
200 | |||
201 | /** |
||
202 | * Gets the value of image. |
||
203 | * |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function getImage() |
||
210 | |||
211 | /** |
||
212 | * Sets the value of image. |
||
213 | * |
||
214 | * @param mixed $image the image |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | public function setImage($image) |
||
224 | |||
225 | /** |
||
226 | * Gets the value of type. |
||
227 | * |
||
228 | * @return mixed |
||
229 | */ |
||
230 | public function getType() |
||
234 | |||
235 | /** |
||
236 | * Sets the value of type. |
||
237 | * |
||
238 | * @param mixed $type the type |
||
239 | * |
||
240 | * @return self |
||
241 | */ |
||
242 | public function setType($type) |
||
248 | |||
249 | /** |
||
250 | * Gets the value of family. |
||
251 | * |
||
252 | * @return mixed |
||
253 | */ |
||
254 | public function getFamily() |
||
258 | |||
259 | /** |
||
260 | * Sets the value of family. |
||
261 | * |
||
262 | * @param mixed $family the family |
||
263 | * |
||
264 | * @return self |
||
265 | */ |
||
266 | public function setFamily($family) |
||
272 | |||
273 | /** |
||
274 | * Gets the value of points. |
||
275 | * |
||
276 | * @return mixed |
||
277 | */ |
||
278 | public function getPoints() |
||
282 | |||
283 | /** |
||
284 | * Sets the value of points. |
||
285 | * |
||
286 | * @param mixed $points the points |
||
287 | * |
||
288 | * @return self |
||
289 | */ |
||
290 | public function setPoints($points) |
||
296 | |||
297 | /** |
||
298 | * Gets the value of distribution. |
||
299 | * |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function getDistribution() |
||
306 | |||
307 | /** |
||
308 | * Sets the value of distribution. |
||
309 | * |
||
310 | * @param mixed $distribution the distribution |
||
311 | * |
||
312 | * @return self |
||
313 | */ |
||
314 | public function setDistribution($distribution) |
||
320 | |||
321 | /** |
||
322 | * Gets the value of availability. |
||
323 | * |
||
324 | * @return mixed |
||
325 | */ |
||
326 | public function getAvailability() |
||
330 | |||
331 | /** |
||
332 | * Sets the value of availability. |
||
333 | * |
||
334 | * @param mixed $availability the availability |
||
335 | * |
||
336 | * @return self |
||
337 | */ |
||
338 | public function setAvailability($availability) |
||
344 | |||
345 | /** |
||
346 | * Gets the value of game. |
||
347 | * |
||
348 | * @return mixed |
||
349 | */ |
||
350 | public function getGame() |
||
354 | |||
355 | /** |
||
356 | * Sets the value of game. |
||
357 | * |
||
358 | * @param mixed $game the game |
||
359 | * |
||
360 | * @return self |
||
361 | */ |
||
362 | public function setGame($game) |
||
368 | |||
369 | /** |
||
370 | * Gets the value of jsonData. |
||
371 | * |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function getJsonData() |
||
378 | |||
379 | /** |
||
380 | * Sets the value of jsonData. |
||
381 | * |
||
382 | * @param mixed $jsonData the json data |
||
383 | * |
||
384 | * @return self |
||
385 | */ |
||
386 | public function setJsonData($jsonData) |
||
392 | |||
393 | /** |
||
394 | * Gets the value of cards. |
||
395 | * |
||
396 | * @return mixed |
||
397 | */ |
||
398 | public function getCards() |
||
402 | |||
403 | /** |
||
404 | * Sets the value of cards. |
||
405 | * |
||
406 | * @param mixed $cards the cards |
||
407 | * |
||
408 | * @return self |
||
409 | */ |
||
410 | private function setCards($cards) |
||
416 | |||
417 | public function addCards(ArrayCollection $cards) |
||
424 | |||
425 | public function removeCards(ArrayCollection $cards) |
||
432 | |||
433 | /** |
||
434 | * Add an card to the Model. |
||
435 | * |
||
436 | * @param TradingCardModel $card |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | public function addCard($card) |
||
444 | |||
445 | /** |
||
446 | * Gets the value of createdAt. |
||
447 | * |
||
448 | * @return mixed |
||
449 | */ |
||
450 | public function getCreatedAt() |
||
454 | |||
455 | /** |
||
456 | * Sets the value of createdAt. |
||
457 | * |
||
458 | * @param mixed $createdAt the created at |
||
459 | * |
||
460 | * @return self |
||
461 | */ |
||
462 | public function setCreatedAt($createdAt) |
||
468 | |||
469 | /** |
||
470 | * Gets the value of updatedAt. |
||
471 | * |
||
472 | * @return mixed |
||
473 | */ |
||
474 | public function getUpdatedAt() |
||
478 | |||
479 | /** |
||
480 | * Sets the value of updatedAt. |
||
481 | * |
||
482 | * @param mixed $updatedAt the updated at |
||
483 | * |
||
484 | * @return self |
||
485 | */ |
||
486 | public function setUpdatedAt($updatedAt) |
||
492 | |||
493 | /** |
||
494 | * Convert the object to an array. |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | public function getArrayCopy() |
||
504 | |||
505 | /** |
||
506 | * Convert the object to json. |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | public function jsonSerialize() |
||
525 | |||
526 | /** |
||
527 | * Populate from an array. |
||
528 | * |
||
529 | * @param array $data |
||
530 | */ |
||
531 | public function populate($data = array()) |
||
569 | |||
570 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
574 | |||
575 | public function getInputFilter() |
||
591 | } |
||
592 |