| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 250 |
| Code Lines | 157 |
| Lines | 12 |
| Ratio | 4.8 % |
| 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 |
||
| 425 | private function parseHistory() |
||
| 426 | { |
||
| 427 | $revisionCount = $this->pageInfo['page']->getNumRevisions(); |
||
| 428 | if ($revisionCount == 0) { |
||
| 429 | // $this->error = "no records"; |
||
|
1 ignored issue
–
show
|
|||
| 430 | return; |
||
| 431 | } |
||
| 432 | |||
| 433 | $firstEdit = $this->pageInfo['firstEdit']; |
||
| 434 | |||
| 435 | // Get UNIX timestamp of the first day of the month of the first edit |
||
| 436 | // This is used as a comparison when building our array of per-month stats |
||
| 437 | $firstEditMonth = mktime(0, 0, 0, (int) $firstEdit->getMonth(), 1, $firstEdit->getYear()); |
||
| 438 | |||
| 439 | $lastEdit = $this->pageInfo['lastEdit']; |
||
| 440 | $secondLastEdit = $revisionCount === 1 ? $lastEdit : $this->pageHistory[ $revisionCount - 2 ]; |
||
| 441 | |||
| 442 | // Now we can start our master array. This one will be HUGE! |
||
| 443 | $data = [ |
||
| 444 | 'general' => [ |
||
| 445 | 'max_add' => $firstEdit, |
||
| 446 | 'max_del' => $firstEdit, |
||
| 447 | 'editor_count' => 0, |
||
| 448 | 'anon_count' => 0, |
||
| 449 | 'minor_count' => 0, |
||
| 450 | 'count_history' => ['day' => 0, 'week' => 0, 'month' => 0, 'year' => 0], |
||
| 451 | 'current_size' => $this->pageHistory[$revisionCount-1]['length'], |
||
| 452 | 'textshares' => [], |
||
| 453 | 'textshare_total' => 0, |
||
| 454 | 'automated_count' => 0, |
||
| 455 | 'revert_count' => 0, |
||
| 456 | 'added' => 0, |
||
| 457 | ], |
||
| 458 | 'max_edits_per_month' => 0, // for bar chart in "Month counts" section |
||
| 459 | 'editors' => [], |
||
| 460 | 'anons' => [], |
||
| 461 | 'year_count' => [], |
||
| 462 | 'tools' => [], |
||
| 463 | ]; |
||
| 464 | |||
| 465 | // restore existing general data |
||
| 466 | $data['general'] = array_merge($data['general'], $this->pageInfo['general']); |
||
| 467 | |||
| 468 | // And now comes the logic for filling said master array |
||
| 469 | foreach ($this->pageHistory as $i => $rev) { |
||
| 470 | $edit = new Edit($this->pageInfo['page'], $rev); |
||
| 471 | $diffSize = $this->getDiffSize($i); |
||
| 472 | $username = htmlspecialchars($rev['username']); |
||
| 473 | |||
| 474 | // Sometimes, with old revisions (2001 era), the revisions from 2002 come before 2001 |
||
| 475 | if ($edit->getTimestamp() < $firstEdit->getTimestamp()) { |
||
| 476 | $firstEdit = $edit; |
||
| 477 | } |
||
| 478 | |||
| 479 | // Fill in the blank arrays for the year and 12 months |
||
| 480 | if (!isset($data['year_count'][$edit->getYear()])) { |
||
| 481 | $data['year_count'][$edit->getYear()] = [ |
||
| 482 | 'all' => 0, |
||
| 483 | 'minor' => 0, |
||
| 484 | 'anon' => 0, |
||
| 485 | 'automated' => 0, |
||
| 486 | 'size' => 0, // keep track of the size by the end of the year |
||
| 487 | 'events' => [], |
||
| 488 | 'months' => [], |
||
| 489 | ]; |
||
| 490 | |||
| 491 | for ($i = 1; $i <= 12; $i++) { |
||
| 492 | $timeObj = mktime(0, 0, 0, $i, 1, $edit->getYear()); |
||
| 493 | |||
| 494 | // don't show zeros for months before the first edit or after the current month |
||
| 495 | if ($timeObj < $firstEditMonth || $timeObj > strtotime('last day of this month')) { |
||
| 496 | continue; |
||
| 497 | } |
||
| 498 | |||
| 499 | $data['year_count'][$edit->getYear()]['months'][sprintf('%02d', $i)] = [ |
||
| 500 | 'all' => 0, |
||
| 501 | 'minor' => 0, |
||
| 502 | 'anon' => 0, |
||
| 503 | 'automated' => 0, |
||
| 504 | ]; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | // Increment year and month counts for all edits |
||
| 509 | $data['year_count'][$edit->getYear()]['all']++; |
||
| 510 | $data['year_count'][$edit->getYear()]['months'][$edit->getMonth()]['all']++; |
||
| 511 | $data['year_count'][$edit->getYear()]['size'] = (int) $rev['length']; |
||
| 512 | |||
| 513 | $editsThisMonth = $data['year_count'][$edit->getYear()]['months'][$edit->getMonth()]['all']; |
||
| 514 | if ($editsThisMonth > $data['max_edits_per_month']) { |
||
| 515 | $data['max_edits_per_month'] = $editsThisMonth; |
||
| 516 | } |
||
| 517 | |||
| 518 | // Fill in various user stats |
||
| 519 | if (!isset($data['editors'][$username])) { |
||
| 520 | $data['general']['editor_count']++; |
||
| 521 | $data['editors'][$username] = [ |
||
| 522 | 'all' => 0, |
||
| 523 | 'minor' => 0, |
||
| 524 | 'minor_percentage' => 0, |
||
| 525 | 'first' => date('Y-m-d, H:i', strtotime($rev['timestamp'])), |
||
| 526 | 'first_id' => $rev['id'], |
||
| 527 | 'last' => null, |
||
| 528 | 'atbe' => null, |
||
| 529 | 'added' => 0, |
||
| 530 | 'sizes' => [], |
||
| 531 | 'urlencoded' => rawurlencode($rev['username']), |
||
| 532 | ]; |
||
| 533 | } |
||
| 534 | |||
| 535 | // Increment user counts |
||
| 536 | $data['editors'][$username]['all']++; |
||
| 537 | $data['editors'][$username]['last'] = date('Y-m-d, H:i', strtotime($rev['timestamp'])); |
||
| 538 | $data['editors'][$username]['last_id'] = $rev['id']; |
||
| 539 | |||
| 540 | // Store number of KB added with this edit |
||
| 541 | $data['editors'][$username]['sizes'][] = $rev['length'] / 1024; |
||
| 542 | |||
| 543 | // check if it was a revert |
||
| 544 | if ($this->aeh->isRevert($rev['comment'])) { |
||
| 545 | $data['general']['revert_count']++; |
||
| 546 | } else { |
||
| 547 | // edit was NOT a revert |
||
| 548 | |||
| 549 | if ($edit->getSize() > 0) { |
||
| 550 | $data['general']['added'] += $edit->getSize(); |
||
| 551 | $data['editors'][$username]['added'] += $edit->getSize(); |
||
| 552 | } |
||
| 553 | |||
| 554 | // determine if the next revision was a revert |
||
| 555 | $nextRevision = isset($this->pageHistory[$i + 1]) ? $this->pageHistory[$i + 1] : null; |
||
| 556 | $nextRevisionIsRevert = $nextRevision && |
||
| 557 | $this->getDiffSize($i + 1) === -$edit->getSize() && |
||
| 558 | $this->aeh->isRevert($nextRevision['comment']); |
||
| 559 | |||
| 560 | // don't count this edit as content removal if the next edit reverted it |
||
| 561 | if (!$nextRevisionIsRevert && $edit->getSize() < $data['general']['max_del']->getSize()) { |
||
| 562 | $data['general']['max_del'] = $edit; |
||
| 563 | } |
||
| 564 | |||
| 565 | // FIXME: possibly remove this |
||
| 566 | if ($edit->getLength() > 0) { |
||
| 567 | // keep track of added content |
||
| 568 | $data['general']['textshare_total'] += $edit->getLength(); |
||
| 569 | if (!isset($data['textshares'][$username]['all'])) { |
||
| 570 | $data['textshares'][$username]['all'] = 0; |
||
| 571 | } |
||
| 572 | $data['textshares'][$username]['all'] += $edit->getLength(); |
||
| 573 | } |
||
| 574 | |||
| 575 | if ($edit->getSize() > $data['general']['max_add']->getSize()) { |
||
| 576 | $data['general']['max_add'] = $edit; |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | if ($edit->isAnon()) { |
||
| 581 | if (!isset($rev['rev_user']['anons'][$username])) { |
||
| 582 | $data['general']['anon_count']++; |
||
| 583 | } |
||
| 584 | // Anonymous, increase counts |
||
| 585 | $data['anons'][] = $username; |
||
| 586 | $data['year_count'][$edit->getYear()]['anon']++; |
||
| 587 | $data['year_count'][$edit->getYear()]['months'][$edit->getMonth()]['anon']++; |
||
| 588 | } |
||
| 589 | |||
| 590 | if ($edit->isMinor()) { |
||
| 591 | // Logged in, increase counts |
||
| 592 | $data['general']['minor_count']++; |
||
| 593 | $data['year_count'][$edit->getYear()]['minor']++; |
||
| 594 | $data['year_count'][$edit->getYear()]['months'][$edit->getMonth()]['minor']++; |
||
| 595 | $data['editors'][$username]['minor']++; |
||
| 596 | } |
||
| 597 | |||
| 598 | $automatedTool = $this->aeh->getTool($rev['comment']); |
||
| 599 | if ($automatedTool) { |
||
| 600 | $data['general']['automated_count']++; |
||
| 601 | $data['year_count'][$edit->getYear()]['automated']++; |
||
| 602 | $data['year_count'][$edit->getYear()]['months'][$edit->getMonth()]['automated']++; |
||
| 603 | |||
| 604 | if (!isset($data['tools'][$automatedTool])) { |
||
| 605 | $data['tools'][$automatedTool] = [ |
||
| 606 | 'count' => 1, |
||
| 607 | 'link' => $this->aeh->getTools()[$automatedTool]['link'], |
||
| 608 | ]; |
||
| 609 | } else { |
||
| 610 | $data['tools'][$automatedTool]['count']++; |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | // Increment "edits per <time>" counts |
||
| 615 | View Code Duplication | if (strtotime($rev['timestamp']) > strtotime('-1 day')) { |
|
| 616 | $data['general']['count_history']['day']++; |
||
| 617 | } |
||
| 618 | View Code Duplication | if (strtotime($rev['timestamp']) > strtotime('-1 week')) { |
|
| 619 | $data['general']['count_history']['week']++; |
||
| 620 | } |
||
| 621 | View Code Duplication | if (strtotime($rev['timestamp']) > strtotime('-1 month')) { |
|
| 622 | $data['general']['count_history']['month']++; |
||
| 623 | } |
||
| 624 | View Code Duplication | if (strtotime($rev['timestamp']) > strtotime('-1 year')) { |
|
| 625 | $data['general']['count_history']['year']++; |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | // add percentages |
||
| 630 | $data['general']['minor_percentage'] = round( |
||
| 631 | ($data['general']['minor_count'] / $revisionCount) * 100, |
||
| 632 | 1 |
||
| 633 | ); |
||
| 634 | $data['general']['anon_percentage'] = round( |
||
| 635 | ($data['general']['anon_count'] / $revisionCount) * 100, |
||
| 636 | 1 |
||
| 637 | ); |
||
| 638 | |||
| 639 | // other general statistics |
||
| 640 | $dateFirst = $firstEdit->getTimestamp(); |
||
| 641 | $dateLast = $lastEdit->getTimestamp(); |
||
| 642 | $data['general']['datetime_first_edit'] = $dateFirst; |
||
| 643 | $data['general']['datetime_last_edit'] = $dateLast; |
||
| 644 | $interval = date_diff($dateLast, $dateFirst, true); |
||
| 645 | |||
| 646 | $data['totaldays'] = $interval->format('%a'); |
||
| 647 | $data['general']['average_days_per_edit'] = round($data['totaldays'] / $revisionCount, 1); |
||
| 648 | $editsPerDay = $data['totaldays'] |
||
| 649 | ? $revisionCount / ($data['totaldays'] / (365 / 12 / 24)) |
||
| 650 | : 0; |
||
| 651 | $data['general']['edits_per_day'] = round($editsPerDay, 1); |
||
| 652 | $editsPerMonth = $data['totaldays'] |
||
| 653 | ? $revisionCount / ($data['totaldays'] / (365 / 12)) |
||
| 654 | : 0; |
||
| 655 | $data['general']['edits_per_month'] = round($editsPerMonth, 1); |
||
| 656 | $editsPerYear = $data['totaldays'] |
||
| 657 | ? $revisionCount / ($data['totaldays'] / 365) |
||
| 658 | : 0; |
||
| 659 | $data['general']['edits_per_year'] = round($editsPerYear, 1); |
||
| 660 | $data['general']['edits_per_editor'] = round($revisionCount / count($data['editors']), 1); |
||
| 661 | |||
| 662 | // If after processing max_del is positive, no edit actually removed text, so unset this value |
||
| 663 | if ($data['general']['max_del']->getSize() > 0) { |
||
| 664 | unset($data['general']['max_del']); |
||
| 665 | } |
||
| 666 | |||
| 667 | // Various sorts |
||
| 668 | arsort($data['editors']); |
||
| 669 | arsort($data['textshares']); |
||
| 670 | arsort($data['tools']); |
||
| 671 | ksort($data['year_count']); |
||
| 672 | |||
| 673 | return $data; |
||
| 674 | } |
||
| 675 | } |
||
| 676 |
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: