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 |
||
14 | abstract class HtmlCollection extends HtmlDoubleElement { |
||
15 | |||
16 | public function __construct($identifier,$tagName="div"){ |
||
20 | |||
21 | View Code Duplication | public function addItems($items){ |
|
32 | |||
33 | public function setItems($items){ |
||
37 | |||
38 | public function getItems(){ |
||
41 | |||
42 | /** |
||
43 | * adds and returns an item |
||
44 | * @param HtmlDoubleElement|string $item |
||
45 | * @return \Ajax\common\html\HtmlDoubleElement |
||
46 | */ |
||
47 | public function addItem($item){ |
||
55 | |||
56 | /** |
||
57 | * Return the item at index |
||
58 | * @param int|string $index the index or the item identifier |
||
59 | * @return \Ajax\common\html\HtmlDoubleElement |
||
60 | */ |
||
61 | View Code Duplication | public function getItem($index) { |
|
69 | |||
70 | public function setItem($index, $value) { |
||
74 | |||
75 | public function removeItem($index){ |
||
78 | |||
79 | public function count(){ |
||
82 | |||
83 | /* (non-PHPdoc) |
||
84 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
85 | */ |
||
86 | public function fromDatabaseObject($object, $function) { |
||
89 | |||
90 | public function apply($callBack){ |
||
95 | |||
96 | /* |
||
97 | * (non-PHPdoc) |
||
98 | * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
||
99 | */ |
||
100 | public function fromArray($array) { |
||
103 | /** |
||
104 | * The item factory |
||
105 | * @param string|HtmlDoubleElement $value |
||
106 | */ |
||
107 | protected abstract function createItem($value); |
||
108 | |||
109 | protected function createCondition($value){ |
||
112 | |||
113 | /* |
||
114 | * (non-PHPdoc) |
||
115 | * @see \Ajax\bootstrap\html\base\HtmlSingleElement::run() |
||
116 | */ |
||
117 | View Code Duplication | public function run(JsUtils $js) { |
|
123 | |||
124 | protected function contentAs($tagName){ |
||
129 | } |
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.