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 |
||
23 | class DataTable extends Widget { |
||
24 | use TableTrait,DataTableFieldAsTrait,HasCheckboxesTrait; |
||
25 | protected $_searchField; |
||
26 | protected $_urls; |
||
27 | protected $_pagination; |
||
28 | protected $_compileParts; |
||
29 | protected $_deleteBehavior; |
||
30 | protected $_editBehavior; |
||
31 | protected $_visibleHover=false; |
||
32 | protected $_targetSelector; |
||
33 | protected $_refreshSelector; |
||
34 | |||
35 | |||
36 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
37 | parent::__construct($identifier, $model,$modelInstance); |
||
38 | $this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,0), false); |
||
39 | $this->_urls=[]; |
||
40 | } |
||
41 | |||
42 | public function run(JsUtils $js){ |
||
43 | if($this->_hasCheckboxes && isset($js)){ |
||
44 | $this->_runCheckboxes($js); |
||
45 | } |
||
46 | if($this->_visibleHover){ |
||
47 | $js->execOn("mouseover", "#".$this->identifier." tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'visible');",["preventDefault"=>false,"stopPropagation"=>true]); |
||
48 | $js->execOn("mouseout", "#".$this->identifier." tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'hidden');",["preventDefault"=>false,"stopPropagation"=>true]); |
||
49 | } |
||
50 | if(\is_array($this->_deleteBehavior)) |
||
51 | $this->_generateBehavior("delete",$this->_deleteBehavior, $js); |
||
52 | if(\is_array($this->_editBehavior)) |
||
53 | $this->_generateBehavior("edit",$this->_editBehavior,$js); |
||
54 | return parent::run($js); |
||
55 | } |
||
56 | |||
57 | |||
58 | |||
59 | protected function _generateBehavior($op,$params,JsUtils $js){ |
||
60 | if(isset($this->_urls[$op])){ |
||
61 | $params=\array_merge($params,["attr"=>"data-ajax"]); |
||
62 | $js->getOnClick("#".$this->identifier." ._".$op, $this->_urls[$op],$this->getTargetSelector(),$params); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
69 | */ |
||
70 | protected function getTable() { |
||
73 | |||
74 | |||
75 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
114 | |||
115 | |||
116 | |||
117 | protected function _generateContent($table){ |
||
140 | |||
141 | private function _generatePagination($table){ |
||
151 | |||
152 | protected function _getFieldName($index){ |
||
155 | |||
156 | protected function _getFieldCaption($index){ |
||
159 | |||
160 | protected function _setToolbarPosition($table,$captions=NULL){ |
||
175 | |||
176 | /** |
||
177 | * Associates a $callback function after the compilation of the field at $index position |
||
178 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
179 | * @param int $index postion of the compiled field |
||
180 | * @param callable $callback function called after the field compilation |
||
181 | * @return DataTable |
||
182 | */ |
||
183 | public function afterCompile($index,$callback){ |
||
187 | |||
188 | private function addToolbarRow($part,$table,$captions){ |
||
198 | |||
199 | public function getHtmlComponent(){ |
||
202 | |||
203 | public function getUrls() { |
||
206 | |||
207 | /** |
||
208 | * Sets the associative array of urls for refreshing, updating or deleting |
||
209 | * @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 |
||
210 | * @return DataTable |
||
211 | */ |
||
212 | public function setUrls($urls) { |
||
222 | |||
223 | /** |
||
224 | * Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
225 | * @param number $page the active page number |
||
226 | * @param number $total_rowcount the total number of items |
||
227 | * @param number $items_per_page The number of items per page |
||
228 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
229 | * @return DataTable |
||
230 | */ |
||
231 | public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=4){ |
||
235 | |||
236 | /** |
||
237 | * Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
238 | * @param number $page the active page number |
||
239 | * @param number $items_per_page The number of items per page |
||
240 | * @param number $pages_visibles The number of visible pages in the Pagination component |
||
241 | * @return DataTable |
||
242 | */ |
||
243 | public function autoPaginate($page=1,$items_per_page=10,$pages_visibles=4){ |
||
247 | |||
248 | |||
249 | |||
250 | /** |
||
251 | * @param array $compileParts |
||
252 | * @return DataTable |
||
253 | */ |
||
254 | public function refresh($compileParts=["tbody"]){ |
||
258 | |||
259 | |||
260 | public function addSearchInToolbar($position=Direction::RIGHT){ |
||
263 | |||
264 | public function getSearchField(){ |
||
271 | |||
272 | /** |
||
273 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
274 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
275 | * @param callable $callback |
||
276 | * @return DataTable |
||
277 | */ |
||
278 | public function onNewRow($callback) { |
||
282 | |||
283 | public function asForm(){ |
||
286 | |||
287 | |||
288 | |||
289 | protected function getTargetSelector() { |
||
295 | |||
296 | /** |
||
297 | * Sets the response element selector for Edit and Delete request with ajax |
||
298 | * @param string $_targetSelector |
||
299 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
300 | */ |
||
301 | public function setTargetSelector($_targetSelector) { |
||
305 | |||
306 | public function getRefreshSelector() { |
||
311 | |||
312 | public function setRefreshSelector($_refreshSelector) { |
||
316 | |||
317 | } |
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: