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 |
||
22 | class DataTable extends Widget { |
||
23 | use FieldAsTrait; |
||
24 | |||
25 | protected $_searchField; |
||
26 | protected $_urls; |
||
27 | protected $_pagination; |
||
28 | protected $_hasCheckboxes; |
||
29 | protected $_toolbar; |
||
30 | protected $_compileParts; |
||
31 | protected $_toolbarPosition; |
||
32 | |||
33 | public function run(JsUtils $js){ |
||
42 | |||
43 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
49 | |||
50 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
88 | |||
89 | private function _generateContent($table){ |
||
108 | |||
109 | private function _generatePagination($table){ |
||
118 | |||
119 | private function _setToolbarPosition($table,$captions){ |
||
131 | |||
132 | /** |
||
133 | * Associates a $callback function after the compilation of the field at $index position |
||
134 | * The $callback function can take the following arguments : $field=>the compiled field, $index: the field position, $instance : the active instance of the object |
||
135 | * @param int $index postion of the compiled field |
||
136 | * @param callable $callback function called after the field compilation |
||
137 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
138 | */ |
||
139 | public function afterCompile($index,$callback){ |
||
143 | |||
144 | private function addToolbarRow($part,$table,$captions){ |
||
149 | |||
150 | public function getInstanceViewer() { |
||
153 | |||
154 | public function setInstanceViewer($_instanceViewer) { |
||
158 | |||
159 | public function setCaptions($captions){ |
||
163 | |||
164 | public function setFields($fields){ |
||
168 | |||
169 | public function addField($field){ |
||
173 | |||
174 | public function insertField($index,$field){ |
||
178 | |||
179 | public function insertInField($index,$field){ |
||
183 | |||
184 | public function setValueFunction($index,$callback){ |
||
188 | |||
189 | public function setIdentifierFunction($callback){ |
||
193 | |||
194 | public function getHtmlComponent(){ |
||
197 | |||
198 | public function getUrls() { |
||
201 | |||
202 | public function setUrls($urls) { |
||
206 | |||
207 | public function paginate($items_per_page=10,$page=1){ |
||
210 | |||
211 | public function getHasCheckboxes() { |
||
214 | |||
215 | public function setHasCheckboxes($_hasCheckboxes) { |
||
219 | |||
220 | public function refresh($compileParts=["tbody"]){ |
||
224 | /** |
||
225 | * @param string $caption |
||
226 | * @param callable $callback |
||
227 | * @return callable |
||
228 | */ |
||
229 | private function getFieldButtonCallable($caption,$callback=null){ |
||
232 | |||
233 | /** |
||
234 | * @param mixed $object |
||
235 | * @param callable $callback |
||
236 | * @return callable |
||
237 | */ |
||
238 | private function getCallable($object,$callback=null){ |
||
249 | |||
250 | /** |
||
251 | * @param string $caption |
||
252 | * @return HtmlButton |
||
253 | */ |
||
254 | private function getFieldButton($caption){ |
||
259 | |||
260 | /** |
||
261 | * Inserts a new Button for each row |
||
262 | * @param string $caption |
||
263 | * @param callable $callback |
||
264 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
265 | */ |
||
266 | public function addFieldButton($caption,$callback=null){ |
||
270 | |||
271 | /** |
||
272 | * Inserts a new Button for each row at col $index |
||
273 | * @param int $index |
||
274 | * @param string $caption |
||
275 | * @param callable $callback |
||
276 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
277 | */ |
||
278 | public function insertFieldButton($index,$caption,$callback=null){ |
||
282 | |||
283 | /** |
||
284 | * Inserts a new Button for each row in col at $index |
||
285 | * @param int $index |
||
286 | * @param string $caption |
||
287 | * @param callable $callback |
||
288 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
289 | */ |
||
290 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
294 | |||
295 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
300 | |||
301 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
306 | |||
307 | private function getDefaultButton($icon,$class=null){ |
||
314 | |||
315 | public function addDeleteButton($callback=null){ |
||
318 | |||
319 | public function addEditButton($callback=null){ |
||
322 | |||
323 | public function addEditDeleteButtons($callbackEdit=null,$callbackDelete=null){ |
||
329 | |||
330 | public function insertDeleteButtonIn($index,$callback=null){ |
||
333 | |||
334 | public function insertEditButtonIn($index,$callback=null){ |
||
337 | |||
338 | public function setSelectable(){ |
||
342 | |||
343 | /** |
||
344 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
345 | */ |
||
346 | public function getToolbar(){ |
||
353 | |||
354 | /** |
||
355 | * @param unknown $element |
||
356 | * @return \Ajax\common\html\HtmlDoubleElement |
||
357 | */ |
||
358 | public function addInToolbar($element){ |
||
362 | |||
363 | public function addItemInToolbar($caption,$icon=NULL){ |
||
368 | |||
369 | public function addButtonInToolbar($caption){ |
||
373 | |||
374 | public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){ |
||
379 | |||
380 | |||
381 | public function addSearchInToolbar(){ |
||
384 | |||
385 | public function getSearchField(){ |
||
392 | |||
393 | public function setSortable($colIndex=NULL) { |
||
397 | |||
398 | protected function _getFieldIdentifier($prefix){ |
||
401 | } |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.