Complex classes like Userlog 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 Userlog, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Userlog extends \Xmf\Module\Helper |
||
35 | { |
||
36 | public $logmodule; |
||
37 | public $user; |
||
38 | public $debugArray = []; |
||
39 | public $logext = 'log'; |
||
40 | public $cookiePrefix = ''; |
||
41 | public $groupList; |
||
42 | public $browscap; |
||
43 | |||
44 | /** |
||
45 | * @param $debug |
||
46 | */ |
||
47 | protected function __construct($debug) |
||
53 | |||
54 | /** |
||
55 | * @param bool $debug |
||
56 | * |
||
57 | * @return Userlog |
||
58 | */ |
||
59 | public static function getInstance($debug = false) |
||
68 | |||
69 | /** |
||
70 | * @return null|\XoopsModule |
||
71 | */ |
||
72 | public function getLogModule() |
||
80 | |||
81 | /** |
||
82 | * @param array $dirnames |
||
83 | * @param null $otherCriteria |
||
84 | * @param bool $asObj |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function getModules($dirnames = [], $otherCriteria = null, $asObj = false) |
||
114 | |||
115 | /** |
||
116 | * @return null |
||
117 | */ |
||
118 | public function getUser() |
||
126 | |||
127 | /** |
||
128 | * @return null|array |
||
129 | */ |
||
130 | public function getGroupList() |
||
138 | |||
139 | /** |
||
140 | * @return null |
||
141 | */ |
||
142 | public function getBrowsCap() |
||
150 | |||
151 | |||
152 | /** |
||
153 | * @param null $name |
||
154 | * @param null $value |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function setConfig($name = null, $value = null) |
||
168 | |||
169 | /** |
||
170 | * @return array |
||
171 | */ |
||
172 | public function getAllLogFiles() |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getWorkingFile() |
||
199 | |||
200 | /** |
||
201 | * @param $array |
||
202 | * @param null $keys |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function getFromKeys($array, $keys = null) |
||
223 | |||
224 | /** |
||
225 | * @param int $since |
||
226 | * |
||
227 | * @return int |
||
228 | */ |
||
229 | public function getSinceTime($since = 1) // one day |
||
237 | |||
238 | /** |
||
239 | * @param null $intTime |
||
240 | * @param string $dateFormat |
||
241 | * @param null|string $timeoffset |
||
242 | * |
||
243 | * @return bool|string |
||
244 | */ |
||
245 | public function formatTime($intTime = null, $dateFormat = 'c', $timeoffset = null) |
||
257 | |||
258 | /** |
||
259 | * @param string $name |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | public function getCookie($name = 'TOGGLE') |
||
269 | |||
270 | /** |
||
271 | * @param int $prob |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function probCheck($prob = 11) |
||
283 | |||
284 | /** |
||
285 | * @param null $post |
||
286 | * @param int $uid |
||
287 | * @param bool $unsetPass |
||
288 | * |
||
289 | * @return null |
||
290 | */ |
||
291 | public function patchLoginHistory($post = null, $uid = 0, $unsetPass = true) |
||
320 | |||
321 | |||
322 | private function initLogModule() |
||
334 | |||
335 | private function initUser() |
||
345 | |||
346 | private function initGroupList() |
||
353 | |||
354 | /** |
||
355 | * @return bool |
||
356 | * @throws \phpbrowscap\Exception |
||
357 | */ |
||
358 | private function initBrowsCap() |
||
376 | } |
||
377 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.