Complex classes like LaravelLogViewer 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 LaravelLogViewer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class LaravelLogViewer |
||
10 | { |
||
11 | /** |
||
12 | * @var string file |
||
13 | */ |
||
14 | private $file; |
||
15 | |||
16 | /** |
||
17 | * @var string folder |
||
18 | */ |
||
19 | private $folder; |
||
20 | |||
21 | /** |
||
22 | * @var string storage_path |
||
23 | */ |
||
24 | private $storage_path; |
||
25 | |||
26 | /** |
||
27 | * Why? Uh... Sorry |
||
28 | */ |
||
29 | const MAX_FILE_SIZE = 52428800; |
||
30 | |||
31 | /** |
||
32 | * @var Level level |
||
33 | */ |
||
34 | private $level; |
||
35 | |||
36 | /** |
||
37 | * @var Pattern pattern |
||
38 | */ |
||
39 | private $pattern; |
||
40 | |||
41 | /** |
||
42 | * LaravelLogViewer constructor. |
||
43 | */ |
||
44 | public function __construct() |
||
51 | |||
52 | /** |
||
53 | * @param string $folder |
||
54 | */ |
||
55 | public function setFolder($folder) |
||
67 | |||
68 | /** |
||
69 | * @param string $file |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | public function setFile($file) |
||
80 | |||
81 | /** |
||
82 | * @param string $file |
||
83 | * @return string |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | public function pathToLogFile($file) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getFolderName() |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getFileName() |
||
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | public function all() |
||
209 | |||
210 | /** |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getFolders() |
||
234 | |||
235 | /** |
||
236 | * @param bool $basename |
||
237 | * @return array |
||
238 | */ |
||
239 | public function getFolderFiles($basename = false) |
||
243 | |||
244 | /** |
||
245 | * @param bool $basename |
||
246 | * @param string $folder |
||
247 | * @return array |
||
248 | */ |
||
249 | public function getFiles($basename = false, $folder = '') |
||
279 | |||
280 | /** |
||
281 | * @throws \Exception |
||
282 | */ |
||
283 | public function getFilesWithPath() |
||
295 | } |
||
296 |