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 FieldAsTrait; |
||
25 | |||
26 | protected $_searchField; |
||
27 | protected $_urls; |
||
28 | protected $_pagination; |
||
29 | protected $_hasCheckboxes; |
||
30 | protected $_toolbar; |
||
31 | protected $_compileParts; |
||
32 | protected $_toolbarPosition; |
||
33 | |||
34 | public function run(JsUtils $js){ |
||
43 | |||
44 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
50 | |||
51 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
89 | |||
90 | private function _generateContent($table){ |
||
110 | |||
111 | private function _generatePagination($table){ |
||
120 | |||
121 | private function _setToolbarPosition($table,$captions){ |
||
133 | |||
134 | /** |
||
135 | * Associates a $callback function after the compilation of the field at $index position |
||
136 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
137 | * @param int $index postion of the compiled field |
||
138 | * @param callable $callback function called after the field compilation |
||
139 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
140 | */ |
||
141 | public function afterCompile($index,$callback){ |
||
145 | |||
146 | private function addToolbarRow($part,$table,$captions){ |
||
151 | |||
152 | public function getInstanceViewer() { |
||
155 | |||
156 | public function setInstanceViewer($_instanceViewer) { |
||
160 | |||
161 | public function setCaptions($captions){ |
||
165 | |||
166 | public function setFields($fields){ |
||
170 | |||
171 | public function addField($field){ |
||
175 | |||
176 | public function insertField($index,$field){ |
||
180 | |||
181 | public function insertInField($index,$field){ |
||
185 | |||
186 | public function setValueFunction($index,$callback){ |
||
190 | |||
191 | public function setIdentifierFunction($callback){ |
||
195 | |||
196 | public function getHtmlComponent(){ |
||
199 | |||
200 | public function getUrls() { |
||
203 | |||
204 | public function setUrls($urls) { |
||
208 | |||
209 | public function paginate($items_per_page=10,$page=1){ |
||
212 | |||
213 | public function getHasCheckboxes() { |
||
216 | |||
217 | public function setHasCheckboxes($_hasCheckboxes) { |
||
221 | |||
222 | public function refresh($compileParts=["tbody"]){ |
||
226 | /** |
||
227 | * @param string $caption |
||
228 | * @param callable $callback |
||
229 | * @return callable |
||
230 | */ |
||
231 | private function getFieldButtonCallable($caption,$callback=null){ |
||
234 | |||
235 | /** |
||
236 | * @param mixed $object |
||
237 | * @param callable $callback |
||
238 | * @return callable |
||
239 | */ |
||
240 | private function getCallable($thisCallback,$parameters,$callback=null){ |
||
255 | |||
256 | /** |
||
257 | * @param string $caption |
||
258 | * @return HtmlButton |
||
259 | */ |
||
260 | private function getFieldButton($caption){ |
||
263 | |||
264 | /** |
||
265 | * Inserts a new Button for each row |
||
266 | * @param string $caption |
||
267 | * @param callable $callback |
||
268 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
269 | */ |
||
270 | public function addFieldButton($caption,$callback=null){ |
||
274 | |||
275 | /** |
||
276 | * Inserts a new Button for each row at col $index |
||
277 | * @param int $index |
||
278 | * @param string $caption |
||
279 | * @param callable $callback |
||
280 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
281 | */ |
||
282 | public function insertFieldButton($index,$caption,$callback=null){ |
||
286 | |||
287 | /** |
||
288 | * Inserts a new Button for each row in col at $index |
||
289 | * @param int $index |
||
290 | * @param string $caption |
||
291 | * @param callable $callback |
||
292 | * @return \Ajax\semantic\widgets\datatable\DataTable |
||
293 | */ |
||
294 | public function insertInFieldButton($index,$caption,$callback=null){ |
||
298 | |||
299 | private function addDefaultButton($icon,$class=null,$callback=null){ |
||
303 | |||
304 | private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){ |
||
308 | |||
309 | private function getDefaultButton($icon,$class=null){ |
||
316 | |||
317 | public function addDeleteButton($callback=null){ |
||
320 | |||
321 | public function addEditButton($callback=null){ |
||
324 | |||
325 | public function addEditDeleteButtons($callbackEdit=null,$callbackDelete=null){ |
||
331 | |||
332 | public function insertDeleteButtonIn($index,$callback=null){ |
||
335 | |||
336 | public function insertEditButtonIn($index,$callback=null){ |
||
339 | |||
340 | public function setSelectable(){ |
||
344 | |||
345 | /** |
||
346 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
347 | */ |
||
348 | public function getToolbar(){ |
||
355 | |||
356 | /** |
||
357 | * Adds a new element in toolbar |
||
358 | * @param mixed $element |
||
359 | * @return \Ajax\common\html\HtmlDoubleElement |
||
360 | */ |
||
361 | public function addInToolbar($element){ |
||
365 | |||
366 | public function addItemInToolbar($caption,$icon=NULL){ |
||
371 | |||
372 | public function addButtonInToolbar($caption){ |
||
376 | |||
377 | public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){ |
||
382 | |||
383 | |||
384 | public function addSearchInToolbar(){ |
||
387 | |||
388 | public function getSearchField(){ |
||
395 | |||
396 | public function setSortable($colIndex=NULL) { |
||
400 | |||
401 | protected function _getFieldIdentifier($prefix){ |
||
404 | |||
405 | /** |
||
406 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
407 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
408 | * @param callable $callback |
||
409 | * @return DataTable |
||
410 | */ |
||
411 | public function onNewRow($callback) { |
||
415 | } |
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.