Total Complexity | 45 |
Total Lines | 512 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ActionLogParser 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.
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 ActionLogParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | final class ActionLogParser |
||
25 | { |
||
26 | // Log codes. Same as Shieldon action codes. |
||
27 | const LOG_BAN = 0; |
||
28 | const LOG_ALLOW = 1; |
||
29 | const LOG_TEMPORARILY_BAN = 2; |
||
30 | const LOG_UNBAN = 9; |
||
31 | |||
32 | const LOG_LIMIT = 3; |
||
33 | const LOG_PAGEVIEW = 11; |
||
34 | const LOG_BLACKLIST = 98; |
||
35 | const LOG_CAPTCHA = 99; |
||
36 | |||
37 | /** |
||
38 | * Statistic data fields. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $fields = []; |
||
43 | |||
44 | /** |
||
45 | * Period type of the statistic data. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $periods = []; |
||
50 | |||
51 | /** |
||
52 | * Data detail. |
||
53 | * |
||
54 | * For example: |
||
55 | * $this->periodDetail['today']['12:00 am'][$field] = 7; |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $periodDetail = []; |
||
60 | |||
61 | /** |
||
62 | * IP Detail |
||
63 | * |
||
64 | * For example: |
||
65 | * $this->ipDetail['today']['127.0.0.1'][$fields] = 6; |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $ipDetail = []; |
||
70 | |||
71 | /** |
||
72 | * ActionLogger instance. |
||
73 | * |
||
74 | * @var ActionLogger |
||
75 | */ |
||
76 | protected $logger; |
||
77 | |||
78 | /** |
||
79 | * Period type. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $type = 'today'; |
||
84 | |||
85 | /** |
||
86 | * Constructer. |
||
87 | * |
||
88 | * @param string $directory The directory where to store the logs in. |
||
89 | */ |
||
90 | public function __construct(string $directory = '') |
||
162 | ]; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get the start and end date depends on the log type. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | protected function getStartEndDate(): array |
||
171 | { |
||
172 | switch ($this->type) { |
||
173 | |||
174 | case 'yesterday': |
||
175 | // Set start date and end date. |
||
176 | $startDate = date('Ymd', strtotime('yesterday')); |
||
177 | $endDate = date('Ymd'); |
||
178 | break; |
||
179 | |||
180 | case 'past_seven_days': |
||
181 | $startDate = date('Ymd', strtotime('-7 days')); |
||
182 | $endDate = date('Ymd'); |
||
183 | break; |
||
184 | |||
185 | case 'this_month': |
||
186 | $startDate = date('Ym') . '01'; |
||
187 | $endDate = date('Ym') . '31'; |
||
188 | break; |
||
189 | |||
190 | case 'last_month': |
||
191 | $startDate = date('Ym', strtotime('-1 month')) . '01'; |
||
192 | $endDate = date('Ym', strtotime('-1 month')) . '31'; |
||
193 | break; |
||
194 | |||
195 | case 'past_seven_hours': |
||
196 | $startDate = date('Ymd', strtotime('yesterday')); |
||
197 | $endDate = date('Ymd'); |
||
198 | break; |
||
199 | |||
200 | case 'today': |
||
201 | $startDate = date('Ymd'); |
||
202 | $endDate = ''; |
||
203 | break; |
||
204 | |||
205 | default: |
||
206 | |||
207 | // We also accept querying N days data from logs. For example: `past_365_days`. |
||
208 | if (preg_match('/past_([0-9]+)_days/', $this->type, $matches) ) { |
||
209 | |||
210 | $dayCount = $matches[1]; |
||
211 | $startDate = date('Ymd', strtotime('-' . $dayCount . ' days')); |
||
212 | $endDate = date('Ymd'); |
||
213 | |||
214 | $this->periods['past_' . $dayCount . '_days'] = [ |
||
215 | 'timesamp_begin' => strtotime(date('Ymd', strtotime('-' . $dayCount . ' days'))), |
||
216 | 'timesamp_end' => strtotime('today'), |
||
217 | 'display_format' => 'D', |
||
218 | 'display_count' => $dayCount, |
||
219 | 'period' => 86400, |
||
220 | ]; |
||
221 | |||
222 | } else { |
||
223 | $startDate = date('Ymd'); |
||
224 | $endDate = ''; |
||
225 | $this->periods[$this->type] = $this->periods['today']; |
||
226 | } |
||
227 | // endswitch; |
||
228 | } |
||
229 | |||
230 | return [ |
||
231 | 'start' => $startDate, |
||
232 | 'end' => $endDate, |
||
233 | ]; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Parse specific period of time of data. |
||
238 | * |
||
239 | * Warning: This method may take long time to generate real-time stats on a high-traffic website. |
||
240 | * Aprroximately 10,000 rows for 3-5 seconds, depnonds on your server's CPU speed. |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | public function parsePeriodData(): void |
||
245 | { |
||
246 | $dateRange = $this->getStartEndDate(); |
||
247 | $startDate = $dateRange['start']; |
||
248 | $endDate = $dateRange['end']; |
||
249 | |||
250 | // Fetch data from log files. |
||
251 | $logs = $this->logger->get($startDate, $endDate); |
||
252 | |||
253 | foreach ($logs as $log) { |
||
254 | |||
255 | $logTimesamp = (int) $log['timesamp']; |
||
256 | $logIp = $log['ip']; |
||
257 | |||
258 | // Add a new field `datetime` that original logs don't have. |
||
259 | $log['datetime'] = date('Y-m-d H:i:s', $logTimesamp); |
||
260 | |||
261 | foreach (array_keys($this->periods) as $t) { |
||
262 | |||
263 | for ($i = 0; $i < $this->periods[$t]['display_count']; $i++) { |
||
264 | |||
265 | $kTimesamp = $this->periods[$t]['timesamp_begin'] + ($i * $this->periods[$t]['period']); |
||
266 | |||
267 | $detailTimesampBegin = $kTimesamp; |
||
268 | $detailTimesampEnd = $kTimesamp + $this->periods[$t]['period']; |
||
269 | |||
270 | $k = date($this->periods[$t]['display_format'], $kTimesamp); |
||
271 | |||
272 | // Initialize all the counters. |
||
273 | foreach ($this->fields as $field) { |
||
274 | if (!isset($this->periodDetail[$t][$k][$field])) { |
||
275 | $this->periodDetail[$t][$k][$field] = 0; |
||
276 | } |
||
277 | |||
278 | if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) { |
||
279 | if (!isset($this->ipDetail[$t][$logIp][$field])) { |
||
280 | $this->ipDetail[$t][$logIp][$field] = 0; |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | // Initialize all the counters. |
||
286 | if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) { |
||
287 | $this->parse($log, $t, $k); |
||
288 | } |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Prepare data. |
||
296 | * |
||
297 | * @param string $type Period type. |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | public function prepare(string $type = 'today'): void |
||
302 | { |
||
303 | $this->type = $type; |
||
304 | |||
305 | $this->parsePeriodData($this->type); |
||
|
|||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get data |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getPeriodData() |
||
314 | { |
||
315 | if (!empty($this->periodDetail[$this->type])) { |
||
316 | return $this->periodDetail[$this->type]; |
||
317 | } |
||
318 | return []; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Get data |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getIpData() |
||
327 | { |
||
328 | if (!empty($this->ipDetail[$this->type])) { |
||
329 | return $this->ipDetail[$this->type]; |
||
330 | } |
||
331 | return []; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Get parsed perid data. |
||
336 | * |
||
337 | * @param string $ip IP address. |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | public function getParsedIpData($ip = ''): array |
||
342 | { |
||
343 | if (empty($ip)) { |
||
344 | return []; |
||
345 | } |
||
346 | |||
347 | $results['captcha_chart_string'] = ''; // string |
||
348 | $results['pageview_chart_string'] = ''; // string |
||
349 | $results['captcha_success_count'] = 0; // integer |
||
350 | $results['captcha_failure_count'] = 0; // integer |
||
351 | $results['captcha_count'] = 0; // integer |
||
352 | $results['pageview_count'] = 0; // integer |
||
353 | $results['captcha_percentageage'] = 0; // integer |
||
354 | $results['captcha_failure_percentage'] = 0; // integer |
||
355 | $results['captcha_success_percentage'] = 0; // integer |
||
356 | |||
357 | $results['action_ban_count'] = 0; // integer |
||
358 | $results['action_temp_ban_count'] = 0; // integer |
||
359 | $results['action_unban_count'] = 0; // integer |
||
360 | $results['blacklist_count'] = 0; // integer |
||
361 | $results['session_limit_count'] = 0; // integer |
||
362 | |||
363 | $ipdData = $this->getIpData(); |
||
364 | |||
365 | if (!empty($ipdData)) { |
||
366 | |||
367 | foreach ($ipdData as $ipKey => $ipInfo) { |
||
368 | |||
369 | if ($ipKey === $ip) { |
||
370 | $results['captcha_success_count'] += $ipInfo['captcha_success_count']; |
||
371 | $results['captcha_failure_count'] += $ipInfo['captcha_failure_count']; |
||
372 | $results['captcha_count'] += $ipInfo['captcha_count']; |
||
373 | $results['pageview_count'] += $ipInfo['pageview_count']; |
||
374 | |||
375 | $results['action_ban_count'] += $ipInfo['action_ban_count']; |
||
376 | $results['action_temp_ban_count'] += $ipInfo['action_temp_ban_count']; |
||
377 | $results['action_unban_count'] += $ipInfo['action_unban_count']; |
||
378 | $results['blacklist_count'] += $ipInfo['blacklist_count']; |
||
379 | $results['session_limit_count'] += $ipInfo['session_limit_count']; |
||
380 | } |
||
381 | } |
||
382 | |||
383 | if ($results['captcha_count'] > 0) { |
||
384 | $results['captcha_percentageage'] = (int) (round($results['captcha_count'] / ($results['captcha_count'] + $results['pageview_count']), 2) * 100); |
||
385 | $results['captcha_failure_percentage'] = (int) (round($results['captcha_failure_count'] / $results['captcha_count'], 2) * 100); |
||
386 | $results['captcha_success_percentage'] = (int) (round($results['captcha_success_count'] / $results['captcha_count'], 2) * 100); |
||
387 | } |
||
388 | } |
||
389 | |||
390 | return $results; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Get parsed perid data. |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | public function getParsedPeriodData(): array |
||
399 | { |
||
400 | $periodData = $this->getPeriodData(); |
||
401 | |||
402 | $results['captcha_chart_string'] = ''; // string |
||
403 | $results['pageview_chart_string'] = ''; // string |
||
404 | $results['label_chart_string'] = ''; // string |
||
405 | $results['captcha_success_count'] = 0; // integer |
||
406 | $results['captcha_failure_count'] = 0; // integer |
||
407 | $results['captcha_count'] = 0; // integer |
||
408 | $results['pageview_count'] = 0; // integer |
||
409 | $results['captcha_percentageage'] = 0; // integer |
||
410 | $results['captcha_failure_percentage'] = 0; // integer |
||
411 | $results['captcha_success_percentage'] = 0; // integer |
||
412 | |||
413 | $results['action_ban_count'] = 0; // integer |
||
414 | $results['action_temp_ban_count'] = 0; // integer |
||
415 | $results['action_unban_count'] = 0; // integer |
||
416 | $results['blacklist_count'] = 0; // integer |
||
417 | $results['session_limit_count'] = 0; // integer |
||
418 | |||
419 | if (!empty($periodData)) { |
||
420 | |||
421 | $chartCaptcha = []; |
||
422 | $chartPageview = []; |
||
423 | $chartCaptchaSuccess = []; |
||
424 | $chartCaptchaFailure = []; |
||
425 | $labels = []; |
||
426 | |||
427 | foreach ($periodData as $label => $period) { |
||
428 | $chartCaptcha[] = $period['captcha_count']; |
||
429 | $chartPageview[] = $period['pageview_count']; |
||
430 | $chartCaptchaSuccess[] = $period['captcha_success_count']; |
||
431 | $chartCaptchaFailure[] = $period['captcha_failure_count']; |
||
432 | $labels[] = $label; |
||
433 | |||
434 | $results['captcha_success_count'] += $period['captcha_success_count']; |
||
435 | $results['captcha_failure_count'] += $period['captcha_failure_count']; |
||
436 | $results['captcha_count'] += $period['captcha_count']; |
||
437 | $results['pageview_count'] += $period['pageview_count']; |
||
438 | |||
439 | $results['action_ban_count'] += $period['action_ban_count']; |
||
440 | $results['action_temp_ban_count'] += $period['action_temp_ban_count']; |
||
441 | $results['action_unban_count'] += $period['action_unban_count']; |
||
442 | $results['blacklist_count'] += $period['blacklist_count']; |
||
443 | $results['session_limit_count'] += $period['session_limit_count']; |
||
444 | } |
||
445 | |||
446 | $results['captcha_chart_string'] = implode(',', $chartCaptcha); |
||
447 | $results['pageview_chart_string']= implode(',', $chartPageview); |
||
448 | $results['captcha_success_chart_string'] = implode(',', $chartCaptchaSuccess); |
||
449 | $results['captcha_failure_chart_string'] = implode(',', $chartCaptchaFailure); |
||
450 | $results['label_chart_string'] = "'" . implode("','", $labels) . "'"; |
||
451 | |||
452 | if ($results['captcha_count'] > 0) { |
||
453 | $results['captcha_percentageage'] = (int) (round($results['captcha_count'] / ($results['captcha_count'] + $results['pageview_count']), 2) * 100); |
||
454 | $results['captcha_failure_percentage'] = (int) (round($results['captcha_failure_count'] / $results['captcha_count'], 2) * 100); |
||
455 | $results['captcha_success_percentage'] = (int) (round($results['captcha_success_count'] / $results['captcha_count'], 2) * 100); |
||
456 | } |
||
457 | } |
||
458 | |||
459 | return $results; |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Parse log data for showing on dashboard. |
||
464 | * |
||
465 | * @param array $logActionCode The log action code. |
||
466 | * @param string $t Time period type. (For example: `today`, `yesterday`, `past_seven_days`) |
||
467 | * @param string $k Time period key. (For example: `12:00 am`, `20190812`) |
||
468 | * |
||
469 | * @return void |
||
470 | */ |
||
471 | private function parse($log, $t, $k): void |
||
472 | { |
||
473 | $logActionCode = (int) $log['action_code']; |
||
474 | $ip = $log['ip']; |
||
475 | $sessionId = $log['session_id']; |
||
476 | |||
477 | $this->ipDetail[$t][$ip]['session_id'][$sessionId ] = 1; |
||
478 | |||
479 | if ($logActionCode === self::LOG_TEMPORARILY_BAN) { |
||
480 | $this->periodDetail[$t][$k]['action_temp_ban_count']++; |
||
481 | $this->periodDetail[$t][$k]['captcha_count']++; |
||
482 | $this->periodDetail[$t][$k]['captcha_failure_count']++; |
||
483 | |||
484 | $this->ipDetail[$t][$ip]['action_temp_ban_count']++; |
||
485 | $this->ipDetail[$t][$ip]['captcha_count']++; |
||
486 | $this->ipDetail[$t][$ip]['captcha_failure_count']++; |
||
487 | } |
||
488 | |||
489 | if ($logActionCode === self::LOG_BAN) { |
||
490 | $this->periodDetail[$t][$k]['action_ban_count']++; |
||
491 | $this->ipDetail[$t][$ip]['action_ban_count']++; |
||
492 | } |
||
493 | |||
494 | if ($logActionCode === self::LOG_UNBAN) { |
||
495 | $this->periodDetail[$t][$k]['action_unban_count']++; |
||
496 | $this->periodDetail[$t][$k]['captcha_success_count']++; |
||
497 | $this->periodDetail[$t][$k]['captcha_failure_count']--; |
||
498 | |||
499 | $this->ipDetail[$t][$ip]['action_unban_count']++; |
||
500 | $this->ipDetail[$t][$ip]['captcha_success_count']++; |
||
501 | $this->ipDetail[$t][$ip]['captcha_failure_count']--; |
||
502 | } |
||
503 | |||
504 | if ($logActionCode === self::LOG_CAPTCHA) { |
||
505 | $this->periodDetail[$t][$k]['captcha_count']++; |
||
506 | $this->periodDetail[$t][$k]['captcha_failure_count']++; |
||
507 | |||
508 | $this->ipDetail[$t][$ip]['captcha_count']++; |
||
509 | $this->ipDetail[$t][$ip]['captcha_failure_count']++; |
||
510 | } |
||
511 | |||
512 | if ($logActionCode === self::LOG_BLACKLIST) { |
||
513 | $this->periodDetail[$t][$k]['blacklist_count']++; |
||
514 | $this->ipDetail[$t][$ip]['blacklist_count']++; |
||
515 | } |
||
516 | |||
517 | if ($logActionCode === self::LOG_PAGEVIEW) { |
||
518 | $this->periodDetail[$t][$k]['pageview_count']++; |
||
519 | $this->ipDetail[$t][$ip]['pageview_count']++; |
||
520 | } |
||
521 | |||
522 | if ($logActionCode === self::LOG_LIMIT) { |
||
523 | $this->periodDetail[$t][$k]['session_limit_count']++; |
||
524 | $this->ipDetail[$t][$ip]['session_limit_count']++; |
||
525 | } |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Return current log's directory. |
||
530 | * |
||
531 | * @return string |
||
532 | */ |
||
533 | public function getDirectory(): string |
||
536 | } |
||
537 | } |
||
538 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.