Complex classes like ProcessListView 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 ProcessListView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class ProcessListView |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string template path |
||
| 43 | */ |
||
| 44 | protected $template = 'EXT:crawler/template/process/list.php'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string icon path |
||
| 48 | */ |
||
| 49 | protected $iconPath; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string Holds the path to start a cli process via command line |
||
| 53 | */ |
||
| 54 | protected $cliPath; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int Holds the total number of items pending in the queue to be processed |
||
| 58 | */ |
||
| 59 | protected $totalItemCount; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean Holds the enable state of the crawler |
||
| 63 | */ |
||
| 64 | protected $isCrawlerEnabled; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int Holds the number of active processes |
||
| 68 | */ |
||
| 69 | protected $activeProcessCount; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int Holds the number of maximum active processes |
||
| 73 | */ |
||
| 74 | protected $maxActiveProcessCount; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string Holds the mode state, can be simple or detail |
||
| 78 | */ |
||
| 79 | protected $mode; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var int Holds the current page id |
||
| 83 | */ |
||
| 84 | protected $pageId; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int $totalItemCount number of total item |
||
| 88 | */ |
||
| 89 | protected $totalUnprocessedItemCount; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var int Holds the number of assigned unprocessed items |
||
| 93 | */ |
||
| 94 | protected $assignedUnprocessedItemCount; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return int |
||
| 98 | */ |
||
| 99 | public function getAssignedUnprocessedItemCount() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | public function getTotalUnprocessedItemCount() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param int $assignedUnprocessedItemCount |
||
| 114 | */ |
||
| 115 | public function setAssignedUnprocessedItemCount($assignedUnprocessedItemCount) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param int $totalUnprocessedItemCount |
||
| 122 | */ |
||
| 123 | public function setTotalUnprocessedItemCount($totalUnprocessedItemCount) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set the page id |
||
| 130 | * |
||
| 131 | * @param int $pageId page id |
||
| 132 | */ |
||
| 133 | public function setPageId($pageId) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the page id |
||
| 140 | * |
||
| 141 | * @return int page id |
||
| 142 | */ |
||
| 143 | public function getPageId() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getMode() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $mode |
||
| 158 | */ |
||
| 159 | public function setMode($mode) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | public function getMaxActiveProcessCount() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $maxActiveProcessCount |
||
| 174 | */ |
||
| 175 | public function setMaxActiveProcessCount($maxActiveProcessCount) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | public function getActiveProcessCount() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param int $activeProcessCount |
||
| 190 | */ |
||
| 191 | public function setActiveProcessCount($activeProcessCount) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return boolean |
||
| 198 | */ |
||
| 199 | public function getIsCrawlerEnabled() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param boolean $isCrawlerEnabled |
||
| 206 | */ |
||
| 207 | public function setIsCrawlerEnabled($isCrawlerEnabled) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns the path to start a cli process from the shell |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getCliPath() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $cliPath |
||
| 224 | */ |
||
| 225 | public function setCliPath($cliPath) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return int |
||
| 232 | */ |
||
| 233 | public function getTotalItemCount() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param int $totalItemCount |
||
| 240 | */ |
||
| 241 | public function setTotalItemCount($totalItemCount) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Method to set the path to the icon from outside |
||
| 248 | * |
||
| 249 | * @param string $iconPath |
||
| 250 | */ |
||
| 251 | public function setIconPath($iconPath) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Method to read the configured icon path |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | protected function getIconPath() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Method to set a collection of process objects to be displayed in |
||
| 268 | * the list view. |
||
| 269 | * |
||
| 270 | * @param ProcessCollection $processCollection |
||
| 271 | */ |
||
| 272 | public function setProcessCollection($processCollection) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a collection of processObjects. |
||
| 279 | * |
||
| 280 | * @return ProcessCollection |
||
| 281 | */ |
||
| 282 | protected function getProcessCollection() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Formats a timestamp as date |
||
| 289 | * |
||
| 290 | * @param int $timestamp |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | protected function asDate($timestamp) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Converts seconds into minutes |
||
| 305 | * |
||
| 306 | * @param int $seconds |
||
| 307 | * |
||
| 308 | * @return float |
||
| 309 | */ |
||
| 310 | protected function asMinutes($seconds) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns the state icon for the current job |
||
| 317 | * |
||
| 318 | * @param string $state |
||
| 319 | * @return string icon |
||
| 320 | */ |
||
| 321 | protected function getIconForState($state) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns an imagetag for an icon |
||
| 343 | * |
||
| 344 | * @param string $icon |
||
| 345 | * @param string $title |
||
| 346 | * |
||
| 347 | * @return string html tag for icon |
||
| 348 | */ |
||
| 349 | protected function getIcon($icon, $title = '') |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns a tag for the refresh icon |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | protected function getRefreshLink() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns a link for the panel to enable or disable the crawler |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | protected function getEnableDisableLink() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get mode link |
||
| 397 | * |
||
| 398 | * @param void |
||
| 399 | * |
||
| 400 | * @return string a-tag |
||
| 401 | */ |
||
| 402 | protected function getModeLink() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get add link |
||
| 421 | * |
||
| 422 | * @param void |
||
| 423 | * |
||
| 424 | * @return string a-tag |
||
| 425 | */ |
||
| 426 | protected function getAddLink() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Method to render the view. |
||
| 441 | * |
||
| 442 | * @return string html content |
||
| 443 | */ |
||
| 444 | public function render() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * retrieve locallanglabel from environment |
||
| 457 | * just a wrapper should be done in a cleaner way |
||
| 458 | * later on |
||
| 459 | * |
||
| 460 | * @param string $label |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | protected function getLLLabel($label) |
||
| 468 | } |
||
| 469 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: