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 |
||
16 | View Code Duplication | class PostVoteComment implements InputFilterAwareInterface |
|
|
|||
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="PostVotePost", inversedBy="comments") |
||
29 | * @ORM\JoinColumn(name="post_id", referencedColumnName="id", onDelete="CASCADE") |
||
30 | **/ |
||
31 | protected $post; |
||
32 | |||
33 | /** |
||
34 | * @ORM\ManyToOne(targetEntity="PostVote") |
||
35 | **/ |
||
36 | protected $postvote; |
||
37 | |||
38 | /** |
||
39 | * @ORM\ManyToOne(targetEntity="PlaygroundUser\Entity\User") |
||
40 | * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id") |
||
41 | **/ |
||
42 | protected $user; |
||
43 | |||
44 | /** |
||
45 | * @ORM\Column(type="text", nullable=true) |
||
46 | */ |
||
47 | protected $ip; |
||
48 | |||
49 | /** |
||
50 | * @ORM\Column(type="text", nullable=true) |
||
51 | */ |
||
52 | protected $message; |
||
53 | |||
54 | /** |
||
55 | * @ORM\Column(name="created_at", type="datetime") |
||
56 | */ |
||
57 | protected $createdAt; |
||
58 | |||
59 | /** |
||
60 | * @ORM\Column(name="updated_at", type="datetime") |
||
61 | */ |
||
62 | protected $updatedAt; |
||
63 | |||
64 | /** @PrePersist */ |
||
65 | public function createChrono() |
||
70 | |||
71 | /** @PreUpdate */ |
||
72 | public function updateChrono() |
||
76 | |||
77 | /** |
||
78 | * @return the unknown_type |
||
79 | */ |
||
80 | public function getId() |
||
84 | |||
85 | /** |
||
86 | * @param unknown_type $id |
||
87 | */ |
||
88 | public function setId($id) |
||
94 | |||
95 | /** |
||
96 | * @return string unknown_type |
||
97 | */ |
||
98 | public function getIp() |
||
102 | |||
103 | /** |
||
104 | * @param string $ip |
||
105 | */ |
||
106 | public function setIp($ip) |
||
112 | |||
113 | /** |
||
114 | * @return the unknown_type |
||
115 | */ |
||
116 | public function getUser() |
||
117 | { |
||
118 | return $this->user; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param unknown_type $user |
||
123 | */ |
||
124 | public function setUser($user) |
||
125 | { |
||
126 | $this->user = $user; |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return the unknown_type |
||
133 | */ |
||
134 | public function getPost() |
||
138 | |||
139 | /** |
||
140 | * @param unknown_type $post |
||
141 | */ |
||
142 | public function setPost($post) |
||
150 | |||
151 | /** |
||
152 | * @return the unknown_type |
||
153 | */ |
||
154 | public function getPostvote() |
||
158 | |||
159 | /** |
||
160 | * @param unknown_type $postvote |
||
161 | */ |
||
162 | public function setPostvote($postvote) |
||
169 | |||
170 | /** |
||
171 | * @return the unknown_type |
||
172 | */ |
||
173 | public function getMessage() |
||
177 | |||
178 | /** |
||
179 | * @param unknown_type $message |
||
180 | */ |
||
181 | public function setMessage($message) |
||
187 | |||
188 | /** |
||
189 | * @return the unknown_type |
||
190 | */ |
||
191 | public function getCreatedAt() |
||
195 | |||
196 | /** |
||
197 | * @param unknown_type $createdAt |
||
198 | */ |
||
199 | public function setCreatedAt($createdAt) |
||
205 | |||
206 | /** |
||
207 | * @return the unknown_type |
||
208 | */ |
||
209 | public function getUpdatedAt() |
||
213 | |||
214 | /** |
||
215 | * @param unknown_type $updatedAt |
||
216 | */ |
||
217 | public function setUpdatedAt($updatedAt) |
||
223 | |||
224 | /** |
||
225 | * Convert the object to an array. |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function getArrayCopy() |
||
235 | |||
236 | /** |
||
237 | * Populate from an array. |
||
238 | * |
||
239 | * @param array $data |
||
240 | */ |
||
241 | public function populate($data = array()) |
||
244 | |||
245 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
249 | |||
250 | public function getInputFilter() |
||
260 | } |
||
261 |
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.