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 MissionGame 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 MissionGame, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class MissionGame implements InputFilterAwareInterface |
||
19 | { |
||
20 | protected $inputFilter; |
||
21 | |||
22 | /** |
||
23 | * @ORM\Id |
||
24 | * @ORM\Column(type="integer"); |
||
25 | * @ORM\GeneratedValue(strategy="AUTO") |
||
26 | */ |
||
27 | protected $id; |
||
28 | |||
29 | /** |
||
30 | * @ORM\ManyToOne(targetEntity="PlaygroundGame\Entity\Game") |
||
31 | * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE") |
||
32 | **/ |
||
33 | protected $game; |
||
34 | |||
35 | /** |
||
36 | * @ORM\ManyToOne(targetEntity="Mission", inversedBy="missionGames") |
||
37 | * @ORM\JoinColumn(name="mission_id", referencedColumnName="id", onDelete="CASCADE") |
||
38 | * |
||
39 | **/ |
||
40 | protected $mission; |
||
41 | |||
42 | /** |
||
43 | * @ORM\OneToMany(targetEntity="MissionGameCondition", mappedBy="missionGame", cascade={"persist","remove"}) |
||
44 | */ |
||
45 | protected $conditions; |
||
46 | |||
47 | /** |
||
48 | * position |
||
49 | * @ORM\Column(name="position", type="integer", nullable=false) |
||
50 | */ |
||
51 | protected $position; |
||
52 | |||
53 | /** |
||
54 | * @ORM\Column(name="created_at", type="datetime", nullable=true) |
||
55 | */ |
||
56 | protected $createdAt; |
||
57 | |||
58 | /** |
||
59 | * @ORM\Column(name="updated_at", type="datetime", nullable=true) |
||
60 | */ |
||
61 | protected $updatedAt; |
||
62 | |||
63 | /** |
||
64 | * Constructor |
||
65 | */ |
||
66 | public function __construct() |
||
70 | |||
71 | public function __clone() |
||
75 | |||
76 | /** @PrePersist */ |
||
77 | public function createChrono() |
||
82 | |||
83 | /** @PreUpdate */ |
||
84 | public function updateChrono() |
||
88 | |||
89 | /** |
||
90 | * @param $id |
||
91 | * @return MissionGame |
||
92 | */ |
||
93 | public function setId($id) |
||
99 | |||
100 | /** |
||
101 | * @return integer |
||
102 | */ |
||
103 | public function getId() |
||
107 | |||
108 | /** |
||
109 | * @param $id |
||
110 | * @return MissionGame |
||
111 | */ |
||
112 | public function setPosition($position) |
||
118 | |||
119 | /** |
||
120 | * @return integer |
||
121 | */ |
||
122 | public function getPosition() |
||
126 | |||
127 | /** |
||
128 | * @return the $game |
||
129 | */ |
||
130 | public function getGame() |
||
134 | |||
135 | /** |
||
136 | * @param field_type $game |
||
137 | */ |
||
138 | public function setGame($game) |
||
144 | |||
145 | /** |
||
146 | * @return \PlaygroundGame\Service\Mission $game |
||
147 | */ |
||
148 | public function getMission() |
||
152 | |||
153 | /** |
||
154 | * @param \PlaygroundGame\Service\Mission $mission |
||
155 | */ |
||
156 | public function setMission($mission) |
||
162 | |||
163 | /** |
||
164 | * @return the $conditions |
||
165 | */ |
||
166 | public function getConditions() |
||
170 | |||
171 | /** |
||
172 | * @param field_type $conditions |
||
173 | */ |
||
174 | public function setConditions($conditions) |
||
180 | |||
181 | public function addConditions(ArrayCollection $conditions) |
||
188 | |||
189 | public function removeConditions(ArrayCollection $conditions) |
||
195 | |||
196 | /** |
||
197 | * Add a condition to the mission game. |
||
198 | * |
||
199 | * @param MisionGameCondition $condition |
||
200 | * |
||
201 | * @return void |
||
202 | */ |
||
203 | public function addCondition($condition) |
||
207 | |||
208 | /** |
||
209 | * |
||
210 | * @return \DateTime $createdAt |
||
211 | */ |
||
212 | public function getCreatedAt() |
||
216 | |||
217 | /** |
||
218 | * |
||
219 | * @param \DateTime $createdAt |
||
220 | */ |
||
221 | public function setCreatedAt($createdAt) |
||
227 | |||
228 | /** |
||
229 | * |
||
230 | * @return \DateTime $updatedAt |
||
231 | */ |
||
232 | public function getUpdatedAt() |
||
236 | |||
237 | /** |
||
238 | * |
||
239 | * @param \DateTime $updatedAt |
||
240 | */ |
||
241 | public function setUpdatedAt($updatedAt) |
||
247 | |||
248 | /** |
||
249 | * Are the conditions linked to this game fulfilled ? |
||
250 | * |
||
251 | * @return boolean |
||
252 | */ |
||
253 | public function fulfillConditions($entry = null) |
||
291 | |||
292 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
296 | |||
297 | View Code Duplication | public function getInputFilter() |
|
312 | } |
||
313 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.