Conditions | 36 |
Paths | > 20000 |
Total Lines | 291 |
Code Lines | 168 |
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 |
||
415 | private function parseHistory(Page $page) |
||
416 | { |
||
417 | $revStmt = $page->getRevisionsStmt(); |
||
418 | $revCount = 0; |
||
419 | |||
420 | /** @var string[] Master array containing all the data we need */ |
||
421 | $data = [ |
||
422 | 'general' => [ |
||
423 | 'max_add' => null, // Edit |
||
424 | 'max_del' => null, // Edit |
||
425 | 'editor_count' => 0, |
||
426 | 'anon_count' => 0, |
||
427 | 'minor_count' => 0, |
||
428 | 'count_history' => ['day' => 0, 'week' => 0, 'month' => 0, 'year' => 0], |
||
429 | 'current_size' => null, |
||
430 | 'textshares' => [], |
||
431 | 'textshare_total' => 0, |
||
432 | 'automated_count' => 0, |
||
433 | 'revert_count' => 0, |
||
434 | 'added' => 0, |
||
435 | ], |
||
436 | 'max_edits_per_month' => 0, // for bar chart in "Month counts" section |
||
437 | 'editors' => [], |
||
438 | 'anons' => [], |
||
439 | 'year_count' => [], |
||
440 | 'tools' => [], |
||
441 | ]; |
||
442 | |||
443 | /** @var Edit|null */ |
||
444 | $firstEdit = null; |
||
445 | |||
446 | /** @var Edit|null The previous edit, used to discount content that was reverted */ |
||
447 | $prevEdit = null; |
||
448 | |||
449 | /** |
||
450 | * The edit previously deemed as having the maximum amount of content added. |
||
451 | * This is used to discount content that was reverted. |
||
452 | * @var Edit|null |
||
453 | */ |
||
454 | $prevMaxAddEdit = null; |
||
455 | |||
456 | /** |
||
457 | * The edit previously deemed as having the maximum amount of content deleted. |
||
458 | * This is used to discount content that was reverted |
||
459 | * @var Edit|null |
||
460 | */ |
||
461 | $prevMaxDelEdit = null; |
||
462 | |||
463 | /** |
||
464 | * The last edit made to the article. |
||
465 | * @var Edit|null |
||
466 | */ |
||
467 | $lastEdit = null; |
||
468 | |||
469 | /** @var Time|null Time of first revision, used as a comparison for month counts */ |
||
470 | $firstEditMonth = null; |
||
471 | |||
472 | while ($rev = $revStmt->fetch()) { |
||
473 | $edit = new Edit($this->pageInfo['page'], $rev); |
||
474 | |||
475 | // Some shorthands |
||
476 | $editYear = $edit->getYear(); |
||
477 | $editMonth = $edit->getMonth(); |
||
478 | $editTimestamp = $edit->getTimestamp(); |
||
479 | |||
480 | // Don't return actual edit size if last revision had a length of null. |
||
481 | // This happens when the edit follows other edits that were revision-deleted. |
||
482 | // See T148857 for more information. |
||
483 | // @TODO: Remove once T101631 is resolved |
||
484 | if ($prevEdit && $prevEdit->getLength() === null) { |
||
485 | $editSize = 0; |
||
486 | } else { |
||
487 | $editSize = $edit->getSize(); |
||
488 | } |
||
489 | |||
490 | if ($revCount === 0) { |
||
491 | $firstEdit = $edit; |
||
492 | $firstEditMonth = mktime(0, 0, 0, (int) $firstEdit->getMonth(), 1, $firstEdit->getYear()); |
||
493 | } |
||
494 | |||
495 | $username = $edit->getUser()->getUsername(); |
||
496 | |||
497 | // Sometimes, with old revisions (2001 era), the revisions from 2002 come before 2001 |
||
498 | if ($editTimestamp < $firstEdit->getTimestamp()) { |
||
499 | $firstEdit = $edit; |
||
500 | } |
||
501 | |||
502 | // Fill in the blank arrays for the year and 12 months |
||
503 | if (!isset($data['year_count'][$editYear])) { |
||
504 | $data['year_count'][$editYear] = [ |
||
505 | 'all' => 0, |
||
506 | 'minor' => 0, |
||
507 | 'anon' => 0, |
||
508 | 'automated' => 0, |
||
509 | 'size' => 0, // keep track of the size by the end of the year |
||
510 | 'events' => [], |
||
511 | 'months' => [], |
||
512 | ]; |
||
513 | |||
514 | for ($i = 1; $i <= 12; $i++) { |
||
515 | $timeObj = mktime(0, 0, 0, $i, 1, $editYear); |
||
516 | |||
517 | // don't show zeros for months before the first edit or after the current month |
||
518 | if ($timeObj < $firstEditMonth || $timeObj > strtotime('last day of this month')) { |
||
519 | continue; |
||
520 | } |
||
521 | |||
522 | $data['year_count'][$editYear]['months'][sprintf('%02d', $i)] = [ |
||
523 | 'all' => 0, |
||
524 | 'minor' => 0, |
||
525 | 'anon' => 0, |
||
526 | 'automated' => 0, |
||
527 | ]; |
||
528 | } |
||
529 | } |
||
530 | |||
531 | // Increment year and month counts for all edits |
||
532 | $data['year_count'][$editYear]['all']++; |
||
533 | $data['year_count'][$editYear]['months'][$editMonth]['all']++; |
||
534 | // This will ultimately be the size of the page by the end of the year |
||
535 | $data['year_count'][$editYear]['size'] = $edit->getLength(); |
||
536 | |||
537 | // Keep track of which month had the most edits |
||
538 | $editsThisMonth = $data['year_count'][$editYear]['months'][$editMonth]['all']; |
||
539 | if ($editsThisMonth > $data['max_edits_per_month']) { |
||
540 | $data['max_edits_per_month'] = $editsThisMonth; |
||
541 | } |
||
542 | |||
543 | // Initialize various user stats |
||
544 | if (!isset($data['editors'][$username])) { |
||
545 | $data['general']['editor_count']++; |
||
546 | $data['editors'][$username] = [ |
||
547 | 'all' => 0, |
||
548 | 'minor' => 0, |
||
549 | 'minor_percentage' => 0, |
||
550 | 'first' => $editTimestamp, |
||
551 | 'first_id' => $edit->getId(), |
||
552 | 'last' => null, |
||
553 | 'atbe' => null, |
||
554 | 'added' => 0, |
||
555 | 'sizes' => [], |
||
556 | ]; |
||
557 | } |
||
558 | |||
559 | // Increment user counts |
||
560 | $data['editors'][$username]['all']++; |
||
561 | $data['editors'][$username]['last'] = $editTimestamp; |
||
562 | $data['editors'][$username]['last_id'] = $edit->getId(); |
||
563 | |||
564 | // Store number of KB added with this edit |
||
565 | $data['editors'][$username]['sizes'][] = $edit->getLength() / 1024; |
||
566 | |||
567 | // Check if it was a revert |
||
568 | if ($edit->isRevert($this->container)) { |
||
1 ignored issue
–
show
|
|||
569 | $data['general']['revert_count']++; |
||
570 | |||
571 | // Since this was a revert, we don't want to treat the previous |
||
572 | // edit as legit content addition or removal |
||
573 | if ($prevEdit && $prevEdit->getSize() > 0) { |
||
574 | $data['general']['added'] -= $prevEdit->getSize(); |
||
575 | } |
||
576 | |||
577 | // @TODO: Test this against an edit war (use your sandbox) |
||
578 | // Also remove as max added or deleted, if applicable |
||
579 | if ($data['general']['max_add'] && |
||
580 | $prevEdit->getId() === $data['general']['max_add']->getId() |
||
581 | ) { |
||
582 | $data['general']['max_add'] = $prevMaxAddEdit; |
||
583 | $prevMaxAddEdit = $prevEdit; // in the event of edit wars |
||
584 | } elseif ($data['general']['max_del'] && |
||
585 | $prevEdit->getId() === $data['general']['max_del']->getId() |
||
586 | ) { |
||
587 | $data['general']['max_del'] = $prevMaxDelEdit; |
||
588 | $prevMaxDelEdit = $prevEdit; // in the event of edit wars |
||
589 | } |
||
590 | } else { |
||
591 | // Edit was not a revert, so treat size > 0 as content added |
||
592 | if ($editSize > 0) { |
||
593 | $data['general']['added'] += $editSize; |
||
594 | $data['editors'][$username]['added'] += $editSize; |
||
595 | |||
596 | // Keep track of edit with max addition |
||
597 | if (!$data['general']['max_add'] || $editSize > $data['general']['max_add']->getSize()) { |
||
598 | // Keep track of old max_add in case we find out the next $edit was reverted |
||
599 | // (and was also a max edit), in which case we'll want to use this one ($edit) |
||
600 | $prevMaxAddEdit = $data['general']['max_add']; |
||
601 | |||
602 | $data['general']['max_add'] = $edit; |
||
603 | } |
||
604 | } elseif ($editSize < 0 && ( |
||
605 | !$data['general']['max_del'] || $editSize < $data['general']['max_del']->getSize() |
||
606 | )) { |
||
607 | $data['general']['max_del'] = $edit; |
||
608 | } |
||
609 | } |
||
610 | |||
611 | // If anonymous, increase counts |
||
612 | if ($edit->isAnon()) { |
||
613 | $data['general']['anon_count']++; |
||
614 | $data['year_count'][$editYear]['anon']++; |
||
615 | $data['year_count'][$editYear]['months'][$editMonth]['anon']++; |
||
616 | } |
||
617 | |||
618 | // If minor edit, increase counts |
||
619 | if ($edit->isMinor()) { |
||
620 | $data['general']['minor_count']++; |
||
621 | $data['year_count'][$editYear]['minor']++; |
||
622 | $data['year_count'][$editYear]['months'][$editMonth]['minor']++; |
||
623 | |||
624 | // Increment minor counts for this user |
||
625 | $data['editors'][$username]['minor']++; |
||
626 | } |
||
627 | |||
628 | $automatedTool = $edit->getTool($this->container); |
||
1 ignored issue
–
show
|
|||
629 | if ($automatedTool !== false) { |
||
630 | $data['general']['automated_count']++; |
||
631 | $data['year_count'][$editYear]['automated']++; |
||
632 | $data['year_count'][$editYear]['months'][$editMonth]['automated']++; |
||
633 | |||
634 | if (!isset($data['tools'][$automatedTool['name']])) { |
||
635 | $data['tools'][$automatedTool['name']] = [ |
||
636 | 'count' => 1, |
||
637 | 'link' => $automatedTool['link'], |
||
638 | ]; |
||
639 | } else { |
||
640 | $data['tools'][$automatedTool['name']]['count']++; |
||
641 | } |
||
642 | } |
||
643 | |||
644 | // Increment "edits per <time>" counts |
||
645 | if ($editTimestamp > new DateTime('-1 day')) { |
||
646 | $data['general']['count_history']['day']++; |
||
647 | } |
||
648 | if ($editTimestamp > new DateTime('-1 week')) { |
||
649 | $data['general']['count_history']['week']++; |
||
650 | } |
||
651 | if ($editTimestamp > new DateTime('-1 month')) { |
||
652 | $data['general']['count_history']['month']++; |
||
653 | } |
||
654 | if ($editTimestamp > new DateTime('-1 year')) { |
||
655 | $data['general']['count_history']['year']++; |
||
656 | } |
||
657 | |||
658 | $revCount++; |
||
659 | $prevEdit = $edit; |
||
660 | $lastEdit = $edit; |
||
661 | } |
||
662 | |||
663 | // add percentages |
||
664 | $data['general']['minor_percentage'] = round( |
||
665 | ($data['general']['minor_count'] / $revCount) * 100, |
||
666 | 1 |
||
667 | ); |
||
668 | $data['general']['anon_percentage'] = round( |
||
669 | ($data['general']['anon_count'] / $revCount) * 100, |
||
670 | 1 |
||
671 | ); |
||
672 | |||
673 | // other general statistics |
||
674 | $dateFirst = $firstEdit->getTimestamp(); |
||
675 | $dateLast = $lastEdit->getTimestamp(); |
||
676 | $data['general']['datetime_first_edit'] = $dateFirst; |
||
677 | $data['general']['datetime_last_edit'] = $dateLast; |
||
678 | $interval = date_diff($dateLast, $dateFirst, true); |
||
679 | |||
680 | $data['totaldays'] = $interval->format('%a'); |
||
681 | $data['general']['average_days_per_edit'] = round($data['totaldays'] / $revCount, 1); |
||
682 | $editsPerDay = $data['totaldays'] |
||
683 | ? $revCount / ($data['totaldays'] / (365 / 12 / 24)) |
||
684 | : 0; |
||
685 | $data['general']['edits_per_day'] = round($editsPerDay, 1); |
||
686 | $editsPerMonth = $data['totaldays'] |
||
687 | ? $revCount / ($data['totaldays'] / (365 / 12)) |
||
688 | : 0; |
||
689 | $data['general']['edits_per_month'] = round($editsPerMonth, 1); |
||
690 | $editsPerYear = $data['totaldays'] |
||
691 | ? $revCount / ($data['totaldays'] / 365) |
||
692 | : 0; |
||
693 | $data['general']['edits_per_year'] = round($editsPerYear, 1); |
||
694 | $data['general']['edits_per_editor'] = round($revCount / count($data['editors']), 1); |
||
695 | |||
696 | $data['firstEdit'] = $firstEdit; |
||
697 | $data['lastEdit'] = $lastEdit; |
||
698 | |||
699 | // Various sorts |
||
700 | arsort($data['editors']); |
||
701 | arsort($data['tools']); |
||
702 | ksort($data['year_count']); |
||
703 | |||
704 | return $data; |
||
705 | } |
||
706 | } |
||
707 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: