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 FieldAsTrait; |
||
| 27 | |||
| 28 | protected $_searchField; |
||
| 29 | protected $_urls; |
||
| 30 | protected $_pagination; |
||
| 31 | protected $_hasCheckboxes; |
||
| 32 | protected $_compileParts; |
||
| 33 | |||
| 34 | public function run(JsUtils $js){ |
||
| 35 | if($this->_hasCheckboxes && isset($js)){ |
||
| 36 | $js->execOn("change", "#".$this->identifier." [name='selection[]']", " |
||
| 37 | var \$parentCheckbox=\$('#ck-main-ck-{$this->identifier}'),\$checkbox=\$('#{$this->identifier} [name=\"selection[]\"]'),allChecked=true,allUnchecked=true; |
||
| 38 | \$checkbox.each(function() {if($(this).prop('checked')){allUnchecked = false;}else{allChecked = false;}}); |
||
| 39 | if(allChecked) {\$parentCheckbox.checkbox('set checked');}else if(allUnchecked){\$parentCheckbox.checkbox('set unchecked');}else{\$parentCheckbox.checkbox('set indeterminate');}"); |
||
| 40 | } |
||
| 41 | parent::run($js); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
| 45 | parent::__construct($identifier, $model,$modelInstance); |
||
| 46 | $this->_instanceViewer=new InstanceViewerCaption(); |
||
|
|
|||
| 47 | $this->content=["table"=>new HtmlTable($identifier, 0,0)]; |
||
| 48 | $this->_toolbarPosition=PositionInTable::BEFORETABLE; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
| 52 | $this->_instanceViewer->setInstance($this->_model); |
||
| 53 | $captions=$this->_instanceViewer->getCaptions(); |
||
| 54 | |||
| 55 | $table=$this->content["table"]; |
||
| 56 | |||
| 57 | if($this->_hasCheckboxes){ |
||
| 58 | $ck=new HtmlCheckbox("main-ck-".$this->identifier,""); |
||
| 59 | $ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);"); |
||
| 60 | $ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);"); |
||
| 61 | \array_unshift($captions, $ck); |
||
| 62 | } |
||
| 63 | |||
| 64 | $table->setRowCount(0, \sizeof($captions)); |
||
| 65 | $table->setHeaderValues($captions); |
||
| 66 | if(isset($this->_compileParts)) |
||
| 67 | $table->setCompileParts($this->_compileParts); |
||
| 68 | if(isset($this->_searchField)){ |
||
| 69 | if(isset($js)) |
||
| 70 | $this->_searchField->postOn("change", $this->_urls,"{'s':$(this).val()}","-#".$this->identifier." tbody",["preventDefault"=>false]); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->_generateContent($table); |
||
| 74 | |||
| 75 | if($this->_hasCheckboxes){ |
||
| 76 | if($table->hasPart("thead")) |
||
| 77 | $table->getHeader()->getCell(0, 0)->addToProperty("class","no-sort"); |
||
| 78 | } |
||
| 79 | |||
| 80 | if(isset($this->_pagination) && $this->_pagination->getVisible()){ |
||
| 81 | $this->_generatePagination($table); |
||
| 82 | } |
||
| 83 | if(isset($this->_toolbar)){ |
||
| 84 | $this->_setToolbarPosition($table, $captions); |
||
| 85 | } |
||
| 86 | $this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]); |
||
| 87 | return parent::compile($js,$view); |
||
| 88 | } |
||
| 89 | |||
| 90 | private function _generateContent($table){ |
||
| 91 | $objects=$this->_modelInstance; |
||
| 92 | if(isset($this->_pagination)){ |
||
| 93 | $objects=$this->_pagination->getObjects($this->_modelInstance); |
||
| 94 | } |
||
| 95 | InstanceViewerCaption::setIndex(0); |
||
| 96 | $table->fromDatabaseObjects($objects, function($instance){ |
||
| 97 | $this->_instanceViewer->setInstance($instance); |
||
| 98 | InstanceViewerCaption::$index++; |
||
| 99 | $result= $this->_instanceViewer->getValues(); |
||
| 100 | if($this->_hasCheckboxes){ |
||
| 101 | $ck=new HtmlCheckbox("ck-".$this->identifier,""); |
||
| 102 | $field=$ck->getField(); |
||
| 103 | $field->setProperty("value",$this->_instanceViewer->getIdentifier()); |
||
| 104 | $field->setProperty("name", "selection[]"); |
||
| 105 | \array_unshift($result, $ck); |
||
| 106 | } |
||
| 107 | return $result; |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | |||
| 111 | private function _generatePagination($table){ |
||
| 112 | $footer=$table->getFooter(); |
||
| 113 | $footer->mergeCol(); |
||
| 114 | $menu=new HtmlPaginationMenu("pagination-".$this->identifier,$this->_pagination->getPagesNumbers()); |
||
| 115 | $menu->floatRight(); |
||
| 116 | $menu->setActiveItem($this->_pagination->getPage()-1); |
||
| 117 | $footer->setValues($menu); |
||
| 118 | $menu->postOnClick($this->_urls,"{'p':$(this).attr('data-page')}","-#".$this->identifier." tbody",["preventDefault"=>false]); |
||
| 119 | } |
||
| 120 | |||
| 121 | private function _setToolbarPosition($table,$captions){ |
||
| 122 | switch ($this->_toolbarPosition){ |
||
| 123 | case PositionInTable::BEFORETABLE:case PositionInTable::AFTERTABLE: |
||
| 124 | if(isset($this->_compileParts)===false){ |
||
| 125 | $this->content[$this->_toolbarPosition]=$this->_toolbar; |
||
| 126 | } |
||
| 127 | break; |
||
| 128 | case PositionInTable::HEADER:case PositionInTable::FOOTER: case PositionInTable::BODY: |
||
| 129 | $this->addToolbarRow($this->_toolbarPosition,$table, $captions); |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Associates a $callback function after the compilation of the field at $index position |
||
| 136 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 137 | * @param int $index postion of the compiled field |
||
| 138 | * @param callable $callback function called after the field compilation |
||
| 139 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 140 | */ |
||
| 141 | public function afterCompile($index,$callback){ |
||
| 142 | $this->_instanceViewer->afterCompile($index,$callback); |
||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | |||
| 146 | private function addToolbarRow($part,$table,$captions){ |
||
| 147 | $row=$table->getPart($part)->addRow(\sizeof($captions)); |
||
| 148 | $row->mergeCol(); |
||
| 149 | $row->setValues([$this->_toolbar]); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function getInstanceViewer() { |
||
| 153 | return $this->_instanceViewer; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function setInstanceViewer($_instanceViewer) { |
||
| 157 | $this->_instanceViewer=$_instanceViewer; |
||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function setCaptions($captions){ |
||
| 162 | $this->_instanceViewer->setCaptions($captions); |
||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function setFields($fields){ |
||
| 167 | $this->_instanceViewer->setVisibleProperties($fields); |
||
| 168 | return $this; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function addField($field){ |
||
| 172 | $this->_instanceViewer->addField($field); |
||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function insertField($index,$field){ |
||
| 177 | $this->_instanceViewer->insertField($index, $field); |
||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function insertInField($index,$field){ |
||
| 182 | $this->_instanceViewer->insertInField($index, $field); |
||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function setValueFunction($index,$callback){ |
||
| 187 | $this->_instanceViewer->setValueFunction($index, $callback); |
||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function setIdentifierFunction($callback){ |
||
| 192 | $this->_instanceViewer->setIdentifierFunction($callback); |
||
| 193 | return $this; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getHtmlComponent(){ |
||
| 197 | return $this->content["table"]; |
||
| 198 | } |
||
| 199 | |||
| 200 | public function getUrls() { |
||
| 201 | return $this->_urls; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function setUrls($urls) { |
||
| 205 | $this->_urls=$urls; |
||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function paginate($items_per_page=10,$page=1){ |
||
| 210 | $this->_pagination=new Pagination($items_per_page,4,$page); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getHasCheckboxes() { |
||
| 214 | return $this->_hasCheckboxes; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function setHasCheckboxes($_hasCheckboxes) { |
||
| 218 | $this->_hasCheckboxes=$_hasCheckboxes; |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function refresh($compileParts=["tbody"]){ |
||
| 223 | $this->_compileParts=$compileParts; |
||
| 224 | return $this; |
||
| 225 | } |
||
| 226 | /** |
||
| 227 | * @param string $caption |
||
| 228 | * @param callable $callback |
||
| 229 | * @return callable |
||
| 230 | */ |
||
| 231 | private function getFieldButtonCallable($caption,$callback=null){ |
||
| 232 | return $this->getCallable("getFieldButton",[$caption],$callback); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param mixed $object |
||
| 237 | * @param callable $callback |
||
| 238 | * @return callable |
||
| 239 | */ |
||
| 240 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
| 241 | $result=function($instance) use($thisCallback,$parameters,$callback){ |
||
| 242 | $object=call_user_func_array(array($this,$thisCallback), $parameters); |
||
| 243 | if(isset($callback)){ |
||
| 244 | if(\is_callable($callback)){ |
||
| 245 | $callback($object,$instance); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | if($object instanceof HtmlSemDoubleElement){ |
||
| 249 | $object->setProperty("data-ajax",$this->_instanceViewer->getIdentifier()); |
||
| 250 | } |
||
| 251 | return $object; |
||
| 252 | }; |
||
| 253 | return $result; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $caption |
||
| 258 | * @return HtmlButton |
||
| 259 | */ |
||
| 260 | private function getFieldButton($caption){ |
||
| 261 | return new HtmlButton("",$caption); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Inserts a new Button for each row |
||
| 266 | * @param string $caption |
||
| 267 | * @param callable $callback |
||
| 268 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 269 | */ |
||
| 270 | public function addFieldButton($caption,$callback=null){ |
||
| 271 | $this->addField($this->getCallable("getFieldButton",[$caption],$callback)); |
||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Inserts a new Button for each row at col $index |
||
| 277 | * @param int $index |
||
| 278 | * @param string $caption |
||
| 279 | * @param callable $callback |
||
| 280 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 281 | */ |
||
| 282 | public function insertFieldButton($index,$caption,$callback=null){ |
||
| 283 | $this->insertField($index, $this->getFieldButtonCallable($caption,$callback)); |
||
| 284 | return $this; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Inserts a new Button for each row in col at $index |
||
| 289 | * @param int $index |
||
| 290 | * @param string $caption |
||
| 291 | * @param callable $callback |
||
| 292 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 293 | */ |
||
| 294 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
| 295 | $this->insertInField($index, $this->getFieldButtonCallable($caption,$callback)); |
||
| 296 | return $this; |
||
| 297 | } |
||
| 298 | |||
| 299 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
| 300 | $this->addField($this->getCallable("getDefaultButton",[$icon,$class],$callback)); |
||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
| 305 | $this->insertInField($index,$this->getCallable("getDefaultButton",[$icon,$class],$callback)); |
||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | private function getDefaultButton($icon,$class=null){ |
||
| 310 | $bt=$this->getFieldButton(""); |
||
| 311 | $bt->asIcon($icon); |
||
| 312 | if(isset($class)) |
||
| 313 | $bt->addToProperty("class", $class); |
||
| 314 | return $bt; |
||
| 315 | } |
||
| 316 | |||
| 317 | public function addDeleteButton($callback=null){ |
||
| 318 | return $this->addDefaultButton("remove","delete red basic",$callback); |
||
| 319 | } |
||
| 320 | |||
| 321 | public function addEditButton($callback=null){ |
||
| 322 | return $this->addDefaultButton("edit","edit basic",$callback); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function addEditDeleteButtons($callbackEdit=null,$callbackDelete=null){ |
||
| 326 | $this->addEditButton($callbackEdit); |
||
| 327 | $index=$this->_instanceViewer->visiblePropertiesCount()-1; |
||
| 328 | $this->insertDeleteButtonIn($index,$callbackDelete); |
||
| 329 | return $this; |
||
| 330 | } |
||
| 331 | |||
| 332 | public function insertDeleteButtonIn($index,$callback=null){ |
||
| 333 | return $this->insertDefaultButtonIn($index,"remove","delete red basic",$callback); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function insertEditButtonIn($index,$callback=null){ |
||
| 337 | return $this->insertDefaultButtonIn($index,"edit","edit basic",$callback); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function setSelectable(){ |
||
| 341 | $this->content["table"]->setSelectable(); |
||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | public function addSearchInToolbar(){ |
||
| 348 | |||
| 349 | public function getSearchField(){ |
||
| 356 | |||
| 357 | public function setSortable($colIndex=NULL) { |
||
| 361 | |||
| 362 | protected function _getFieldIdentifier($prefix){ |
||
| 363 | return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier(); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 368 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 369 | * @param callable $callback |
||
| 370 | * @return DataTable |
||
| 371 | */ |
||
| 372 | public function onNewRow($callback) { |
||
| 373 | $this->content["table"]->onNewRow($callback); |
||
| 374 | return $this; |
||
| 375 | } |
||
| 376 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..