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 |
||
17 | class PostVote extends Game implements InputFilterAwareInterface |
||
18 | { |
||
19 | const CLASSTYPE = 'postvote'; |
||
20 | |||
21 | /** |
||
22 | * Display mode of Posts : |
||
23 | * 'date' : sort by post date desc |
||
24 | * 'random' : ... |
||
25 | * 'votes' : sort by number of vote desc |
||
26 | * |
||
27 | * @ORM\Column(name="post_display_mode", type="string", nullable=false) |
||
28 | */ |
||
29 | protected $postDisplayMode = 'date'; |
||
30 | |||
31 | /** |
||
32 | * Number of Post displayed : |
||
33 | * 0 : infinite |
||
34 | * |
||
35 | * @ORM\Column(name="post_display_number", type="integer", nullable=false) |
||
36 | */ |
||
37 | protected $postDisplayNumber = 0; |
||
38 | |||
39 | /** |
||
40 | * Is it possible to vote ? |
||
41 | * @ORM\Column(name="vote_active", type="boolean", nullable=false) |
||
42 | */ |
||
43 | protected $voteActive = 1; |
||
44 | |||
45 | /** |
||
46 | * Is it possible to vote anonymously ? |
||
47 | * @ORM\Column(name="vote_anonymous", type="boolean", nullable=false) |
||
48 | */ |
||
49 | protected $voteAnonymous; |
||
50 | |||
51 | /** |
||
52 | * Type of moderation : moderate posts before their publication, or after their publication (default) |
||
53 | * @ORM\Column(name="moderation_type", type="boolean", nullable=false, options={"default" = 0}) |
||
54 | */ |
||
55 | protected $moderationType = 0; |
||
56 | |||
57 | /** |
||
58 | * @ORM\OneToOne(targetEntity="PostVoteForm", mappedBy="postvote", cascade={"persist","remove"}) |
||
59 | **/ |
||
60 | private $form; |
||
61 | |||
62 | /** |
||
63 | * @ORM\OneToMany(targetEntity="PostVotePost", mappedBy="post_vote") |
||
64 | **/ |
||
65 | private $posts; |
||
66 | |||
67 | public function __construct() |
||
73 | |||
74 | /** |
||
75 | * Add a post to the game |
||
76 | * |
||
77 | * @param PostVotePost $post |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function addPost($post) |
||
85 | |||
86 | public function getPosts() |
||
90 | |||
91 | public function setPosts($posts) |
||
97 | |||
98 | /** |
||
99 | * @return the unknown_type |
||
100 | */ |
||
101 | public function getForm() |
||
105 | |||
106 | /** |
||
107 | * @param unknown_type $form |
||
108 | */ |
||
109 | public function setForm($form) |
||
115 | |||
116 | /** |
||
117 | * @return string unknown_type |
||
118 | */ |
||
119 | public function getPostDisplayMode() |
||
123 | |||
124 | /** |
||
125 | * @param unknown_type $postDisplayMode |
||
126 | */ |
||
127 | public function setPostDisplayMode($postDisplayMode) |
||
133 | |||
134 | /** |
||
135 | * @return int |
||
136 | */ |
||
137 | public function getPostDisplayNumber() |
||
141 | |||
142 | /** |
||
143 | * @param int $postDisplayNumber |
||
144 | * @return PostVote |
||
145 | */ |
||
146 | public function setPostDisplayNumber($postDisplayNumber) |
||
151 | |||
152 | /** |
||
153 | * @return the unknown_type |
||
154 | */ |
||
155 | public function getVoteActive() |
||
159 | |||
160 | /** |
||
161 | * @param unknown_type $voteActive |
||
162 | */ |
||
163 | public function setVoteActive($voteActive) |
||
169 | |||
170 | /** |
||
171 | * @return the unknown_type |
||
172 | */ |
||
173 | public function getVoteAnonymous() |
||
177 | |||
178 | /** |
||
179 | * @param unknown_type $voteAnonymous |
||
180 | */ |
||
181 | public function setVoteAnonymous($voteAnonymous) |
||
187 | |||
188 | /** |
||
189 | * @return bool |
||
190 | */ |
||
191 | public function getModerationType() |
||
195 | |||
196 | /** |
||
197 | * @param bool $moderationType |
||
198 | */ |
||
199 | public function setModerationType($moderationType) |
||
205 | |||
206 | /** |
||
207 | * Convert the object to an array. |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | public function getArrayCopy() |
||
218 | |||
219 | /** |
||
220 | * Populate from an array. |
||
221 | * |
||
222 | * @param array $data |
||
223 | */ |
||
224 | View Code Duplication | public function populate($data = array()) |
|
236 | |||
237 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
241 | |||
242 | public function getInputFilter() |
||
317 | } |
||
318 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: