Complex classes like Display 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 Display, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Display |
||
15 | { |
||
16 | |||
17 | /** @var array */ |
||
18 | protected $defaults = array( |
||
19 | 'relative_path' => true, |
||
20 | 'script_path' => 'asset/script.js', |
||
21 | 'style_path' => 'asset/style.css' |
||
22 | ); |
||
23 | |||
24 | /** @var array */ |
||
25 | protected $options; |
||
26 | |||
27 | /** @var double */ |
||
28 | protected $startTime; |
||
29 | |||
30 | /** @var Console */ |
||
31 | protected $console; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $speedData; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $queryData; |
||
38 | |||
39 | /** @var array */ |
||
40 | protected $memoryData; |
||
41 | |||
42 | /** @var array */ |
||
43 | protected $fileData; |
||
44 | |||
45 | /** @var integer */ |
||
46 | protected $pathTrimStart = 0; |
||
47 | |||
48 | /** |
||
49 | * @param array $options |
||
50 | */ |
||
51 | public function __construct(array $options = array()) |
||
60 | |||
61 | /** |
||
62 | * @param double $startTime |
||
63 | */ |
||
64 | public function setStartTime($startTime) |
||
68 | |||
69 | /** |
||
70 | * @param Console $console |
||
71 | */ |
||
72 | public function setConsole(Console $console) |
||
76 | |||
77 | /** |
||
78 | * Sets memory data |
||
79 | * |
||
80 | * @param array $data |
||
81 | */ |
||
82 | public function setMemoryData(array $data) |
||
86 | |||
87 | /** |
||
88 | * Sets query data |
||
89 | * |
||
90 | * @param array $data |
||
91 | */ |
||
92 | public function setQueryData(array $data) |
||
96 | |||
97 | /** |
||
98 | * Sets speed data |
||
99 | * |
||
100 | * @param array $data |
||
101 | */ |
||
102 | public function setSpeedData(array $data) |
||
106 | |||
107 | /** |
||
108 | * Sets file data |
||
109 | * |
||
110 | * @param array $data |
||
111 | */ |
||
112 | public function setFileData(array $data) |
||
116 | |||
117 | /** |
||
118 | * @return integer |
||
119 | */ |
||
120 | protected function getPathTrimStart($cwd, $dir) |
||
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | protected function getConsoleMeta() |
||
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function getConsoleMessages() |
||
199 | |||
200 | /** |
||
201 | * @return array |
||
202 | */ |
||
203 | protected function getSpeedMeta() |
||
213 | |||
214 | /** |
||
215 | * @return array |
||
216 | */ |
||
217 | public function getQueryMeta() |
||
235 | |||
236 | /** |
||
237 | * @return array |
||
238 | */ |
||
239 | public function getQueryList() |
||
251 | |||
252 | /** |
||
253 | * @return array |
||
254 | */ |
||
255 | public function getMemoryMeta() |
||
265 | |||
266 | /** |
||
267 | * @return array |
||
268 | */ |
||
269 | protected function getFileMeta() |
||
287 | |||
288 | /** |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function getFileList() |
||
302 | |||
303 | /** |
||
304 | * Formatter for human-readable time |
||
305 | * Only handles time up to 60 minutes gracefully |
||
306 | * |
||
307 | * @param double $time |
||
308 | * @param integer $percision |
||
309 | * @return string |
||
310 | */ |
||
311 | protected function getReadableTime($time, $percision = 3) |
||
325 | |||
326 | /** |
||
327 | * Formatter for human-readable memory |
||
328 | * Only handles time up to a few gigs gracefully |
||
329 | * |
||
330 | * @param double $size |
||
331 | * @param integer $percision |
||
332 | */ |
||
333 | protected function getReadableMemory($size, $percision = 2) |
||
343 | |||
344 | /** |
||
345 | * @param string $path |
||
346 | * @return string |
||
347 | */ |
||
348 | protected function getFilePath($path) |
||
356 | |||
357 | /** |
||
358 | * @param array $messages |
||
359 | * @param string $type |
||
360 | * @return array |
||
361 | */ |
||
362 | protected function filterMessages($messages, $type) |
||
368 | |||
369 | /** |
||
370 | * @returns array |
||
371 | */ |
||
372 | protected function gatherTemplateData() |
||
414 | |||
415 | public function __invoke() |
||
428 | } |
||
429 |