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) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $file |
||
| 66 | * @throws \Exception |
||
| 67 | */ |
||
| 68 | public function setFile($file) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $file |
||
| 79 | * @return string |
||
| 80 | * @throws \Exception |
||
| 81 | */ |
||
| 82 | public function pathToLogFile($file) |
||
| 83 | { |
||
| 84 | if (app('files')->exists($file)) { // try the absolute path |
||
| 85 | return $file; |
||
| 86 | } |
||
| 87 | if (is_array($this->storage_path)) { |
||
| 88 | foreach ($this->storage_path as $value) { |
||
| 89 | if (app('files')->exists($value . '/' . $file)) { // try the absolute path |
||
| 90 | $file = $value . '/' . $file; |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | if (!is_array($this->storage_path)) { |
||
| 96 | $logsPath = $this->storage_path; |
||
| 97 | $logsPath .= ($this->folder) ? '/' . $this->folder : ''; |
||
| 98 | $file = $logsPath . '/' . $file; |
||
| 99 | // check if requested file is really in the logs directory |
||
| 100 | if (dirname($file) !== $logsPath) { |
||
| 101 | throw new \Exception('No such log file'); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | return $file; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getFolderName() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getFileName() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function all() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getFolders() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param bool $basename |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public function getFolderFiles($basename = false) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param bool $basename |
||
| 241 | * @param string $folder |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function getFiles($basename = false, $folder = '') |
||
| 276 | } |
||
| 277 |