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) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritDoc} |
||
| 50 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 51 | */ |
||
| 52 | protected function getTable() { |
||
| 55 | |||
| 56 | |||
| 57 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
| 91 | |||
| 92 | private function _generateMainCheckbox(&$captions){ |
||
| 98 | |||
| 99 | protected function _generateContent($table){ |
||
| 119 | |||
| 120 | private function _generatePagination($table){ |
||
| 129 | |||
| 130 | protected function _getFieldName($index){ |
||
| 133 | |||
| 134 | protected function _getFieldCaption($index){ |
||
| 137 | |||
| 138 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Associates a $callback function after the compilation of the field at $index position |
||
| 156 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 157 | * @param int $index postion of the compiled field |
||
| 158 | * @param callable $callback function called after the field compilation |
||
| 159 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 160 | */ |
||
| 161 | public function afterCompile($index,$callback){ |
||
| 165 | |||
| 166 | private function addToolbarRow($part,$table,$captions){ |
||
| 176 | |||
| 177 | public function getHtmlComponent(){ |
||
| 180 | |||
| 181 | public function getUrls() { |
||
| 184 | |||
| 185 | public function setUrls($urls) { |
||
| 189 | |||
| 190 | public function paginate($items_per_page=10,$page=1){ |
||
| 193 | |||
| 194 | public function getHasCheckboxes() { |
||
| 197 | |||
| 198 | public function setHasCheckboxes($_hasCheckboxes) { |
||
| 202 | |||
| 203 | public function refresh($compileParts=["tbody"]){ |
||
| 207 | /** |
||
| 208 | * @param string $caption |
||
| 209 | * @param callable $callback |
||
| 210 | * @return callable |
||
| 211 | */ |
||
| 212 | private function getFieldButtonCallable($caption,$callback=null){ |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param callable $thisCallback |
||
| 218 | * @param array $parameters |
||
| 219 | * @param callable $callback |
||
| 220 | * @return callable |
||
| 221 | */ |
||
| 222 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $caption |
||
| 240 | * @return HtmlButton |
||
| 241 | */ |
||
| 242 | private function getFieldButton($caption){ |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Inserts a new Button for each row |
||
| 248 | * @param string $caption |
||
| 249 | * @param callable $callback |
||
| 250 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 251 | */ |
||
| 252 | public function addFieldButton($caption,$callback=null){ |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Inserts a new Button for each row at col $index |
||
| 259 | * @param int $index |
||
| 260 | * @param string $caption |
||
| 261 | * @param callable $callback |
||
| 262 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 263 | */ |
||
| 264 | public function insertFieldButton($index,$caption,$callback=null){ |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Inserts a new Button for each row in col at $index |
||
| 271 | * @param int $index |
||
| 272 | * @param string $caption |
||
| 273 | * @param callable $callback |
||
| 274 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
| 275 | */ |
||
| 276 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
| 280 | |||
| 281 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
| 285 | |||
| 286 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
| 290 | |||
| 291 | private function getDefaultButton($icon,$class=null){ |
||
| 298 | |||
| 299 | public function addDeleteButton($callback=null){ |
||
| 302 | |||
| 303 | public function addEditButton($callback=null){ |
||
| 306 | |||
| 307 | public function addEditDeleteButtons($callbackEdit=null,$callbackDelete=null){ |
||
| 313 | |||
| 314 | public function insertDeleteButtonIn($index,$callback=null){ |
||
| 317 | |||
| 318 | public function insertEditButtonIn($index,$callback=null){ |
||
| 321 | |||
| 322 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
| 325 | |||
| 326 | public function getSearchField(){ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 336 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 337 | * @param callable $callback |
||
| 338 | * @return DataTable |
||
| 339 | */ |
||
| 340 | public function onNewRow($callback) { |
||
| 344 | |||
| 345 | public function asForm(){ |
||
| 348 | } |
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: