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:
Complex classes like DataTable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DataTable extends Widget { |
||
26 | use TableTrait; |
||
27 | protected $_searchField; |
||
28 | protected $_urls; |
||
29 | protected $_pagination; |
||
30 | protected $_hasCheckboxes; |
||
31 | protected $_compileParts; |
||
32 | protected $_visibleHover=false; |
||
33 | |||
34 | public function run(JsUtils $js){ |
||
47 | |||
48 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
56 | */ |
||
57 | protected function getTable() { |
||
60 | |||
61 | |||
62 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
96 | |||
97 | private function _generateMainCheckbox(&$captions){ |
||
103 | |||
104 | protected function _generateContent($table){ |
||
127 | |||
128 | private function _generatePagination($table){ |
||
137 | |||
138 | protected function _getFieldName($index){ |
||
141 | |||
142 | protected function _getFieldCaption($index){ |
||
145 | |||
146 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
161 | |||
162 | /** |
||
163 | * Associates a $callback function after the compilation of the field at $index position |
||
164 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
165 | * @param int $index postion of the compiled field |
||
166 | * @param callable $callback function called after the field compilation |
||
167 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
168 | */ |
||
169 | public function afterCompile($index,$callback){ |
||
173 | |||
174 | private function addToolbarRow($part,$table,$captions){ |
||
184 | |||
185 | public function getHtmlComponent(){ |
||
188 | |||
189 | public function getUrls() { |
||
192 | |||
193 | public function setUrls($urls) { |
||
197 | |||
198 | public function paginate($items_per_page=10,$page=1){ |
||
201 | |||
202 | public function getHasCheckboxes() { |
||
205 | |||
206 | public function setHasCheckboxes($_hasCheckboxes) { |
||
210 | |||
211 | public function refresh($compileParts=["tbody"]){ |
||
215 | /** |
||
216 | * @param string $caption |
||
217 | * @param callable $callback |
||
218 | * @param boolean $visibleHover |
||
219 | * @return callable |
||
220 | */ |
||
221 | private function getFieldButtonCallable($caption,$visibleHover=true,$callback=null){ |
||
224 | |||
225 | /** |
||
226 | * @param callable $thisCallback |
||
227 | * @param array $parameters |
||
228 | * @param callable $callback |
||
229 | * @return callable |
||
230 | */ |
||
231 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
250 | |||
251 | /** |
||
252 | * @param string $caption |
||
253 | * @return HtmlButton |
||
254 | */ |
||
255 | private function getFieldButton($caption,$visibleHover=true){ |
||
261 | |||
262 | /** |
||
263 | * Inserts a new Button for each row |
||
264 | * @param string $caption |
||
265 | * @param callable $callback |
||
266 | * @param boolean $visibleHover |
||
267 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
268 | */ |
||
269 | public function addFieldButton($caption,$visibleHover=true,$callback=null){ |
||
273 | |||
274 | /** |
||
275 | * Inserts a new Button for each row at col $index |
||
276 | * @param int $index |
||
277 | * @param string $caption |
||
278 | * @param callable $callback |
||
279 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
280 | */ |
||
281 | public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
285 | |||
286 | /** |
||
287 | * Inserts a new Button for each row in col at $index |
||
288 | * @param int $index |
||
289 | * @param string $caption |
||
290 | * @param callable $callback |
||
291 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
292 | */ |
||
293 | public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
297 | |||
298 | private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null){ |
||
302 | |||
303 | private function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null){ |
||
307 | |||
308 | private function getDefaultButton($icon,$class=null,$visibleHover=true){ |
||
315 | |||
316 | public function addDeleteButton($visibleHover=true,$callback=null){ |
||
319 | |||
320 | public function addEditButton($visibleHover=true,$callback=null){ |
||
323 | |||
324 | public function addEditDeleteButtons($visibleHover=true,$callbackEdit=null,$callbackDelete=null){ |
||
330 | |||
331 | public function insertDeleteButtonIn($index,$visibleHover=true,$callback=null){ |
||
334 | |||
335 | public function insertEditButtonIn($index,$visibleHover=true,$callback=null){ |
||
338 | |||
339 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
342 | |||
343 | public function getSearchField(){ |
||
350 | |||
351 | /** |
||
352 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
353 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
354 | * @param callable $callback |
||
355 | * @return DataTable |
||
356 | */ |
||
357 | public function onNewRow($callback) { |
||
361 | |||
362 | public function asForm(){ |
||
365 | |||
366 | View Code Duplication | public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
|
373 | |||
374 | protected function _visibleOver($element){ |
||
378 | } |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.