Complex classes like TbBulkActions 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 TbBulkActions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class TbBulkActions extends CComponent { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var TbGridView The grid view object that owns this column. |
||
| 22 | */ |
||
| 23 | public $grid; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array the configuration for action displays. |
||
| 27 | * Each array element specifies a single button |
||
| 28 | * which has the following format: |
||
| 29 | * <pre> |
||
| 30 | * 'actions' => array( |
||
| 31 | * array( |
||
| 32 | * 'context'=> 'primary', // '', 'primary', 'info', 'success', 'warning', 'danger' or 'inverse' |
||
| 33 | * 'size'=> 'large', // '', 'large', 'small', 'mini' |
||
| 34 | * 'label'=>'...', // text label of the button or dropdown label |
||
| 35 | * 'click'=> // the js function that will be called |
||
| 36 | * ) |
||
| 37 | * ), |
||
| 38 | * </pre> |
||
| 39 | * For more configuration options please @see TbButton |
||
| 40 | * |
||
| 41 | * Note that in order to display these additional buttons, the {@link template} property needs to |
||
| 42 | * be configured so that the corresponding button IDs appear as tokens in the template. |
||
| 43 | */ |
||
| 44 | public $actionButtons = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array the checkbox column configuration |
||
| 48 | */ |
||
| 49 | public $checkBoxColumnConfig = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $noCheckedMessage = 'No items are checked'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $align = 'right'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var integer the counter for generating implicit IDs. |
||
| 63 | */ |
||
| 64 | private static $_counter = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string id of the widget. |
||
| 68 | */ |
||
| 69 | private $_id; |
||
| 70 | |||
| 71 | /** |
||
| 72 | *### .getId() |
||
| 73 | * |
||
| 74 | * Returns the ID of the widget or generates a new one if requested. |
||
| 75 | * |
||
| 76 | * @param boolean $autoGenerate whether to generate an ID if it is not set previously |
||
| 77 | * |
||
| 78 | * @return string id of the widget. |
||
| 79 | */ |
||
| 80 | public function getId($autoGenerate = true) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string the column name of the checkbox column |
||
| 93 | */ |
||
| 94 | protected $columnName; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array the bulk action buttons |
||
| 98 | */ |
||
| 99 | protected $buttons = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array the life events to attach the buttons to |
||
| 103 | */ |
||
| 104 | protected $events = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | *### .__construct() |
||
| 108 | * |
||
| 109 | * Constructor. |
||
| 110 | * |
||
| 111 | * @param CGridView $grid the grid view that owns this column. |
||
| 112 | */ |
||
| 113 | public function __construct($grid) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | *### .init() |
||
| 120 | * |
||
| 121 | * Component's initialization method |
||
| 122 | */ |
||
| 123 | public function init() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | *### .initColumn() |
||
| 132 | * |
||
| 133 | * @return bool checks whether they are |
||
| 134 | */ |
||
| 135 | public function initColumn() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | *### .initButtons() |
||
| 170 | * |
||
| 171 | * initializes the buttons to be render |
||
| 172 | */ |
||
| 173 | public function initButtons() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | *### .renderButtons() |
||
| 185 | * |
||
| 186 | * @return bool renders all initialized buttons |
||
| 187 | */ |
||
| 188 | public function renderButtons() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | *### .registerClientScript() |
||
| 211 | * |
||
| 212 | * Registers client script |
||
| 213 | */ |
||
| 214 | public function registerClientScript() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | *### .renderButton() |
||
| 238 | * |
||
| 239 | * Creates a TbButton and renders it |
||
| 240 | * |
||
| 241 | * @param array $actionButton the configuration to create the TbButton |
||
| 242 | */ |
||
| 243 | protected function renderButton($actionButton) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | *### .attachCheckBoxColumn() |
||
| 268 | * |
||
| 269 | * Adds a checkbox column to the grid. It is called when |
||
| 270 | */ |
||
| 271 | protected function attachCheckBoxColumn() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param $action |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | * @throws CException |
||
| 317 | */ |
||
| 318 | private function convertToTbButtonConfig($action) { |
||
| 345 | } |
||
| 346 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.