Conditions | 36 |
Paths | > 20000 |
Total Lines | 286 |
Code Lines | 167 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
365 | private function parseHistory(Page $page) |
||
366 | { |
||
367 | $revStmt = $page->getRevisionsStmt(); |
||
368 | $revCount = 0; |
||
369 | |||
370 | /** @var string[] Master array containing all the data we need */ |
||
371 | $data = [ |
||
372 | 'general' => [ |
||
373 | 'max_add' => null, // Edit |
||
374 | 'max_del' => null, // Edit |
||
375 | 'editor_count' => 0, |
||
376 | 'anon_count' => 0, |
||
377 | 'minor_count' => 0, |
||
378 | 'count_history' => ['day' => 0, 'week' => 0, 'month' => 0, 'year' => 0], |
||
379 | 'current_size' => null, |
||
380 | 'textshares' => [], |
||
381 | 'textshare_total' => 0, |
||
382 | 'automated_count' => 0, |
||
383 | 'revert_count' => 0, |
||
384 | 'added' => 0, |
||
385 | ], |
||
386 | 'max_edits_per_month' => 0, // for bar chart in "Month counts" section |
||
387 | 'editors' => [], |
||
388 | 'anons' => [], |
||
389 | 'year_count' => [], |
||
390 | 'tools' => [], |
||
391 | ]; |
||
392 | |||
393 | /** @var Edit|null */ |
||
394 | $firstEdit = null; |
||
395 | |||
396 | /** @var Edit|null The previous edit, used to discount content that was reverted */ |
||
397 | $prevEdit = null; |
||
398 | |||
399 | /** |
||
400 | * The edit previously deemed as having the maximum amount of content added. |
||
401 | * This is used to discount content that was reverted. |
||
402 | * @var Edit|null |
||
403 | */ |
||
404 | $prevMaxAddEdit = null; |
||
405 | |||
406 | /** |
||
407 | * The edit previously deemed as having the maximum amount of content deleted. |
||
408 | * This is used to discount content that was reverted |
||
409 | * @var Edit|null |
||
410 | */ |
||
411 | $prevMaxDelEdit = null; |
||
412 | |||
413 | /** @var Time|null Time of first revision, used as a comparison for month counts */ |
||
414 | $firstEditMonth = null; |
||
415 | |||
416 | while ($rev = $revStmt->fetch()) { |
||
417 | $edit = new Edit($this->pageInfo['page'], $rev); |
||
418 | // $edit->setContainer($this->container); |
||
1 ignored issue
–
show
|
|||
419 | |||
420 | // Some shorthands |
||
421 | $editYear = $edit->getYear(); |
||
422 | $editMonth = $edit->getMonth(); |
||
423 | $editTimestamp = $edit->getTimestamp(); |
||
424 | |||
425 | // Don't return actual edit size if last revision had a length of null. |
||
426 | // This happens when the edit follows other edits that were revision-deleted. |
||
427 | // See T148857 for more information. |
||
428 | // @TODO: Remove once T101631 is resolved |
||
429 | if ($prevEdit && $prevEdit->getLength() === null) { |
||
430 | $editSize = 0; |
||
431 | } else { |
||
432 | $editSize = $edit->getSize(); |
||
433 | } |
||
434 | |||
435 | if ($revCount === 0) { |
||
436 | $firstEdit = $edit; |
||
437 | $firstEditMonth = mktime(0, 0, 0, (int) $firstEdit->getMonth(), 1, $firstEdit->getYear()); |
||
438 | } |
||
439 | |||
440 | $username = $edit->getUser()->getUsername(); |
||
441 | |||
442 | // Sometimes, with old revisions (2001 era), the revisions from 2002 come before 2001 |
||
443 | if ($editTimestamp < $firstEdit->getTimestamp()) { |
||
444 | $firstEdit = $edit; |
||
445 | } |
||
446 | |||
447 | // Fill in the blank arrays for the year and 12 months |
||
448 | if (!isset($data['year_count'][$editYear])) { |
||
449 | $data['year_count'][$editYear] = [ |
||
450 | 'all' => 0, |
||
451 | 'minor' => 0, |
||
452 | 'anon' => 0, |
||
453 | 'automated' => 0, |
||
454 | 'size' => 0, // keep track of the size by the end of the year |
||
455 | 'events' => [], |
||
456 | 'months' => [], |
||
457 | ]; |
||
458 | |||
459 | for ($i = 1; $i <= 12; $i++) { |
||
460 | $timeObj = mktime(0, 0, 0, $i, 1, $editYear); |
||
461 | |||
462 | // don't show zeros for months before the first edit or after the current month |
||
463 | if ($timeObj < $firstEditMonth || $timeObj > strtotime('last day of this month')) { |
||
464 | continue; |
||
465 | } |
||
466 | |||
467 | $data['year_count'][$editYear]['months'][sprintf('%02d', $i)] = [ |
||
468 | 'all' => 0, |
||
469 | 'minor' => 0, |
||
470 | 'anon' => 0, |
||
471 | 'automated' => 0, |
||
472 | ]; |
||
473 | } |
||
474 | } |
||
475 | |||
476 | // Increment year and month counts for all edits |
||
477 | $data['year_count'][$editYear]['all']++; |
||
478 | $data['year_count'][$editYear]['months'][$editMonth]['all']++; |
||
479 | // This will ultimately be the size of the page by the end of the year |
||
480 | $data['year_count'][$editYear]['size'] = $edit->getLength(); |
||
481 | |||
482 | // Keep track of which month had the most edits |
||
483 | $editsThisMonth = $data['year_count'][$editYear]['months'][$editMonth]['all']; |
||
484 | if ($editsThisMonth > $data['max_edits_per_month']) { |
||
485 | $data['max_edits_per_month'] = $editsThisMonth; |
||
486 | } |
||
487 | |||
488 | // Initialize various user stats |
||
489 | if (!isset($data['editors'][$username])) { |
||
490 | $data['general']['editor_count']++; |
||
491 | $data['editors'][$username] = [ |
||
492 | 'all' => 0, |
||
493 | 'minor' => 0, |
||
494 | 'minor_percentage' => 0, |
||
495 | 'first' => $editTimestamp, |
||
496 | 'first_id' => $edit->getId(), |
||
497 | 'last' => null, |
||
498 | 'atbe' => null, |
||
499 | 'added' => 0, |
||
500 | 'sizes' => [], |
||
501 | ]; |
||
502 | } |
||
503 | |||
504 | // Increment user counts |
||
505 | $data['editors'][$username]['all']++; |
||
506 | $data['editors'][$username]['last'] = $editTimestamp; |
||
507 | $data['editors'][$username]['last_id'] = $edit->getId(); |
||
508 | |||
509 | // Store number of KB added with this edit |
||
510 | $data['editors'][$username]['sizes'][] = $edit->getLength() / 1024; |
||
511 | |||
512 | // Check if it was a revert |
||
513 | if ($this->aeh->isRevert($edit->getComment())) { |
||
514 | $data['general']['revert_count']++; |
||
515 | |||
516 | // Since this was a revert, we don't want to treat the previous |
||
517 | // edit as legit content addition or removal |
||
518 | if ($prevEdit && $prevEdit->getSize() > 0) { |
||
519 | $data['general']['added'] -= $prevEdit->getSize(); |
||
520 | } |
||
521 | |||
522 | // @TODO: Test this against an edit war (use your sandbox) |
||
523 | // Also remove as max added or deleted, if applicable |
||
524 | if ($data['general']['max_add'] && |
||
525 | $prevEdit->getId() === $data['general']['max_add']->getId() |
||
526 | ) { |
||
527 | $data['general']['max_add'] = $prevMaxAddEdit; |
||
528 | $prevMaxAddEdit = $prevEdit; // in the event of edit wars |
||
529 | } elseif ($data['general']['max_del'] && |
||
530 | $prevEdit->getId() === $data['general']['max_del']->getId() |
||
531 | ) { |
||
532 | $data['general']['max_del'] = $prevMaxDelEdit; |
||
533 | $prevMaxDelEdit = $prevEdit; // in the event of edit wars |
||
534 | } |
||
535 | } else { |
||
536 | // Edit was not a revert, so treat size > 0 as content added |
||
537 | if ($editSize > 0) { |
||
538 | $data['general']['added'] += $editSize; |
||
539 | $data['editors'][$username]['added'] += $editSize; |
||
540 | |||
541 | // Keep track of edit with max addition |
||
542 | if (!$data['general']['max_add'] || $editSize > $data['general']['max_add']->getSize()) { |
||
543 | // Keep track of old max_add in case we find out the next $edit was reverted |
||
544 | // (and was also a max edit), in which case we'll want to use this one ($edit) |
||
545 | $prevMaxAddEdit = $data['general']['max_add']; |
||
546 | |||
547 | $data['general']['max_add'] = $edit; |
||
548 | } |
||
549 | } elseif ($editSize < 0 && ( |
||
550 | !$data['general']['max_del'] || $editSize < $data['general']['max_del']->getSize() |
||
551 | )) { |
||
552 | $data['general']['max_del'] = $edit; |
||
553 | } |
||
554 | } |
||
555 | |||
556 | // If anonymous, increase counts |
||
557 | if ($edit->isAnon()) { |
||
558 | $data['general']['anon_count']++; |
||
559 | $data['year_count'][$editYear]['anon']++; |
||
560 | $data['year_count'][$editYear]['months'][$editMonth]['anon']++; |
||
561 | } |
||
562 | |||
563 | // If minor edit, increase counts |
||
564 | if ($edit->isMinor()) { |
||
565 | $data['general']['minor_count']++; |
||
566 | $data['year_count'][$editYear]['minor']++; |
||
567 | $data['year_count'][$editYear]['months'][$editMonth]['minor']++; |
||
568 | |||
569 | // Increment minor counts for this user |
||
570 | $data['editors'][$username]['minor']++; |
||
571 | } |
||
572 | |||
573 | $automatedTool = $this->aeh->getTool($edit->getComment()); |
||
574 | if ($automatedTool) { |
||
575 | $data['general']['automated_count']++; |
||
576 | $data['year_count'][$editYear]['automated']++; |
||
577 | $data['year_count'][$editYear]['months'][$editMonth]['automated']++; |
||
578 | |||
579 | if (!isset($data['tools'][$automatedTool])) { |
||
580 | $data['tools'][$automatedTool] = [ |
||
581 | 'count' => 1, |
||
582 | 'link' => $this->aeh->getTools()[$automatedTool]['link'], |
||
583 | ]; |
||
584 | } else { |
||
585 | $data['tools'][$automatedTool]['count']++; |
||
586 | } |
||
587 | } |
||
588 | |||
589 | // Increment "edits per <time>" counts |
||
590 | if ($editTimestamp > new DateTime('-1 day')) { |
||
591 | $data['general']['count_history']['day']++; |
||
592 | } |
||
593 | if ($editTimestamp > new DateTime('-1 week')) { |
||
594 | $data['general']['count_history']['week']++; |
||
595 | } |
||
596 | if ($editTimestamp > new DateTime('-1 month')) { |
||
597 | $data['general']['count_history']['month']++; |
||
598 | } |
||
599 | if ($editTimestamp > new DateTime('-1 year')) { |
||
600 | $data['general']['count_history']['year']++; |
||
601 | } |
||
602 | |||
603 | $revCount++; |
||
604 | $prevEdit = $edit; |
||
605 | $lastEdit = $edit; |
||
606 | } |
||
607 | |||
608 | // add percentages |
||
609 | $data['general']['minor_percentage'] = round( |
||
610 | ($data['general']['minor_count'] / $revCount) * 100, |
||
611 | 1 |
||
612 | ); |
||
613 | $data['general']['anon_percentage'] = round( |
||
614 | ($data['general']['anon_count'] / $revCount) * 100, |
||
615 | 1 |
||
616 | ); |
||
617 | |||
618 | // other general statistics |
||
619 | $dateFirst = $firstEdit->getTimestamp(); |
||
620 | $dateLast = $lastEdit->getTimestamp(); |
||
621 | $data['general']['datetime_first_edit'] = $dateFirst; |
||
622 | $data['general']['datetime_last_edit'] = $dateLast; |
||
623 | $interval = date_diff($dateLast, $dateFirst, true); |
||
624 | |||
625 | $data['totaldays'] = $interval->format('%a'); |
||
626 | $data['general']['average_days_per_edit'] = round($data['totaldays'] / $revCount, 1); |
||
627 | $editsPerDay = $data['totaldays'] |
||
628 | ? $revCount / ($data['totaldays'] / (365 / 12 / 24)) |
||
629 | : 0; |
||
630 | $data['general']['edits_per_day'] = round($editsPerDay, 1); |
||
631 | $editsPerMonth = $data['totaldays'] |
||
632 | ? $revCount / ($data['totaldays'] / (365 / 12)) |
||
633 | : 0; |
||
634 | $data['general']['edits_per_month'] = round($editsPerMonth, 1); |
||
635 | $editsPerYear = $data['totaldays'] |
||
636 | ? $revCount / ($data['totaldays'] / 365) |
||
637 | : 0; |
||
638 | $data['general']['edits_per_year'] = round($editsPerYear, 1); |
||
639 | $data['general']['edits_per_editor'] = round($revCount / count($data['editors']), 1); |
||
640 | |||
641 | $data['firstEdit'] = $firstEdit; |
||
642 | $data['lastEdit'] = $lastEdit; |
||
643 | |||
644 | // Various sorts |
||
645 | arsort($data['editors']); |
||
646 | arsort($data['tools']); |
||
647 | ksort($data['year_count']); |
||
648 | |||
649 | return $data; |
||
650 | } |
||
651 | } |
||
652 |
This check marks private properties in classes that are never used. Those properties can be removed.