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,DataTableFieldAsTrait,HasCheckboxesTrait; |
||
| 27 | protected $_searchField; |
||
| 28 | protected $_urls; |
||
| 29 | protected $_pagination; |
||
| 30 | protected $_compileParts; |
||
| 31 | protected $_deleteBehavior; |
||
| 32 | protected $_editBehavior; |
||
| 33 | protected $_visibleHover=false; |
||
| 34 | protected $_targetSelector; |
||
| 35 | protected $_refreshSelector; |
||
| 36 | protected $_emptyMessage; |
||
| 37 | protected $_json; |
||
| 38 | protected $_rowClass=""; |
||
| 39 | protected $_sortable; |
||
| 40 | |||
| 41 | |||
| 42 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
| 49 | |||
| 50 | public function run(JsUtils $js){ |
||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | protected function _generateBehavior($op,$params,JsUtils $js){ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritDoc} |
||
| 76 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 77 | */ |
||
| 78 | protected function getTable() { |
||
| 81 | |||
| 82 | |||
| 83 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
| 125 | |||
| 126 | protected function _generateHeader(HtmlTable $table,$captions){ |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | protected function _generateContent($table){ |
||
| 150 | |||
| 151 | protected function _generateRow($instance,&$table,$checkedClass=null){ |
||
| 173 | |||
| 174 | protected function _generatePagination($table,$js=NULL){ |
||
| 187 | |||
| 188 | protected function _associatePaginationBehavior(HtmlMenu $menu,JsUtils $js=NULL){ |
||
| 193 | |||
| 194 | protected function _getFieldName($index){ |
||
| 197 | |||
| 198 | protected function _getFieldCaption($index){ |
||
| 201 | |||
| 202 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Associates a $callback function after the compilation of the field at $index position |
||
| 220 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 221 | * @param int $index postion of the compiled field |
||
| 222 | * @param callable $callback function called after the field compilation |
||
| 223 | * @return DataTable |
||
| 224 | */ |
||
| 225 | public function afterCompile($index,$callback){ |
||
| 229 | |||
| 230 | private function addToolbarRow($part,$table,$captions){ |
||
| 240 | |||
| 241 | public function getHtmlComponent(){ |
||
| 244 | |||
| 245 | public function getUrls() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Sets the associative array of urls for refreshing, updating or deleting |
||
| 251 | * @param string|array $urls associative array with keys refresh: for refreshing with search field or pagination, edit : for updating a row, delete: for deleting a row |
||
| 252 | * @return DataTable |
||
| 253 | */ |
||
| 254 | public function setUrls($urls) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
| 267 | * @param number $page the active page number |
||
| 268 | * @param number $total_rowcount the total number of items |
||
| 269 | * @param number $items_per_page The number of items per page |
||
| 270 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
| 271 | * @return DataTable |
||
| 272 | */ |
||
| 273 | public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
| 280 | * @param number $page the active page number |
||
| 281 | * @param number $items_per_page The number of items per page |
||
| 282 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
| 283 | * @return DataTable |
||
| 284 | */ |
||
| 285 | public function autoPaginate($page=1,$items_per_page=10,$pages_visibles=4){ |
||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * @param array $compileParts |
||
| 294 | * @return DataTable |
||
| 295 | */ |
||
| 296 | public function refresh($compileParts=["tbody"]){ |
||
| 300 | |||
| 301 | |||
| 302 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
| 305 | |||
| 306 | public function getSearchField(){ |
||
| 313 | |||
| 314 | /** |
||
| 315 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 316 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 317 | * @param callable $callback |
||
| 318 | * @return DataTable |
||
| 319 | */ |
||
| 320 | public function onNewRow($callback) { |
||
| 324 | |||
| 325 | public function asForm(){ |
||
| 328 | |||
| 329 | |||
| 330 | |||
| 331 | protected function getTargetSelector() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Sets the response element selector for Edit and Delete request with ajax |
||
| 340 | * @param string $_targetSelector |
||
| 341 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 342 | */ |
||
| 343 | public function setTargetSelector($_targetSelector) { |
||
| 347 | |||
| 348 | public function getRefreshSelector() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $_refreshSelector |
||
| 356 | * @return DataTable |
||
| 357 | */ |
||
| 358 | public function setRefreshSelector($_refreshSelector) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritDoc} |
||
| 365 | * @see \Ajax\common\Widget::show() |
||
| 366 | */ |
||
| 367 | public function show($modelInstance){ |
||
| 374 | |||
| 375 | public function getRowClass() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Sets the default row class (tr class) |
||
| 381 | * @param string $_rowClass |
||
| 382 | * @return DataTable |
||
| 383 | */ |
||
| 384 | public function setRowClass($_rowClass) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Sets the message displayed when there is no record |
||
| 391 | * @param mixed $_emptyMessage |
||
| 392 | * @return DataTable |
||
| 393 | */ |
||
| 394 | public function setEmptyMessage($_emptyMessage) { |
||
| 398 | |||
| 399 | public function setSortable($colIndex=NULL) { |
||
| 403 | |||
| 404 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
| 408 | |||
| 409 | public function hideColumn($colIndex){ |
||
| 413 | |||
| 414 | } |
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: