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 |
||
15 | abstract class HtmlCollection extends HtmlDoubleElement { |
||
16 | |||
17 | public function __construct($identifier,$tagName="div"){ |
||
21 | |||
22 | public function addItems($items){ |
||
34 | |||
35 | public function setItems($items){ |
||
39 | |||
40 | public function getItems(){ |
||
43 | |||
44 | protected function getItemToAdd($item){ |
||
51 | |||
52 | protected function setItemIdentifier($item,$classname,$index){ |
||
59 | |||
60 | /** |
||
61 | * adds and returns an item |
||
62 | * @param HtmlDoubleElement|string|array $item |
||
63 | * @return \Ajax\common\html\HtmlDoubleElement |
||
64 | */ |
||
65 | public function addItem($item){ |
||
70 | |||
71 | public function insertItem($item,$position=0){ |
||
76 | |||
77 | /** |
||
78 | * Return the item at index |
||
79 | * @param int|string $index the index or the item identifier |
||
80 | * @return \Ajax\common\html\HtmlDoubleElement |
||
81 | */ |
||
82 | public function getItem($index) { |
||
90 | |||
91 | public function setItem($index, $value) { |
||
95 | |||
96 | public function removeItem($index){ |
||
99 | |||
100 | public function count(){ |
||
103 | |||
104 | /* (non-PHPdoc) |
||
105 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
106 | */ |
||
107 | public function fromDatabaseObject($object, $function) { |
||
110 | |||
111 | public function apply($callBack){ |
||
117 | |||
118 | /* |
||
119 | * (non-PHPdoc) |
||
120 | * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
||
121 | */ |
||
122 | public function fromArray($array) { |
||
126 | /** |
||
127 | * The item factory |
||
128 | * @param mixed $value |
||
129 | */ |
||
130 | abstract protected function createItem($value); |
||
131 | |||
132 | protected function createCondition($value){ |
||
135 | |||
136 | protected function contentAs($tagName){ |
||
142 | |||
143 | public function setProperties($properties){ |
||
154 | |||
155 | /** |
||
156 | * Sets the values of a property for each item in the collection |
||
157 | * @param string $property |
||
158 | * @param array $values |
||
159 | * @return HtmlCollection |
||
160 | */ |
||
161 | public function setPropertyValues($property,$values){ |
||
177 | |||
178 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
186 | |||
187 | public function getItemById($identifier){ |
||
190 | } |
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.