Complex classes like TbButton 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 TbButton, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class TbButton extends TbWidget { |
||
| 22 | |||
| 23 | // Button callback types. |
||
| 24 | const BUTTON_LINK = 'link'; |
||
| 25 | const BUTTON_BUTTON = 'button'; |
||
| 26 | const BUTTON_SUBMIT = 'submit'; |
||
| 27 | const BUTTON_SUBMITLINK = 'submitLink'; |
||
| 28 | const BUTTON_RESET = 'reset'; |
||
| 29 | const BUTTON_AJAXLINK = 'ajaxLink'; |
||
| 30 | const BUTTON_AJAXBUTTON = 'ajaxButton'; |
||
| 31 | const BUTTON_AJAXSUBMIT = 'ajaxSubmit'; |
||
| 32 | const BUTTON_INPUTBUTTON = 'inputButton'; |
||
| 33 | const BUTTON_INPUTSUBMIT = 'inputSubmit'; |
||
| 34 | const BUTTON_TOGGLE_RADIO = 'radio'; |
||
| 35 | const BUTTON_TOGGLE_CHECKBOX = 'checkbox'; |
||
| 36 | |||
| 37 | const CTX_LINK = 'link'; |
||
| 38 | const CTX_LINK_CLASS = 'link'; |
||
| 39 | |||
| 40 | // Button sizes. |
||
| 41 | const SIZE_LARGE = 'large'; |
||
| 42 | const SIZE_DEFAULT = 'default'; |
||
| 43 | const SIZE_SMALL = 'small'; |
||
| 44 | const SIZE_EXTRA_SMALL = 'extra_small'; |
||
| 45 | |||
| 46 | protected static $sizeClasses = array( |
||
| 47 | self::SIZE_LARGE => 'btn-lg', |
||
| 48 | self::SIZE_DEFAULT => '', |
||
| 49 | self::SIZE_SMALL => 'btn-sm', |
||
| 50 | self::SIZE_EXTRA_SMALL => 'btn-xs', |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string the button callback types. |
||
| 55 | * Valid values are 'link', 'button', 'submit', 'submitLink', 'reset', 'ajaxLink', 'ajaxButton' and 'ajaxSubmit'. |
||
| 56 | */ |
||
| 57 | public $buttonType = self::BUTTON_BUTTON; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string the button size. |
||
| 61 | * Valid values are 'large', 'small' and 'mini'. |
||
| 62 | */ |
||
| 63 | public $size; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string the button icon, e.g. 'ok' or 'remove white'. |
||
| 67 | */ |
||
| 68 | public $icon; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string the button label. |
||
| 72 | */ |
||
| 73 | public $label; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string the button URL. |
||
| 77 | */ |
||
| 78 | public $url; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var boolean indicates whether the button should span the full width of the a parent. |
||
| 82 | */ |
||
| 83 | public $block = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var boolean indicates whether the button is active. |
||
| 87 | */ |
||
| 88 | public $active = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var boolean indicates whether the button is disabled. |
||
| 92 | */ |
||
| 93 | public $disabled = false; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var boolean indicates whether to encode the label. |
||
| 97 | */ |
||
| 98 | public $encodeLabel = true; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var boolean indicates whether to enable toggle. |
||
| 102 | */ |
||
| 103 | public $toggle; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string the loading text. |
||
| 107 | */ |
||
| 108 | public $loadingText; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var string the complete text. |
||
| 112 | */ |
||
| 113 | public $completeText; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var array the dropdown button items. |
||
| 117 | */ |
||
| 118 | public $items; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var array the HTML attributes for the widget container. |
||
| 122 | */ |
||
| 123 | public $htmlOptions = array(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array the button ajax options (used by 'ajaxLink' and 'ajaxButton'). |
||
| 127 | */ |
||
| 128 | public $ajaxOptions = array(); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var array the HTML attributes for the dropdown menu. |
||
| 132 | * @since 0.9.11 |
||
| 133 | */ |
||
| 134 | public $dropdownOptions = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var bool whether the button is visible or not |
||
| 138 | * @since 0.9.11 |
||
| 139 | */ |
||
| 140 | public $visible = true; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Tooltip for button |
||
| 144 | * @var bool |
||
| 145 | * @since 2.1.0 |
||
| 146 | */ |
||
| 147 | public $tooltip = false; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Tooltip options |
||
| 151 | * @var array |
||
| 152 | * @since 2.1.0 |
||
| 153 | */ |
||
| 154 | public $tooltipOptions = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | *### .init() |
||
| 158 | * |
||
| 159 | * Initializes the widget. |
||
| 160 | */ |
||
| 161 | 1 | public function init() { |
|
| 286 | |||
| 287 | /** |
||
| 288 | *### .run() |
||
| 289 | * |
||
| 290 | * Runs the widget. |
||
| 291 | */ |
||
| 292 | 1 | public function run() { |
|
| 316 | |||
| 317 | /** |
||
| 318 | *### .createButton() |
||
| 319 | * |
||
| 320 | * Creates the button element. |
||
| 321 | * |
||
| 322 | * @return string the created button. |
||
| 323 | */ |
||
| 324 | 1 | protected function createButton() { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $toggleType |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | protected function createToggleButton($toggleType) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | *### .hasDropdown() |
||
| 391 | * |
||
| 392 | * Returns whether the button has a dropdown. |
||
| 393 | * |
||
| 394 | * @return bool the result. |
||
| 395 | */ |
||
| 396 | 1 | protected function hasDropdown() { |
|
| 400 | } |
||
| 401 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.