Complex classes like GroupColumnsBehavior 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 GroupColumnsBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class GroupColumnsBehavior extends Behavior |
||
| 20 | { |
||
| 21 | const MERGE_SIMPLE = 'simple'; |
||
| 22 | const MERGE_NESTED = 'nested'; |
||
| 23 | const MERGE_FIRST_ROW = 'firstrow'; |
||
| 24 | const POS_ABOVE = 'above'; |
||
| 25 | const POS_BELOW = 'below'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array the column names to merge |
||
| 29 | */ |
||
| 30 | public $mergeColumns = []; |
||
| 31 | /** |
||
| 32 | * @var string the way to merge the columns. Possible values are: |
||
| 33 | * |
||
| 34 | * - [[GroupGridView::MERGE_SIMPLE]] Column values are merged independently |
||
| 35 | * - [[GroupGridView::MERGE_NESTED]] Column values are merged if at least one value of nested columns changes |
||
| 36 | * (makes sense when several columns in $mergeColumns option) |
||
| 37 | * - [[GroupGridView::MERGE_FIRST_ROW]] Column values are merged independently, but value is shown in first row of |
||
| 38 | * group and below cells just cleared (instead of `rowspan`) |
||
| 39 | * |
||
| 40 | */ |
||
| 41 | public $type = self::MERGE_SIMPLE; |
||
| 42 | /** |
||
| 43 | * Need to merge null values in columns? Default - merge on. |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | public $doNotMergeEmptyValue = false; |
||
| 47 | /** |
||
| 48 | * Exclude column for the rule if [[GroupGridView::doNotMergeEmptyValue]] is true. |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | public $mergeEmptyColumns = []; |
||
| 52 | /** |
||
| 53 | * @var string the CSS class to use for the merged cells |
||
| 54 | */ |
||
| 55 | public $mergeCellClass = 'group-view-merge-cells'; |
||
| 56 | /** |
||
| 57 | * @var array the list of columns on which change extra row will be triggered |
||
| 58 | */ |
||
| 59 | public $extraRowColumns = []; |
||
| 60 | /** |
||
| 61 | * @var string|\Closure an anonymous function that returns the value to be displayed for every extra row. |
||
| 62 | * The signature of this function is `function ($model, $key, $index, $totals)`. |
||
| 63 | * If this is not set, `$model[$attribute]` will be used to obtain the value. |
||
| 64 | * |
||
| 65 | * You may also set this property to a string representing the attribute name to be displayed in this column. |
||
| 66 | * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for |
||
| 67 | * sorting and filtering. |
||
| 68 | */ |
||
| 69 | public $extraRowValue; |
||
| 70 | /** |
||
| 71 | * @var string the position of the extra row. Possible values are [[GroupGridView::POS_ABOVE]] or |
||
| 72 | * [[GroupGridView::POS_BELOW]] |
||
| 73 | */ |
||
| 74 | public $extraRowPosition = self::POS_ABOVE; |
||
| 75 | /** |
||
| 76 | * @var \Closure an anonymous function that returns a calculated value of the totals. Its signature is |
||
| 77 | * `function($model, $index, $totals)` |
||
| 78 | */ |
||
| 79 | public $extraRowTotalsValue; |
||
| 80 | /** |
||
| 81 | * @var string the CSS class to add on the extra row TD tag |
||
| 82 | */ |
||
| 83 | public $extraRowClass = 'group-view-extra-row'; |
||
| 84 | /** |
||
| 85 | * @var array stores the groups |
||
| 86 | */ |
||
| 87 | private $_groups = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Renders the data models for the grid view. |
||
| 91 | */ |
||
| 92 | public function renderItems() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Renders the table body. |
||
| 116 | * @return string the rendering result. |
||
| 117 | */ |
||
| 118 | public function renderTableBody() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Finds and stores changes of grouped columns |
||
| 157 | */ |
||
| 158 | public function groupColumns() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @inheritdoc |
||
| 263 | */ |
||
| 264 | public function renderTableRow($model, $key, $index) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Renders extra row when required |
||
| 323 | * |
||
| 324 | * @param mixed $model |
||
| 325 | * @param mixed $key |
||
| 326 | * @param int $index |
||
| 327 | * @param number $totals |
||
| 328 | * |
||
| 329 | * @return string the extra row |
||
| 330 | */ |
||
| 331 | protected function renderExtraRow($model, $key, $index, $totals) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Is current row start or end of group in particular column |
||
| 356 | * |
||
| 357 | * @param string $name the column name |
||
| 358 | * @param int $row the row index |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | protected function isGroupEdge($name, $row) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * If there is a Closure will return the newly calculated totals on the Closure according to the code specified by |
||
| 384 | * the user. |
||
| 385 | * |
||
| 386 | * @param mixed $model the data model being rendered |
||
| 387 | * @param int $index the zero-based index of the data model among the models array |
||
| 388 | * @param array $totals the calculated totals by the Closure |
||
| 389 | * |
||
| 390 | * @return array|mixed |
||
| 391 | */ |
||
| 392 | protected function getExtraRowTotals($model, $index, $totals) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns the row values of the column |
||
| 401 | * |
||
| 402 | * @param array $columns the columns |
||
| 403 | * @param mixed $model the model |
||
| 404 | * @param int $index the zero-base index of the model among the models array |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | protected function getRowValues($columns, $model, $index = 0) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Returns the column data cell content |
||
| 426 | * |
||
| 427 | * @param \yii\grid\DataColumn $column |
||
| 428 | * @param mixed $model the data model being rendered |
||
| 429 | * @param mixed $key the key associated with the data model |
||
| 430 | * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]] |
||
| 431 | * |
||
| 432 | * @return string the rendering result |
||
| 433 | */ |
||
| 434 | protected function getColumnDataCellContent($column, $model, $key, $index) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns the column data cell value |
||
| 453 | * |
||
| 454 | * @param \yii\grid\DataColumn $column |
||
| 455 | * @param mixed $model the data model being rendered |
||
| 456 | * @param mixed $key the key associated with the data model |
||
| 457 | * @param int $index the zero-based index of the data model among the models array |
||
| 458 | * |
||
| 459 | * @return mixed|null the result |
||
| 460 | */ |
||
| 461 | protected function getColumnDataCellValue($column, $model, $key, $index) |
||
| 475 | } |
||
| 476 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.