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:
1 | <?php |
||
18 | class MissionGame implements InputFilterAwareInterface |
||
19 | { |
||
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="PlaygroundGame\Entity\Game") |
||
32 | * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE") |
||
33 | **/ |
||
34 | protected $game; |
||
35 | |||
36 | /** |
||
37 | * @ORM\ManyToOne(targetEntity="Mission", inversedBy="missionGames") |
||
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 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
252 | |||
253 | View Code Duplication | public function getInputFilter() |
|
268 | } |
||
269 |
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.