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 |
||
12 | class Select extends NamedElement |
||
13 | { |
||
14 | use SelectOptionsFromModel; |
||
15 | |||
16 | protected $type = 'select'; |
||
17 | |||
18 | protected $size = ''; |
||
19 | |||
20 | protected $options; |
||
21 | |||
22 | protected $extraOptions; |
||
23 | |||
24 | public function __construct($name, $title, $options = null) |
||
25 | { |
||
26 | parent::__construct($name, $title); |
||
27 | |||
28 | $this->setOptions($options); |
||
29 | |||
30 | $this->extraOptions = new Collection(); |
||
31 | } |
||
32 | |||
33 | public function getSize() |
||
37 | |||
38 | public function setSize($value) |
||
44 | |||
45 | public function addOptions($options) |
||
53 | |||
54 | public function addOption($key, $value) |
||
60 | |||
61 | public function getOptions() |
||
88 | |||
89 | /** |
||
90 | * @param mixed $options |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function setOptions($options) |
||
100 | |||
101 | public function getValue() |
||
105 | |||
106 | public function toArray() |
||
113 | } |
||
114 |
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.