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 Log Data log_data |
||
| 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() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $headings |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | protected function getLogData($headings) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Create array data from log |
||
| 220 | * @param $data |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | protected function getArrayLog($data) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getFolders() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param bool $basename |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | public function getFolderFiles($basename = false) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param bool $basename |
||
| 279 | * @param string $folder |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getFiles($basename = false, $folder = '') |
||
| 311 | } |
||
| 312 |
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..