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 Widget extends HtmlDoubleElement { |
||
16 | use FieldAsTrait; |
||
17 | |||
18 | /** |
||
19 | * @var string classname |
||
20 | */ |
||
21 | protected $_model; |
||
22 | protected $_modelInstance; |
||
23 | /** |
||
24 | * @var InstanceViewer |
||
25 | */ |
||
26 | protected $_instanceViewer; |
||
27 | /** |
||
28 | * @var HtmlMenu |
||
29 | */ |
||
30 | protected $_toolbar; |
||
31 | /** |
||
32 | * @var PositionInTable |
||
33 | */ |
||
34 | protected $_toolbarPosition; |
||
35 | |||
36 | protected $_edition; |
||
37 | |||
38 | |||
39 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
46 | |||
47 | protected function _init($instanceViewer,$contentKey,$content,$edition){ |
||
53 | |||
54 | protected function _getFieldIdentifier($prefix){ |
||
57 | |||
58 | abstract protected function _setToolbarPosition($table,$captions=NULL); |
||
59 | |||
60 | public function show($modelInstance){ |
||
63 | |||
64 | public function getModel() { |
||
67 | |||
68 | public function setModel($_model) { |
||
72 | |||
73 | public function getInstanceViewer() { |
||
76 | |||
77 | public function setInstanceViewer($_instanceViewer) { |
||
81 | |||
82 | abstract public function getHtmlComponent(); |
||
83 | |||
84 | public function setColor($color){ |
||
87 | |||
88 | |||
89 | public function setCaptions($captions){ |
||
93 | |||
94 | public function setFields($fields){ |
||
98 | |||
99 | public function addField($field){ |
||
103 | |||
104 | public function insertField($index,$field){ |
||
108 | |||
109 | public function insertInField($index,$field){ |
||
113 | |||
114 | public function setValueFunction($index,$callback){ |
||
118 | |||
119 | public function setIdentifierFunction($callback){ |
||
123 | |||
124 | /** |
||
125 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
126 | */ |
||
127 | public function getToolbar(){ |
||
134 | |||
135 | /** |
||
136 | * Adds a new element in toolbar |
||
137 | * @param mixed $element |
||
138 | * @param callable $callback function to call on $element |
||
139 | * @return \Ajax\common\html\HtmlDoubleElement |
||
140 | */ |
||
141 | public function addInToolbar($element,$callback=NULL){ |
||
150 | |||
151 | /** |
||
152 | * @param string $caption |
||
153 | * @param string $icon |
||
154 | * @param callable $callback function($element) |
||
155 | * @return \Ajax\common\html\HtmlDoubleElement |
||
156 | */ |
||
157 | public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){ |
||
163 | |||
164 | /** |
||
165 | * @param array $items |
||
166 | * @param callable $callback function($element) |
||
167 | * @return \Ajax\common\Widget |
||
168 | */ |
||
169 | public function addItemsInToolbar(array $items,$callback=NULL){ |
||
181 | |||
182 | /** |
||
183 | * @param string $value |
||
184 | * @param array|NULL $items |
||
185 | * @param callable $callback function($element) |
||
186 | * @return \Ajax\common\html\HtmlDoubleElement |
||
187 | */ |
||
188 | View Code Duplication | public function addDropdownInToolbar($value,$items=NULL,$callback=NULL){ |
|
195 | |||
196 | /** |
||
197 | * @param unknown $caption |
||
198 | * @param callable $callback function($element) |
||
199 | * @return \Ajax\common\html\HtmlDoubleElement |
||
200 | */ |
||
201 | public function addButtonInToolbar($caption,$callback=NULL){ |
||
205 | |||
206 | /** |
||
207 | * @param array $captions |
||
208 | * @param boolean $asIcon |
||
209 | * @param callable $callback function($element) |
||
210 | * @return \Ajax\common\html\HtmlDoubleElement |
||
211 | */ |
||
212 | public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){ |
||
216 | |||
217 | /** |
||
218 | * @param string $caption |
||
219 | * @param string $icon |
||
220 | * @param boolean $before |
||
221 | * @param boolean $labeled |
||
222 | * @return \Ajax\common\html\HtmlDoubleElement |
||
223 | */ |
||
224 | public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){ |
||
229 | |||
230 | /** |
||
231 | * Defines a callback function to call for modifying captions |
||
232 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
233 | * @param callable $captionCallback |
||
234 | * @return Widget |
||
235 | */ |
||
236 | public function setCaptionCallback($captionCallback) { |
||
240 | |||
241 | /** |
||
242 | * Makes the input fields editable |
||
243 | * @param boolean $_edition |
||
244 | * @return \Ajax\common\Widget |
||
245 | */ |
||
246 | public function setEdition($_edition=true) { |
||
250 | |||
251 | /** |
||
252 | * Defines the default function which displays fields value |
||
253 | * @param callable $defaultValueFunction |
||
254 | * @return \Ajax\common\Widget |
||
255 | */ |
||
256 | public function setDefaultValueFunction($defaultValueFunction){ |
||
260 | |||
261 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.