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 | |||
| 33 | public function run(JsUtils $js){ |
||
| 42 | |||
| 43 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
| 44 | parent::__construct($identifier, $model,$modelInstance); |
||
| 45 | $this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,0), false); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritDoc} |
||
| 50 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 51 | */ |
||
| 52 | protected function getTable() { |
||
| 53 | return $this->content["table"]; |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
| 58 | $this->_instanceViewer->setInstance($this->_model); |
||
| 59 | $captions=$this->_instanceViewer->getCaptions(); |
||
| 60 | |||
| 61 | $table=$this->content["table"]; |
||
| 62 | |||
| 63 | if($this->_hasCheckboxes){ |
||
| 64 | $this->_generateMainCheckbox($captions); |
||
| 65 | } |
||
| 66 | |||
| 67 | $table->setRowCount(0, \sizeof($captions)); |
||
| 68 | $table->setHeaderValues($captions); |
||
| 69 | if(isset($this->_compileParts)) |
||
| 70 | $table->setCompileParts($this->_compileParts); |
||
| 71 | if(isset($this->_searchField) && isset($js)){ |
||
| 72 | $this->_searchField->postOn("change", $this->_urls,"{'s':$(this).val()}","#".$this->identifier." tbody",["preventDefault"=>false,"jqueryDone"=>"replaceWith"]); |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->_generateContent($table); |
||
| 76 | |||
| 77 | if($this->_hasCheckboxes && $table->hasPart("thead")){ |
||
| 78 | $table->getHeader()->getCell(0, 0)->addToProperty("class","no-sort"); |
||
| 79 | } |
||
| 80 | |||
| 81 | if(isset($this->_pagination) && $this->_pagination->getVisible()){ |
||
| 82 | $this->_generatePagination($table); |
||
| 83 | } |
||
| 84 | if(isset($this->_toolbar)){ |
||
| 85 | $this->_setToolbarPosition($table, $captions); |
||
| 86 | } |
||
| 87 | $this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]); |
||
| 88 | return parent::compile($js,$view); |
||
| 89 | } |
||
| 90 | |||
| 91 | private function _generateMainCheckbox(&$captions){ |
||
| 92 | $ck=new HtmlCheckbox("main-ck-".$this->identifier,""); |
||
| 93 | $ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);"); |
||
| 94 | $ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);"); |
||
| 95 | \array_unshift($captions, $ck); |
||
| 96 | } |
||
| 97 | |||
| 98 | protected function _generateContent($table){ |
||
| 118 | |||
| 119 | private function _generatePagination($table){ |
||
| 128 | |||
| 129 | protected function _getFieldName($index){ |
||
| 130 | return parent::_getFieldName($index)."[]"; |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function _getFieldCaption($index){ |
||
| 136 | |||
| 137 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Associates a $callback function after the compilation of the field at $index position |
||
| 155 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 156 | * @param int $index postion of the compiled field |
||
| 157 | * @param callable $callback function called after the field compilation |
||
| 158 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 159 | */ |
||
| 160 | public function afterCompile($index,$callback){ |
||
| 164 | |||
| 165 | private function addToolbarRow($part,$table,$captions){ |
||
| 175 | |||
| 176 | public function getHtmlComponent(){ |
||
| 179 | |||
| 180 | public function getUrls() { |
||
| 183 | |||
| 184 | public function setUrls($urls) { |
||
| 188 | |||
| 189 | public function paginate($items_per_page=10,$page=1){ |
||
| 192 | |||
| 193 | public function getHasCheckboxes() { |
||
| 196 | |||
| 197 | public function setHasCheckboxes($_hasCheckboxes) { |
||
| 201 | |||
| 202 | public function refresh($compileParts=["tbody"]){ |
||
| 206 | /** |
||
| 207 | * @param string $caption |
||
| 208 | * @param callable $callback |
||
| 209 | * @return callable |
||
| 210 | */ |
||
| 211 | private function getFieldButtonCallable($caption,$callback=null){ |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param callable $thisCallback |
||
| 217 | * @param array $parameters |
||
| 218 | * @param callable $callback |
||
| 219 | * @return callable |
||
| 220 | */ |
||
| 221 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $caption |
||
| 239 | * @return HtmlButton |
||
| 240 | */ |
||
| 241 | private function getFieldButton($caption){ |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Inserts a new Button for each row |
||
| 247 | * @param string $caption |
||
| 248 | * @param callable $callback |
||
| 249 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 250 | */ |
||
| 251 | public function addFieldButton($caption,$callback=null){ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Inserts a new Button for each row at col $index |
||
| 258 | * @param int $index |
||
| 259 | * @param string $caption |
||
| 260 | * @param callable $callback |
||
| 261 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 262 | */ |
||
| 263 | public function insertFieldButton($index,$caption,$callback=null){ |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Inserts a new Button for each row in col at $index |
||
| 270 | * @param int $index |
||
| 271 | * @param string $caption |
||
| 272 | * @param callable $callback |
||
| 273 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 274 | */ |
||
| 275 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
| 279 | |||
| 280 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
| 284 | |||
| 285 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
| 289 | |||
| 290 | private function getDefaultButton($icon,$class=null){ |
||
| 297 | |||
| 298 | public function addDeleteButton($callback=null){ |
||
| 301 | |||
| 302 | public function addEditButton($callback=null){ |
||
| 305 | |||
| 306 | public function addEditDeleteButtons($callbackEdit=null,$callbackDelete=null){ |
||
| 312 | |||
| 313 | public function insertDeleteButtonIn($index,$callback=null){ |
||
| 316 | |||
| 317 | public function insertEditButtonIn($index,$callback=null){ |
||
| 320 | |||
| 321 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
| 324 | |||
| 325 | public function getSearchField(){ |
||
| 332 | |||
| 333 | /** |
||
| 334 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 335 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 336 | * @param callable $callback |
||
| 337 | * @return DataTable |
||
| 338 | */ |
||
| 339 | public function onNewRow($callback) { |
||
| 343 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: