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 Level level |
||
| 38 | */ |
||
| 39 | private $log_data; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Pattern pattern |
||
| 43 | */ |
||
| 44 | private $pattern; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * LaravelLogViewer constructor. |
||
| 48 | */ |
||
| 49 | public function __construct() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $folder |
||
| 60 | */ |
||
| 61 | public function setFolder($folder) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $file |
||
| 72 | * @throws \Exception |
||
| 73 | */ |
||
| 74 | public function setFile($file) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $file |
||
| 85 | * @return string |
||
| 86 | * @throws \Exception |
||
| 87 | */ |
||
| 88 | public function pathToLogFile($file) |
||
| 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 | protected function setFileAll() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function all() |
||
| 179 | |||
| 180 | private function getLog($headings){ |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Create array data from log |
||
| 202 | * @param $index |
||
| 203 | * @param $current |
||
| 204 | * @param $level |
||
| 205 | * @param $key |
||
| 206 | * @param $line |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | protected function getArrayLog($index, $current, $level, $key, $line) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function getFolders() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param bool $basename |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function getFolderFiles($basename = false) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param bool $basename |
||
| 248 | * @param string $folder |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function getFiles($basename = false, $folder = '') |
||
| 265 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..