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 |
||
40 | class ProcessListView |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * @var string template path |
||
45 | */ |
||
46 | protected $template = 'EXT:crawler/template/process/list.php'; |
||
47 | |||
48 | /** |
||
49 | * @var string icon path |
||
50 | */ |
||
51 | protected $iconPath; |
||
52 | |||
53 | /** |
||
54 | * @var string Holds the path to start a cli process via command line |
||
55 | */ |
||
56 | protected $cliPath; |
||
57 | |||
58 | /** |
||
59 | * @var int Holds the total number of items pending in the queue to be processed |
||
60 | */ |
||
61 | protected $totalItemCount; |
||
62 | |||
63 | /** |
||
64 | * @var boolean Holds the enable state of the crawler |
||
65 | */ |
||
66 | protected $isCrawlerEnabled; |
||
67 | |||
68 | /** |
||
69 | * @var int Holds the number of active processes |
||
70 | */ |
||
71 | protected $activeProcessCount; |
||
72 | |||
73 | /** |
||
74 | * @var int Holds the number of maximum active processes |
||
75 | */ |
||
76 | protected $maxActiveProcessCount; |
||
77 | |||
78 | /** |
||
79 | * @var string Holds the mode state, can be simple or detail |
||
80 | */ |
||
81 | protected $mode; |
||
82 | |||
83 | /** |
||
84 | * @var int Holds the current page id |
||
85 | */ |
||
86 | protected $pageId; |
||
87 | |||
88 | /** |
||
89 | * @var int $totalItemCount number of total item |
||
90 | */ |
||
91 | protected $totalUnprocessedItemCount; |
||
92 | |||
93 | /** |
||
94 | * @var int Holds the number of assigned unprocessed items |
||
95 | */ |
||
96 | protected $assignedUnprocessedItemCount; |
||
97 | |||
98 | /** |
||
99 | * @return int |
||
100 | */ |
||
101 | public function getAssignedUnprocessedItemCount() |
||
105 | |||
106 | /** |
||
107 | * @return int |
||
108 | */ |
||
109 | public function getTotalUnprocessedItemCount() |
||
113 | |||
114 | /** |
||
115 | * @param int $assignedUnprocessedItemCount |
||
116 | */ |
||
117 | public function setAssignedUnprocessedItemCount($assignedUnprocessedItemCount) |
||
121 | |||
122 | /** |
||
123 | * @param int $totalUnprocessedItemCount |
||
124 | */ |
||
125 | public function setTotalUnprocessedItemCount($totalUnprocessedItemCount) |
||
129 | |||
130 | /** |
||
131 | * Set the page id |
||
132 | * |
||
133 | * @param int $pageId page id |
||
134 | */ |
||
135 | public function setPageId($pageId) |
||
139 | |||
140 | /** |
||
141 | * Get the page id |
||
142 | * |
||
143 | * @return int page id |
||
144 | */ |
||
145 | public function getPageId() |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getMode() |
||
157 | |||
158 | /** |
||
159 | * @param string $mode |
||
160 | */ |
||
161 | public function setMode($mode) |
||
165 | |||
166 | /** |
||
167 | * @return int |
||
168 | */ |
||
169 | public function getMaxActiveProcessCount() |
||
173 | |||
174 | /** |
||
175 | * @param int $maxActiveProcessCount |
||
176 | */ |
||
177 | public function setMaxActiveProcessCount($maxActiveProcessCount) |
||
181 | |||
182 | /** |
||
183 | * @return int |
||
184 | */ |
||
185 | public function getActiveProcessCount() |
||
189 | |||
190 | /** |
||
191 | * @param int $activeProcessCount |
||
192 | */ |
||
193 | public function setActiveProcessCount($activeProcessCount) |
||
197 | |||
198 | /** |
||
199 | * @return boolean |
||
200 | */ |
||
201 | public function getIsCrawlerEnabled() |
||
205 | |||
206 | /** |
||
207 | * @param boolean $isCrawlerEnabled |
||
208 | */ |
||
209 | public function setIsCrawlerEnabled($isCrawlerEnabled) |
||
213 | |||
214 | /** |
||
215 | * Returns the path to start a cli process from the shell |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getCliPath() |
||
223 | |||
224 | /** |
||
225 | * @param string $cliPath |
||
226 | */ |
||
227 | public function setCliPath($cliPath) |
||
231 | |||
232 | /** |
||
233 | * @return int |
||
234 | */ |
||
235 | public function getTotalItemCount() |
||
239 | |||
240 | /** |
||
241 | * @param int $totalItemCount |
||
242 | */ |
||
243 | public function setTotalItemCount($totalItemCount) |
||
247 | |||
248 | /** |
||
249 | * Method to set the path to the icon from outside |
||
250 | * |
||
251 | * @param string $iconPath |
||
252 | */ |
||
253 | public function setIconPath($iconPath) |
||
257 | |||
258 | /** |
||
259 | * Method to read the configured icon path |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function getIconPath() |
||
267 | |||
268 | /** |
||
269 | * Method to set a collection of process objects to be displayed in |
||
270 | * the list view. |
||
271 | * |
||
272 | * @param ProcessCollection $processCollection |
||
273 | */ |
||
274 | public function setProcessCollection($processCollection) |
||
278 | |||
279 | /** |
||
280 | * Returns a collection of processObjects. |
||
281 | * |
||
282 | * @return ProcessCollection |
||
283 | */ |
||
284 | protected function getProcessCollection() |
||
288 | |||
289 | /** |
||
290 | * Formats a timestamp as date |
||
291 | * |
||
292 | * @param int $timestamp |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected function asDate($timestamp) |
||
304 | |||
305 | /** |
||
306 | * Converts seconds into minutes |
||
307 | * |
||
308 | * @param int $seconds |
||
309 | * |
||
310 | * @return float |
||
311 | */ |
||
312 | protected function asMinutes($seconds) |
||
316 | |||
317 | /** |
||
318 | * Returns the state icon for the current job |
||
319 | * |
||
320 | * @param string $state |
||
321 | * @return string icon |
||
322 | */ |
||
323 | protected function getIconForState($state) |
||
342 | |||
343 | /** |
||
344 | * Returns an imagetag for an icon |
||
345 | * |
||
346 | * @param string $icon |
||
347 | * @param string $title |
||
348 | * |
||
349 | * @return string html tag for icon |
||
350 | */ |
||
351 | protected function getIcon($icon, $title = '') |
||
358 | |||
359 | /** |
||
360 | * Returns a tag for the refresh icon |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | protected function getRefreshLink() |
||
372 | |||
373 | /** |
||
374 | * Returns a link for the panel to enable or disable the crawler |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | protected function getEnableDisableLink() |
||
396 | |||
397 | /** |
||
398 | * Get mode link |
||
399 | * |
||
400 | * @param void |
||
401 | * |
||
402 | * @return string a-tag |
||
403 | */ |
||
404 | protected function getModeLink() |
||
420 | |||
421 | /** |
||
422 | * Get add link |
||
423 | * |
||
424 | * @param void |
||
425 | * |
||
426 | * @return string a-tag |
||
427 | */ |
||
428 | protected function getAddLink() |
||
440 | |||
441 | /** |
||
442 | * Method to render the view. |
||
443 | * |
||
444 | * @return string html content |
||
445 | */ |
||
446 | public function render() |
||
456 | |||
457 | /** |
||
458 | * retrieve locallanglabel from environment |
||
459 | * just a wrapper should be done in a cleaner way |
||
460 | * later on |
||
461 | * |
||
462 | * @param string $label |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | protected function getLLLabel($label) |
||
470 | } |
||
471 |
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: