Complex classes like EditCounter 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 EditCounter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class EditCounter extends Model |
||
| 19 | { |
||
| 20 | |||
| 21 | /** @var Project The project. */ |
||
| 22 | protected $project; |
||
| 23 | |||
| 24 | /** @var User The user. */ |
||
| 25 | protected $user; |
||
| 26 | |||
| 27 | /** @var int[] Revision and page counts etc. */ |
||
| 28 | protected $pairData; |
||
| 29 | |||
| 30 | /** @var string[] The start and end dates of revisions. */ |
||
| 31 | protected $revisionDates; |
||
| 32 | |||
| 33 | /** @var int[] The total page counts. */ |
||
| 34 | protected $pageCounts; |
||
| 35 | |||
| 36 | /** @var int[] The lot totals. */ |
||
| 37 | protected $logCounts; |
||
| 38 | |||
| 39 | /** @var mixed[] Total numbers of edits per month */ |
||
| 40 | protected $monthCounts; |
||
| 41 | |||
| 42 | /** @var mixed[] Total numbers of edits per year */ |
||
| 43 | protected $yearCounts; |
||
| 44 | |||
| 45 | /** @var int[] Keys are project DB names. */ |
||
| 46 | protected $globalEditCounts; |
||
| 47 | |||
| 48 | /** @var array Block data, with keys 'set' and 'received'. */ |
||
| 49 | protected $blocks; |
||
| 50 | |||
| 51 | /** @var integer[] Array keys are namespace IDs, values are the edit counts */ |
||
| 52 | protected $namespaceTotals; |
||
| 53 | |||
| 54 | /** @var int Number of semi-automated edits */ |
||
| 55 | protected $autoEditCount; |
||
| 56 | |||
| 57 | /** @var string[] Data needed for time card chart */ |
||
| 58 | protected $timeCardData; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Revision size data, with keys 'average_size', 'large_edits' and 'small_edits'. |
||
| 62 | * @var string[] As returned by the DB, unconverted to int or float |
||
| 63 | */ |
||
| 64 | protected $editSizeData; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Duration of the longest block in seconds; -1 if indefinite, |
||
| 68 | * or false if could not be parsed from log params |
||
| 69 | * @var int|bool |
||
| 70 | */ |
||
| 71 | protected $longestBlockSeconds; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * EditCounter constructor. |
||
| 75 | * @param Project $project The base project to count edits |
||
| 76 | * @param User $user |
||
| 77 | */ |
||
| 78 | 19 | public function __construct(Project $project, User $user) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * This method asynchronously fetches all the expensive data, waits |
||
| 86 | * for each request to finish, and copies the values to the class instance. |
||
| 87 | * @return null |
||
| 88 | */ |
||
| 89 | public function prepareData() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get revision and page counts etc. |
||
| 153 | * @return int[] |
||
| 154 | */ |
||
| 155 | 4 | public function getPairData() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get revision dates. |
||
| 166 | * @return int[] |
||
| 167 | */ |
||
| 168 | public function getLogCounts() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get block data. |
||
| 179 | * @param string $type Either 'set', 'received' |
||
| 180 | * @param bool $blocksOnly Whether to include only blocks, and not reblocks and unblocks. |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | 6 | protected function getBlocks($type, $blocksOnly = true) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get the total number of currently-live revisions. |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | 1 | public function countLiveRevisions() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get the total number of the user's revisions that have been deleted. |
||
| 214 | * @return int |
||
| 215 | */ |
||
| 216 | 1 | public function countDeletedRevisions() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get the total edit count (live + deleted). |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | 1 | public function countAllRevisions() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Get the total number of live revisions with comments. |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | 1 | public function countRevisionsWithComments() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get the total number of live revisions without comments. |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | 1 | public function countRevisionsWithoutComments() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function countMinorRevisions() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the total number of non-deleted pages edited by the user. |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | 1 | public function countLivePagesEdited() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get the total number of deleted pages ever edited by the user. |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | 1 | public function countDeletedPagesEdited() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 282 | * @return int |
||
| 283 | */ |
||
| 284 | 1 | public function countAllPagesEdited() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Get the total number of pages (both still live and those that have been deleted) created |
||
| 291 | * by the user. |
||
| 292 | * @return int |
||
| 293 | */ |
||
| 294 | 1 | public function countPagesCreated() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get the total number of pages created by the user, that have not been deleted. |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | 1 | public function countCreatedPagesLive() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Get the total number of pages created by the user, that have since been deleted. |
||
| 311 | * @return int |
||
| 312 | */ |
||
| 313 | 1 | public function countPagesCreatedDeleted() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get the total number of pages that have been deleted by the user. |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | public function countPagesDeleted() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the total number of pages moved by the user. |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | public function countPagesMoved() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get the total number of times the user has blocked a user. |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | public function countBlocksSet() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the total number of times the user has re-blocked a user. |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | public function countReblocksSet() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the total number of times the user has unblocked a user. |
||
| 363 | * @return int |
||
| 364 | */ |
||
| 365 | public function countUnblocksSet() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the total number of blocks that have been lifted (i.e. unblocks) by this user. |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | public function countBlocksLifted() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the total number of times the user has been blocked. |
||
| 383 | * @return int |
||
| 384 | */ |
||
| 385 | public function countBlocksReceived() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get the length of the longest block the user received, in seconds. |
||
| 393 | * @return int Number of seconds or false if it could not be determined. |
||
| 394 | * If the user is blocked, the time since the block is returned. If the block is |
||
| 395 | * indefinite, -1 is returned. 0 if there was never a block. |
||
| 396 | */ |
||
| 397 | 6 | public function getLongestBlockSeconds() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Given a block log entry from the database, get the timestamp and duration in seconds. |
||
| 471 | * @param mixed[] $block Block log entry as fetched via self::getBlocks() |
||
| 472 | * @return int[] [ |
||
| 473 | * Unix timestamp, |
||
| 474 | * Duration in seconds (-1 if indefinite, null if unparsable or unblock) |
||
| 475 | * ] |
||
| 476 | */ |
||
| 477 | 11 | public function parseBlockLogEntry($block) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Get the total number of pages protected by the user. |
||
| 507 | * @return int |
||
| 508 | */ |
||
| 509 | public function countPagesProtected() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get the total number of pages reprotected by the user. |
||
| 517 | * @return int |
||
| 518 | */ |
||
| 519 | public function countPagesReprotected() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get the total number of pages unprotected by the user. |
||
| 527 | * @return int |
||
| 528 | */ |
||
| 529 | public function countPagesUnprotected() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Get the total number of edits deleted by the user. |
||
| 537 | * @return int |
||
| 538 | */ |
||
| 539 | public function countEditsDeleted() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get the total number of pages restored by the user. |
||
| 547 | * @return int |
||
| 548 | */ |
||
| 549 | public function countPagesRestored() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get the total number of times the user has modified the rights of a user. |
||
| 557 | * @return int |
||
| 558 | */ |
||
| 559 | public function countRightsModified() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Get the total number of pages imported by the user (through any import mechanism: |
||
| 567 | * interwiki, or XML upload). |
||
| 568 | * @return int |
||
| 569 | */ |
||
| 570 | public function countPagesImported() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 581 | * @return float |
||
| 582 | */ |
||
| 583 | public function averageRevisionsPerPage() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Average number of edits made per day. |
||
| 593 | * @return float |
||
| 594 | */ |
||
| 595 | public function averageRevisionsPerDay() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 605 | */ |
||
| 606 | public function countAutomatedEdits() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 617 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 618 | * @return int The total number of live edits. |
||
| 619 | */ |
||
| 620 | public function countRevisionsInLast($time) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get the date and time of the user's first edit. |
||
| 628 | * @return DateTime|bool The time of the first revision, or false. |
||
| 629 | */ |
||
| 630 | 2 | public function datetimeFirstRevision() |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Get the date and time of the user's first edit. |
||
| 638 | * @return DateTime|bool The time of the last revision, or false. |
||
| 639 | */ |
||
| 640 | 2 | public function datetimeLastRevision() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get the number of days between the first and last edits. |
||
| 648 | * If there's only one edit, this is counted as one day. |
||
| 649 | * @return int |
||
| 650 | */ |
||
| 651 | 2 | public function getDays() |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Get the total number of files uploaded (including those now deleted). |
||
| 664 | * @return int |
||
| 665 | */ |
||
| 666 | public function countFilesUploaded() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get the total number of files uploaded to Commons (including those now deleted). |
||
| 674 | * This is only applicable for WMF labs installations. |
||
| 675 | * @return int |
||
| 676 | */ |
||
| 677 | public function countFilesUploadedCommons() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Get the total number of revisions the user has sent thanks for. |
||
| 685 | * @return int |
||
| 686 | */ |
||
| 687 | public function thanks() |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Get the total number of approvals |
||
| 695 | * @return int |
||
| 696 | */ |
||
| 697 | public function approvals() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Get the total number of patrols performed by the user. |
||
| 709 | * @return int |
||
| 710 | */ |
||
| 711 | public function patrols() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Get the total number of accounts created by the user. |
||
| 719 | * @return int |
||
| 720 | */ |
||
| 721 | public function accountsCreated() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get the given user's total edit counts per namespace. |
||
| 731 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 732 | */ |
||
| 733 | 1 | public function namespaceTotals() |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 746 | * @return string[] |
||
| 747 | */ |
||
| 748 | public function timeCard() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Get the total numbers of edits per month. |
||
| 760 | * @param null|DateTime $currentTime - *USED ONLY FOR UNIT TESTING* |
||
| 761 | * so we can mock the current DateTime. |
||
| 762 | * @return mixed[] With keys 'yearLabels', 'monthLabels' and 'totals', |
||
| 763 | * the latter keyed by namespace, year and then month. |
||
| 764 | */ |
||
| 765 | 2 | public function monthCounts($currentTime = null) |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Loop through the database results and fill in the values |
||
| 814 | * for the months that we have data for. |
||
| 815 | * @param array $out |
||
| 816 | * @param string[] $totals |
||
| 817 | * @param DateTime $firstEdit |
||
| 818 | * @return array [ |
||
| 819 | * string[] - Modified $out filled with month stats, |
||
| 820 | * DateTime - timestamp of first edit |
||
| 821 | * ] |
||
| 822 | * Tests covered in self::monthCounts(). |
||
| 823 | * @codeCoverageIgnore |
||
| 824 | */ |
||
| 825 | private function fillInMonthCounts($out, $totals, $firstEdit) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Given the output array, fill each month's totals and labels. |
||
| 853 | * @param array $out |
||
| 854 | * @param DatePeriod $dateRange From first edit to present. |
||
| 855 | * @return string[] - Modified $out filled with month stats. |
||
| 856 | * Tests covered in self::monthCounts(). |
||
| 857 | * @codeCoverageIgnore |
||
| 858 | */ |
||
| 859 | private function fillInMonthTotalsAndLabels($out, DatePeriod $dateRange) |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Get the total numbers of edits per year. |
||
| 887 | * @param null|DateTime [$currentTime] - *USED ONLY FOR UNIT TESTING* |
||
| 888 | * so we can mock the current DateTime. |
||
| 889 | * @return mixed[] With keys 'yearLabels' and 'totals', the latter |
||
| 890 | * keyed by namespace then year. |
||
| 891 | */ |
||
| 892 | 1 | public function yearCounts($currentTime = null) |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Get the total edit counts for the top n projects of this user. |
||
| 912 | * @param int $numProjects |
||
| 913 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 914 | */ |
||
| 915 | 1 | public function globalEditCountsTopN($numProjects = 10) |
|
| 922 | |||
| 923 | /** |
||
| 924 | * Get the total number of edits excluding the top n. |
||
| 925 | * @param int $numProjects |
||
| 926 | * @return int |
||
| 927 | */ |
||
| 928 | 1 | public function globalEditCountWithoutTopN($numProjects = 10) |
|
| 938 | |||
| 939 | /** |
||
| 940 | * Get the grand total of all edits on all projects. |
||
| 941 | * @return int |
||
| 942 | */ |
||
| 943 | 1 | public function globalEditCount() |
|
| 951 | |||
| 952 | /** |
||
| 953 | * Get the total revision counts for all projects for this user. |
||
| 954 | * @param bool $sorted Whether to sort the list by total, or not. |
||
| 955 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 956 | */ |
||
| 957 | 1 | public function globalEditCounts($sorted = false) |
|
| 973 | |||
| 974 | /** |
||
| 975 | * Get the most recent n revisions across all projects. |
||
| 976 | * @param int $max The maximum number of revisions to return. |
||
| 977 | * @return Edit[] |
||
| 978 | */ |
||
| 979 | public function globalEdits($max) |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Get average edit size, and number of large and small edits. |
||
| 1016 | * @return int[] |
||
| 1017 | */ |
||
| 1018 | public function getEditSizeData() |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Get the total edit count of this user or 5,000 if they've made more than 5,000 edits. |
||
| 1029 | * This is used to ensure percentages of small and large edits are computed properly. |
||
| 1030 | * @return int |
||
| 1031 | */ |
||
| 1032 | 1 | public function countLast5000() |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Get the number of edits under 20 bytes of the user's past 5000 edits. |
||
| 1039 | * @return int |
||
| 1040 | */ |
||
| 1041 | public function countSmallEdits() |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Get the total number of edits over 1000 bytes of the user's past 5000 edits. |
||
| 1049 | * @return int |
||
| 1050 | */ |
||
| 1051 | public function countLargeEdits() |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Get the average size of the user's past 5000 edits. |
||
| 1059 | * @return float Size in bytes. |
||
| 1060 | */ |
||
| 1061 | public function averageEditSize() |
||
| 1070 | } |
||
| 1071 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.