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() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Create array data from log |
||
| 208 | * @param $data |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | protected function getArrayLog($data) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function getFolders() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param bool $basename |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public function getFolderFiles($basename = false) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param bool $basename |
||
| 255 | * @param string $folder |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getFiles($basename = false, $folder = '') |
||
| 274 | } |
||
| 275 |
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..