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; |
||
| 28 | protected $_searchField; |
||
| 29 | protected $_urls; |
||
| 30 | protected $_pagination; |
||
| 31 | protected $_hasCheckboxes; |
||
| 32 | protected $_compileParts; |
||
| 33 | protected $_hasDelete=false; |
||
| 34 | protected $_hasEdit=false; |
||
| 35 | protected $_visibleHover=false; |
||
| 36 | protected $_hasCheckedMessage=false; |
||
| 37 | protected $_targetSelector; |
||
| 38 | protected $_checkedMessage; |
||
| 39 | |||
| 40 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
| 45 | |||
| 46 | public function run(JsUtils $js){ |
||
| 60 | |||
| 61 | protected function _runCheckboxes(JsUtils $js){ |
||
| 78 | |||
| 79 | protected function _generateBehavior($op,JsUtils $js){ |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 87 | */ |
||
| 88 | protected function getTable() { |
||
| 91 | |||
| 92 | |||
| 93 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
| 132 | |||
| 133 | private function _generateMainCheckbox(&$captions){ |
||
| 142 | |||
| 143 | protected function _generateContent($table){ |
||
| 166 | |||
| 167 | private function _generatePagination($table){ |
||
| 177 | |||
| 178 | protected function _getFieldName($index){ |
||
| 181 | |||
| 182 | protected function _getFieldCaption($index){ |
||
| 185 | |||
| 186 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Associates a $callback function after the compilation of the field at $index position |
||
| 204 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 205 | * @param int $index postion of the compiled field |
||
| 206 | * @param callable $callback function called after the field compilation |
||
| 207 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 208 | */ |
||
| 209 | public function afterCompile($index,$callback){ |
||
| 213 | |||
| 214 | private function addToolbarRow($part,$table,$captions){ |
||
| 224 | |||
| 225 | public function getHtmlComponent(){ |
||
| 228 | |||
| 229 | public function getUrls() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Sets the associative array of urls for refreshing, updating or deleting |
||
| 235 | * @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 |
||
| 236 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 237 | */ |
||
| 238 | public function setUrls($urls) { |
||
| 248 | |||
| 249 | public function paginate($items_per_page=10,$page=1){ |
||
| 252 | |||
| 253 | public function getHasCheckboxes() { |
||
| 256 | |||
| 257 | public function setHasCheckboxes($_hasCheckboxes) { |
||
| 261 | |||
| 262 | public function refresh($compileParts=["tbody"]){ |
||
| 266 | /** |
||
| 267 | * @param string $caption |
||
| 268 | * @param callable $callback |
||
| 269 | * @param boolean $visibleHover |
||
| 270 | * @return callable |
||
| 271 | */ |
||
| 272 | private function getFieldButtonCallable($caption,$visibleHover=true,$callback=null){ |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param callable $thisCallback |
||
| 278 | * @param array $parameters |
||
| 279 | * @param callable $callback |
||
| 280 | * @return callable |
||
| 281 | */ |
||
| 282 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $caption |
||
| 304 | * @return HtmlButton |
||
| 305 | */ |
||
| 306 | private function getFieldButton($caption,$visibleHover=true){ |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Inserts a new Button for each row |
||
| 315 | * @param string $caption |
||
| 316 | * @param callable $callback |
||
| 317 | * @param boolean $visibleHover |
||
| 318 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 319 | */ |
||
| 320 | public function addFieldButton($caption,$visibleHover=true,$callback=null){ |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Inserts a new Button for each row at col $index |
||
| 327 | * @param int $index |
||
| 328 | * @param string $caption |
||
| 329 | * @param callable $callback |
||
| 330 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 331 | */ |
||
| 332 | public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Inserts a new Button for each row in col at $index |
||
| 339 | * @param int $index |
||
| 340 | * @param string $caption |
||
| 341 | * @param callable $callback |
||
| 342 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 343 | */ |
||
| 344 | public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
||
| 348 | |||
| 349 | private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null){ |
||
| 353 | |||
| 354 | private function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null){ |
||
| 358 | |||
| 359 | private function getDefaultButton($icon,$class=null,$visibleHover=true){ |
||
| 366 | |||
| 367 | public function addDeleteButton($visibleHover=true,$generateBehavior=true,$callback=null){ |
||
| 371 | |||
| 372 | public function addEditButton($visibleHover=true,$generateBehavior=true,$callback=null){ |
||
| 376 | |||
| 377 | public function addEditDeleteButtons($visibleHover=true,$generateBehavior=true,$callbackEdit=null,$callbackDelete=null){ |
||
| 383 | |||
| 384 | public function insertDeleteButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){ |
||
| 388 | |||
| 389 | public function insertEditButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){ |
||
| 393 | |||
| 394 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
| 397 | |||
| 398 | public function getSearchField(){ |
||
| 405 | |||
| 406 | /** |
||
| 407 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 408 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 409 | * @param callable $callback |
||
| 410 | * @return DataTable |
||
| 411 | */ |
||
| 412 | public function onNewRow($callback) { |
||
| 416 | |||
| 417 | public function asForm(){ |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Creates a submit button at $index position |
||
| 423 | * @param int $index |
||
| 424 | * @param string $cssStyle |
||
| 425 | * @param string $url |
||
| 426 | * @param string $responseElement |
||
| 427 | * @param array $attributes |
||
| 428 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 429 | */ |
||
| 430 | public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
||
| 439 | |||
| 440 | protected function _visibleOver($element){ |
||
| 444 | |||
| 445 | protected function getTargetSelector() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Sets the response element selector for Edit and Delete request with ajax |
||
| 454 | * @param string $_targetSelector |
||
| 455 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 456 | */ |
||
| 457 | public function setTargetSelector($_targetSelector) { |
||
| 461 | |||
| 462 | protected function getCheckedMessage() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Defines the message displayed when checkboxes are checked or unchecked |
||
| 472 | * with an associative array 0=>no selection,1=>one item selected, other=>{count} items selected |
||
| 473 | * @param array $_checkedMessage |
||
| 474 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 475 | */ |
||
| 476 | public function setCheckedMessage(array $_checkedMessage) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param array $checkedMessage |
||
| 483 | * @param callable $callback |
||
| 484 | */ |
||
| 485 | public function addCountCheckedInToolbar(array $checkedMessage=null,$callback=null){ |
||
| 494 | |||
| 495 | |||
| 496 | } |
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: