Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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() |
||
45 | { |
||
46 | $this->level = new Level(); |
||
47 | $this->pattern = new Pattern(); |
||
48 | $this->storage_path = function_exists('config') ? config('logviewer.storage_path', storage_path('logs')) : storage_path('logs'); |
||
49 | |||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $folder |
||
54 | */ |
||
55 | public function setFolder($folder) |
||
56 | { |
||
57 | if (app('files')->exists($folder)) { |
||
58 | $this->folder = $folder; |
||
59 | } |
||
60 | if(is_array($this->storage_path)){ |
||
61 | View Code Duplication | foreach ($this->storage_path as $value) { |
|
|
|||
62 | $logsPath = $value . '/' . $folder; |
||
63 | if (app('files')->exists($logsPath)) { |
||
64 | $this->folder = $folder; |
||
65 | break; |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param string $file |
||
73 | * @throws \Exception |
||
74 | */ |
||
75 | public function setFile($file) |
||
76 | { |
||
77 | $file = $this->pathToLogFile($file); |
||
78 | |||
79 | if (app('files')->exists($file)) { |
||
80 | $this->file = $file; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $file |
||
86 | * @return string |
||
87 | * @throws \Exception |
||
88 | */ |
||
89 | public function pathToLogFile($file) |
||
90 | { |
||
91 | |||
92 | if (app('files')->exists($file)) { // try the absolute path |
||
93 | return $file; |
||
94 | } |
||
95 | if (is_array($this->storage_path)) { |
||
96 | View Code Duplication | foreach ($this->storage_path as $folder) { |
|
97 | if (app('files')->exists($folder . '/' . $file)) { // try the absolute path |
||
98 | $file = $folder . '/' . $file; |
||
99 | break; |
||
100 | } |
||
101 | } |
||
102 | return $file; |
||
103 | } |
||
104 | |||
105 | $logsPath = $this->storage_path; |
||
106 | $logsPath .= ($this->folder) ? '/' . $this->folder : ''; |
||
107 | $file = $logsPath . '/' . $file; |
||
108 | // check if requested file is really in the logs directory |
||
109 | if (dirname($file) !== $logsPath) { |
||
110 | throw new \Exception('No such log file'); |
||
111 | } |
||
112 | return $file; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getFolderName() |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getFileName() |
||
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | public function all() |
||
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | public function getFolders() |
||
235 | |||
236 | /** |
||
237 | * @param bool $basename |
||
238 | * @return array |
||
239 | */ |
||
240 | public function getFolderFiles($basename = false) |
||
244 | |||
245 | /** |
||
246 | * @param bool $basename |
||
247 | * @param string $folder |
||
248 | * @return array |
||
249 | */ |
||
250 | public function getFiles($basename = false, $folder = '') |
||
278 | } |
||
279 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.