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 |
||
| 26 | class DataTable extends Widget { |
||
| 27 | use TableTrait,DataTableFieldAsTrait,HasCheckboxesTrait,BaseTrait; |
||
| 28 | protected $_searchField; |
||
| 29 | protected $_urls; |
||
| 30 | protected $_pagination; |
||
| 31 | protected $_compileParts; |
||
| 32 | protected $_deleteBehavior; |
||
| 33 | protected $_editBehavior; |
||
| 34 | protected $_visibleHover=false; |
||
| 35 | protected $_targetSelector; |
||
| 36 | protected $_refreshSelector; |
||
| 37 | protected $_emptyMessage; |
||
| 38 | protected $_json; |
||
| 39 | protected $_rowClass=""; |
||
| 40 | protected $_sortable; |
||
| 41 | protected $_hiddenColumns; |
||
| 42 | protected $_colWidths; |
||
| 43 | |||
| 44 | |||
| 45 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
| 52 | |||
| 53 | public function run(JsUtils $js){ |
||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | protected function _generateBehavior($op,$params,JsUtils $js){ |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritDoc} |
||
| 79 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 80 | */ |
||
| 81 | protected function getTable() { |
||
| 84 | |||
| 85 | |||
| 86 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
| 111 | |||
| 112 | protected function compileExtraElements($table,$captions,JsUtils $js=NULL){ |
||
| 127 | |||
| 128 | protected function _applyStyleAttributes($table){ |
||
| 137 | |||
| 138 | protected function _hideColumns(){ |
||
| 144 | |||
| 145 | protected function _generateHeader(HtmlTable $table,$captions){ |
||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | protected function _generateContent($table){ |
||
| 169 | |||
| 170 | protected function _generateRow($instance,&$table,$checkedClass=null){ |
||
| 194 | |||
| 195 | protected function _generatePagination($table,$js=NULL){ |
||
| 208 | |||
| 209 | protected function _associatePaginationBehavior(HtmlMenu $menu,JsUtils $js=NULL){ |
||
| 214 | |||
| 215 | protected function _getFieldName($index){ |
||
| 221 | |||
| 222 | protected function _getFieldCaption($index){ |
||
| 225 | |||
| 226 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Associates a $callback function after the compilation of the field at $index position |
||
| 244 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 245 | * @param int $index postion of the compiled field |
||
| 246 | * @param callable $callback function called after the field compilation |
||
| 247 | * @return DataTable |
||
| 248 | */ |
||
| 249 | public function afterCompile($index,$callback){ |
||
| 253 | |||
| 254 | private function addToolbarRow($part,$table,$captions){ |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritDoc} |
||
| 267 | * @see Widget::getHtmlComponent() |
||
| 268 | * @return HtmlTable |
||
| 269 | */ |
||
| 270 | public function getHtmlComponent(){ |
||
| 273 | |||
| 274 | public function getUrls() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Sets the associative array of urls for refreshing, updating or deleting |
||
| 280 | * think of defining the update zone with the setTargetSelector method |
||
| 281 | * @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 |
||
| 282 | * @return DataTable |
||
| 283 | */ |
||
| 284 | public function setUrls($urls) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
| 297 | * @param number $page the active page number |
||
| 298 | * @param number $total_rowcount the total number of items |
||
| 299 | * @param number $items_per_page The number of items per page |
||
| 300 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
| 301 | * @return DataTable |
||
| 302 | */ |
||
| 303 | public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){ |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
| 310 | * @param number $page the active page number |
||
| 311 | * @param number $items_per_page The number of items per page |
||
| 312 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
| 313 | * @return DataTable |
||
| 314 | */ |
||
| 315 | public function autoPaginate($page=1,$items_per_page=10,$pages_visibles=4){ |
||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $compileParts |
||
| 324 | * @return DataTable |
||
| 325 | */ |
||
| 326 | public function refresh($compileParts=["tbody"]){ |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Adds a search input in toolbar |
||
| 334 | * @param string $position |
||
| 335 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 336 | */ |
||
| 337 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
| 340 | |||
| 341 | public function getSearchField(){ |
||
| 348 | |||
| 349 | /** |
||
| 350 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 351 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 352 | * @param callable $callback |
||
| 353 | * @return DataTable |
||
| 354 | */ |
||
| 355 | public function onNewRow($callback) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns a form corresponding to the Datatable |
||
| 362 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
| 363 | */ |
||
| 364 | public function asForm(){ |
||
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | protected function getTargetSelector($op) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Sets the response element selector for Edit and Delete request with ajax |
||
| 379 | * @param string|array $_targetSelector string or associative array ["edit"=>"edit_selector","delete"=>"delete_selector"] |
||
| 380 | * @return DataTable |
||
| 381 | */ |
||
| 382 | public function setTargetSelector($_targetSelector) { |
||
| 389 | |||
| 390 | public function getRefreshSelector() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $_refreshSelector |
||
| 398 | * @return DataTable |
||
| 399 | */ |
||
| 400 | public function setRefreshSelector($_refreshSelector) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritDoc} |
||
| 407 | * @see \Ajax\common\Widget::show() |
||
| 408 | */ |
||
| 409 | public function show($modelInstance){ |
||
| 416 | |||
| 417 | public function getRowClass() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Sets the default row class (tr class) |
||
| 423 | * @param string $_rowClass |
||
| 424 | * @return DataTable |
||
| 425 | */ |
||
| 426 | public function setRowClass($_rowClass) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Sets the message displayed when there is no record |
||
| 433 | * @param mixed $_emptyMessage |
||
| 434 | * @return DataTable |
||
| 435 | */ |
||
| 436 | public function setEmptyMessage($_emptyMessage) { |
||
| 440 | |||
| 441 | public function setSortable($colIndex=NULL) { |
||
| 445 | |||
| 446 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
| 450 | |||
| 451 | public function hideColumn($colIndex){ |
||
| 457 | |||
| 458 | public function setColWidth($colIndex,$width){ |
||
| 466 | |||
| 467 | public function setColAlignment($colIndex,$alignment){ |
||
| 471 | } |
||
| 472 |
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: