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 |
||
18 | class HtmlBreadcrumb extends HtmlSemNavElement { |
||
19 | /** |
||
20 | * |
||
21 | * @var integer the start index for href generation |
||
22 | */ |
||
23 | protected $startIndex=0; |
||
24 | /** |
||
25 | * |
||
26 | * @var boolean $autoActive sets the last element's class to <b>active</b> if true |
||
27 | */ |
||
28 | protected $autoActive; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var boolean if set to true, the path of the elements is absolute |
||
33 | */ |
||
34 | protected $absolutePaths=false; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var object<Closure> the function who generates the href elements. default : function($e){return $e->getContent()} |
||
39 | */ |
||
40 | protected $_hrefFunction; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @param string $identifier |
||
45 | * @param array $items |
||
46 | * @param boolean $autoActive sets the last element's class to <b>active</b> if true |
||
47 | * @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()} |
||
48 | */ |
||
49 | View Code Duplication | public function __construct($identifier, $items=array(), $autoActive=true, $startIndex=0, $hrefFunction=NULL) { |
|
62 | |||
63 | /** |
||
64 | * Associate an ajax get to the breadcrumb elements, displayed in $targetSelector |
||
65 | * $attr member is used to build each element url |
||
66 | * @param string $targetSelector the target of the get |
||
67 | * @param string $attr the html attribute used to build the elements url |
||
68 | * @return HtmlBreadcrumbs |
||
69 | */ |
||
70 | public function autoGetOnClick($targetSelector) { |
||
73 | |||
74 | public function contentAsString() { |
||
80 | |||
81 | public function setActive($index=null) { |
||
89 | |||
90 | /** |
||
91 | * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters |
||
92 | * @param JsUtils $js |
||
93 | * @param Dispatcher $dispatcher the request dispatcher |
||
94 | * @return \Ajax\bootstrap\html\HtmlBreadcrumbs |
||
95 | */ |
||
96 | public function fromDispatcher(JsUtils $js,$dispatcher, $startIndex=0) { |
||
99 | |||
100 | /** |
||
101 | * Return the url of the element at $index or the breadcrumbs url if $index is ommited |
||
102 | * @param int $index |
||
103 | * @param string $separator |
||
104 | * @return string |
||
105 | */ |
||
106 | View Code Duplication | public function getHref($index=null, $separator="/") { |
|
118 | |||
119 | /** |
||
120 | * sets the function who generates the href elements. |
||
121 | * default : function($element){return $element->getContent()} |
||
122 | * @param function $_hrefFunction |
||
123 | * @return \Ajax\bootstrap\html\HtmlBreadcrumbs |
||
124 | */ |
||
125 | public function setHrefFunction($_hrefFunction) { |
||
129 | |||
130 | public function setStartIndex($startIndex) { |
||
134 | |||
135 | public function setAutoActive($autoActive) { |
||
139 | |||
140 | /* |
||
141 | * (non-PHPdoc) |
||
142 | * @see \Ajax\bootstrap\html\BaseHtml::compile() |
||
143 | */ |
||
144 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
154 | |||
155 | /* |
||
156 | * (non-PHPdoc) |
||
157 | * @see \Ajax\bootstrap\html\base\BaseHtml::on() |
||
158 | */ |
||
159 | public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
165 | |||
166 | public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
||
174 | |||
175 | /** |
||
176 | * |
||
177 | * {@inheritDoc} |
||
178 | * |
||
179 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
180 | */ |
||
181 | protected function createItem($value) { |
||
191 | |||
192 | public function addIconAt($icon, $index) { |
||
199 | |||
200 | public function addItem($item) { |
||
207 | |||
208 | public function asLinks() { |
||
211 | |||
212 | public function asTexts() { |
||
215 | |||
216 | public function setAbsolutePaths($absolutePaths) { |
||
223 | |||
224 | } |
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.