Complex classes like Dashlet 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 Dashlet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Dashlet |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Id of the Dashlet |
||
| 49 | * @var guid |
||
| 50 | */ |
||
| 51 | var $id; |
||
| 52 | /** |
||
| 53 | * Title of the Dashlet |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | var $title = 'Generic Dashlet'; |
||
| 57 | /** |
||
| 58 | * true if the Dashlet has configuration options. |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | var $isConfigurable = false; |
||
| 62 | /** |
||
| 63 | * true if the Dashlet is refreshable (ie charts that provide their own refresh) |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | var $isRefreshable = true; |
||
| 67 | /** |
||
| 68 | * true if the Dashlet configuration options panel has the clear button |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | public $isConfigPanelClearShown = true; |
||
| 72 | /** |
||
| 73 | * true if the Dashlet contains javascript |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | var $hasScript = false; |
||
| 77 | /** |
||
| 78 | * Language strings, must be loaded at the Dashlet level w/ loadLanguage |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | var $dashletStrings; |
||
| 82 | /** |
||
| 83 | * Time period in minutes to refresh the dashlet (0 for never) |
||
| 84 | * Do not refresh if $isRefreshable is set to false |
||
| 85 | * |
||
| 86 | * To support auto refresh all refreshable dashlets that override process() must call processAutoRefresh() |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | var $autoRefresh = "0"; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Constructor |
||
| 93 | * |
||
| 94 | * @param $id |
||
| 95 | */ |
||
| 96 | 1 | public function __construct($id) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead |
||
| 103 | */ |
||
| 104 | public function Dashlet($id){ |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the HTML for the configure icon |
||
| 118 | * |
||
| 119 | * @return string HTML |
||
| 120 | */ |
||
| 121 | 1 | public function setConfigureIcon() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the HTML for the refresh icon |
||
| 138 | * |
||
| 139 | * @return string HTML |
||
| 140 | */ |
||
| 141 | 1 | public function setRefreshIcon() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the HTML for the delete icon |
||
| 156 | * |
||
| 157 | * @return string HTML |
||
| 158 | */ |
||
| 159 | 1 | public function setDeleteIcon() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @deprecated No longer needed, replaced with Dashlet::getHeader() and Dashlet::getFooter() |
||
| 175 | * |
||
| 176 | * @param string $text |
||
| 177 | * @return string HTML |
||
| 178 | */ |
||
| 179 | public function getTitle($text = '') |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Called when Dashlet is displayed |
||
| 186 | * |
||
| 187 | * @param string $text text after the title |
||
| 188 | * @return string Header html |
||
| 189 | */ |
||
| 190 | 1 | public function getHeader($text = '') |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Called when Dashlet is displayed |
||
| 208 | * |
||
| 209 | * @return string footer HTML |
||
| 210 | */ |
||
| 211 | 1 | public function getFooter() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Called when Dashlet is displayed, override this |
||
| 220 | * |
||
| 221 | * @return string title HTML |
||
| 222 | */ |
||
| 223 | 1 | public function display() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Called when Dashlets configuration options are called |
||
| 230 | */ |
||
| 231 | public function displayOptions() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Override if you need to do pre-processing before display is called |
||
| 237 | */ |
||
| 238 | public function process() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Processes and displays the auto refresh code for the dashlet |
||
| 244 | * |
||
| 245 | * @param int $dashletOffset |
||
| 246 | * @return string HTML code |
||
| 247 | */ |
||
| 248 | 1 | protected function processAutoRefresh($dashletOffset = 0) |
|
| 278 | |||
| 279 | 1 | protected function getAutoRefresh() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Override this if your dashlet is configurable (this is called when the the configureDashlet form is shown) |
||
| 303 | * Filters the array for only the parameters it needs to save |
||
| 304 | * |
||
| 305 | * @param array $req the array to pull options from |
||
| 306 | * @return array options array |
||
| 307 | */ |
||
| 308 | public function saveOptions($req) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Sets the language strings |
||
| 314 | * |
||
| 315 | * @param string $dashletClassname classname of the dashlet |
||
| 316 | * @param string $dashletDirectory directory path of the dashlet |
||
| 317 | */ |
||
| 318 | public function loadLanguage($dashletClassname, $dashletDirectory = 'modules/Home/Dashlets/') |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Generic way to store an options array into UserPreferences |
||
| 348 | * |
||
| 349 | * @param array $optionsArray the array to save |
||
| 350 | */ |
||
| 351 | public function storeOptions($optionsArray) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Generic way to retrieve options array from UserPreferences |
||
| 362 | * |
||
| 363 | * @return array options array stored in UserPreferences |
||
| 364 | */ |
||
| 365 | public function loadOptions() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Override this in the subclass. It is used to determine whether the dashlet can be displayed. |
||
| 378 | * |
||
| 379 | * @return bool indicating whether or not the current user has access to display this Dashlet. |
||
| 380 | */ |
||
| 381 | public function hasAccess() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Returns the available auto refresh settings you can set a dashlet to |
||
| 388 | * |
||
| 389 | * @return array options available |
||
| 390 | */ |
||
| 391 | protected function getAutoRefreshOptions() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns true if the dashlet is auto refreshable |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | protected function isAutoRefreshable() |
||
| 417 | } |
||
| 418 |