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 |
||
23 | class Button extends Element |
||
24 | { |
||
25 | protected $tagname = 'a'; |
||
26 | |||
27 | protected $classnames = 'button btn'; |
||
28 | |||
29 | protected $drop = false; |
||
30 | |||
31 | protected $block = false; |
||
32 | |||
33 | protected $active = false; |
||
34 | |||
35 | protected $_tags = array('a','input','button'); |
||
36 | |||
37 | protected $_sizes = array('xs','sm','lg'); |
||
38 | protected $_foundations_sizes = array( |
||
39 | 'xs' => 'tiny', 'xs' => 'small', 'lg' => 'large', |
||
40 | ); |
||
41 | |||
42 | protected $_types = array('default','primary','success','info','warning','alert','danger','link'); |
||
43 | |||
44 | /** |
||
45 | * View helper entry point: |
||
46 | * Retrieves helper and optionally sets component options to operate on |
||
47 | * |
||
48 | * @param array|StdClass $options [optional] component options to operate on |
||
49 | * @return self |
||
50 | */ |
||
51 | public function __invoke($options = array()) |
||
120 | |||
121 | // |
||
122 | // private getters/setters |
||
123 | // |
||
124 | |||
125 | /** |
||
126 | * @return the $_sizes |
||
127 | */ |
||
128 | public function getSizes() { |
||
131 | |||
132 | /** |
||
133 | * @param multitype:string $_sizes |
||
134 | */ |
||
135 | public function setSizes($_sizes) { |
||
139 | |||
140 | /** |
||
141 | * @return the $_types |
||
142 | */ |
||
143 | public function getTypes() { |
||
146 | |||
147 | /** |
||
148 | * @param multitype:string $_types |
||
149 | */ |
||
150 | public function setTypes($_types) { |
||
154 | |||
155 | /** |
||
156 | * @return the $_tags |
||
157 | */ |
||
158 | public function getTags() { |
||
161 | |||
162 | /** |
||
163 | * @param multitype:string $_tags |
||
164 | */ |
||
165 | public function setTags($_tags) { |
||
169 | |||
170 | |||
171 | // |
||
172 | // option getters/setters |
||
173 | // |
||
174 | |||
175 | |||
176 | /** |
||
177 | * @return the $drop |
||
178 | */ |
||
179 | public function getDrop() { |
||
182 | |||
183 | /** |
||
184 | * @param string $drop |
||
185 | */ |
||
186 | public function setDrop($drop) { |
||
192 | |||
193 | /** |
||
194 | * @return the $block |
||
195 | */ |
||
196 | public function getBlock() { |
||
199 | |||
200 | /** |
||
201 | * @param boolean $block |
||
202 | */ |
||
203 | public function setBlock($block) { |
||
207 | |||
208 | /** |
||
209 | * @return the $active |
||
210 | */ |
||
211 | public function getActive() { |
||
214 | |||
215 | /** |
||
216 | * @param boolean $active |
||
217 | */ |
||
218 | public function setActive($active) { |
||
222 | |||
223 | |||
224 | |||
225 | |||
226 | |||
227 | } |
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.