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 |
||
| 16 | class EditCounter extends Model |
||
| 17 | { |
||
| 18 | |||
| 19 | /** @var Project The project. */ |
||
| 20 | protected $project; |
||
| 21 | |||
| 22 | /** @var User The user. */ |
||
| 23 | protected $user; |
||
| 24 | |||
| 25 | /** @var int[] Revision and page counts etc. */ |
||
| 26 | protected $pairData; |
||
| 27 | |||
| 28 | /** @var string[] The start and end dates of revisions. */ |
||
| 29 | protected $revisionDates; |
||
| 30 | |||
| 31 | /** @var int[] The total page counts. */ |
||
| 32 | protected $pageCounts; |
||
| 33 | |||
| 34 | /** @var int[] The lot totals. */ |
||
| 35 | protected $logCounts; |
||
| 36 | |||
| 37 | /** @var mixed[] Total numbers of edits per month */ |
||
| 38 | protected $monthCounts; |
||
| 39 | |||
| 40 | /** @var mixed[] Total numbers of edits per year */ |
||
| 41 | protected $yearCounts; |
||
| 42 | |||
| 43 | /** @var int[] Keys are project DB names. */ |
||
| 44 | protected $globalEditCounts; |
||
| 45 | |||
| 46 | /** @var array Block data, with keys 'set' and 'received'. */ |
||
| 47 | protected $blocks; |
||
| 48 | |||
| 49 | /** @var int Number of semi-automated edits */ |
||
| 50 | protected $autoEditCount; |
||
| 51 | |||
| 52 | /** @var string[] Data needed for time card chart */ |
||
| 53 | protected $timeCardData; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Revision size data, with keys 'average_size', 'large_edits' and 'small_edits'. |
||
| 57 | * @var string[] As returned by the DB, unconverted to int or float |
||
| 58 | */ |
||
| 59 | protected $editSizeData; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Duration of the longest block in days; -1 if indefinite, |
||
| 63 | * or false if could not be parsed from log params |
||
| 64 | * @var int|bool |
||
| 65 | */ |
||
| 66 | protected $longestBlockDays; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * EditCounter constructor. |
||
| 70 | * @param Project $project The base project to count edits |
||
| 71 | * @param User $user |
||
| 72 | */ |
||
| 73 | public function __construct(Project $project, User $user) |
||
| 74 | { |
||
| 75 | $this->project = $project; |
||
| 76 | $this->user = $user; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get revision and page counts etc. |
||
| 81 | * @return int[] |
||
| 82 | */ |
||
| 83 | protected function getPairData() |
||
| 84 | { |
||
| 85 | if (! is_array($this->pairData)) { |
||
| 86 | $this->pairData = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 87 | ->getPairData($this->project, $this->user); |
||
| 88 | } |
||
| 89 | return $this->pairData; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get revision dates. |
||
| 94 | * @return int[] |
||
| 95 | */ |
||
| 96 | protected function getLogCounts() |
||
| 97 | { |
||
| 98 | if (! is_array($this->logCounts)) { |
||
| 99 | $this->logCounts = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 100 | ->getLogCounts($this->project, $this->user); |
||
| 101 | } |
||
| 102 | return $this->logCounts; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get block data. |
||
| 107 | * @param string $type Either 'set' or 'received'. |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | protected function getBlocks($type) |
||
| 111 | { |
||
| 112 | if (isset($this->blocks[$type]) && is_array($this->blocks[$type])) { |
||
| 113 | return $this->blocks[$type]; |
||
| 114 | } |
||
| 115 | $method = "getBlocks".ucfirst($type); |
||
| 116 | $blocks = $this->getRepository()->$method($this->project, $this->user); |
||
| 117 | $this->blocks[$type] = $blocks; |
||
| 118 | return $this->blocks[$type]; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the total number of currently-live revisions. |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public function countLiveRevisions() |
||
| 126 | { |
||
| 127 | $revCounts = $this->getPairData(); |
||
| 128 | return isset($revCounts['live']) ? (int)$revCounts['live'] : 0; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the total number of the user's revisions that have been deleted. |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | public function countDeletedRevisions() |
||
| 136 | { |
||
| 137 | $revCounts = $this->getPairData(); |
||
| 138 | return isset($revCounts['deleted']) ? (int)$revCounts['deleted'] : 0; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the total edit count (live + deleted). |
||
| 143 | * @return int |
||
| 144 | */ |
||
| 145 | public function countAllRevisions() |
||
| 146 | { |
||
| 147 | return $this->countLiveRevisions() + $this->countDeletedRevisions(); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the total number of revisions with comments. |
||
| 152 | * @return int |
||
| 153 | */ |
||
| 154 | public function countRevisionsWithComments() |
||
| 155 | { |
||
| 156 | $revCounts = $this->getPairData(); |
||
| 157 | return isset($revCounts['with_comments']) ? (int)$revCounts['with_comments'] : 0; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the total number of revisions without comments. |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function countRevisionsWithoutComments() |
||
| 165 | { |
||
| 166 | return $this->countAllRevisions() - $this->countRevisionsWithComments(); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | public function countMinorRevisions() |
||
| 174 | { |
||
| 175 | $revCounts = $this->getPairData(); |
||
| 176 | return isset($revCounts['minor']) ? (int)$revCounts['minor'] : 0; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the total number of non-deleted pages edited by the user. |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | public function countLivePagesEdited() |
||
| 184 | { |
||
| 185 | $pageCounts = $this->getPairData(); |
||
| 186 | return isset($pageCounts['edited-live']) ? (int)$pageCounts['edited-live'] : 0; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the total number of deleted pages ever edited by the user. |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function countDeletedPagesEdited() |
||
| 194 | { |
||
| 195 | $pageCounts = $this->getPairData(); |
||
| 196 | return isset($pageCounts['edited-deleted']) ? (int)$pageCounts['edited-deleted'] : 0; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 201 | * @return int |
||
| 202 | */ |
||
| 203 | public function countAllPagesEdited() |
||
| 204 | { |
||
| 205 | return $this->countLivePagesEdited() + $this->countDeletedPagesEdited(); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the total number of pages (both still live and those that have been deleted) created |
||
| 210 | * by the user. |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function countPagesCreated() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the total number of pages created by the user, that have not been deleted. |
||
| 220 | * @return int |
||
| 221 | */ |
||
| 222 | public function countCreatedPagesLive() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the total number of pages created by the user, that have since been deleted. |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | public function countPagesCreatedDeleted() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the total number of pages that have been deleted by the user. |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | public function countPagesDeleted() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the total number of pages moved by the user. |
||
| 250 | * @return int |
||
| 251 | */ |
||
| 252 | public function countPagesMoved() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the total number of times the user has blocked a user. |
||
| 260 | * @return int |
||
| 261 | */ |
||
| 262 | public function countBlocksSet() |
||
| 263 | { |
||
| 264 | $logCounts = $this->getLogCounts(); |
||
| 265 | $reBlock = isset($logCounts['block-block']) ? (int)$logCounts['block-block'] : 0; |
||
| 266 | return $reBlock; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the total number of times the user has re-blocked a user. |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | public function countReblocksSet() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the total number of times the user has unblocked a user. |
||
| 282 | * @return int |
||
| 283 | */ |
||
| 284 | public function countUnblocksSet() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the total number of blocks that have been lifted (i.e. unblocks) by this user. |
||
| 292 | * @return int |
||
| 293 | */ |
||
| 294 | public function countBlocksLifted() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the total number of times the user has been blocked. |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | public function countBlocksReceived() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get the length of the longest block the user received. |
||
| 312 | * @return int|bool Number of days or false if it could not be determined. |
||
| 313 | * If the longest duration is indefinite, -1 is returned. |
||
| 314 | */ |
||
| 315 | public function getLongestBlockDays() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the total number of pages protected by the user. |
||
| 358 | * @return int |
||
| 359 | */ |
||
| 360 | public function countPagesProtected() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the total number of pages reprotected by the user. |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | public function countPagesReprotected() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the total number of pages unprotected by the user. |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | public function countPagesUnprotected() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the total number of edits deleted by the user. |
||
| 388 | * @return int |
||
| 389 | */ |
||
| 390 | public function countEditsDeleted() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the total number of pages restored by the user. |
||
| 398 | * @return int |
||
| 399 | */ |
||
| 400 | public function countPagesRestored() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the total number of times the user has modified the rights of a user. |
||
| 408 | * @return int |
||
| 409 | */ |
||
| 410 | public function countRightsModified() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the total number of pages imported by the user (through any import mechanism: |
||
| 418 | * interwiki, or XML upload). |
||
| 419 | * @return int |
||
| 420 | */ |
||
| 421 | public function countPagesImported() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 432 | * @return float |
||
| 433 | */ |
||
| 434 | public function averageRevisionsPerPage() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Average number of edits made per day. |
||
| 444 | * @return float |
||
| 445 | */ |
||
| 446 | public function averageRevisionsPerDay() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 456 | */ |
||
| 457 | public function countAutomatedEdits() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 468 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 469 | * @return int The total number of live edits. |
||
| 470 | */ |
||
| 471 | public function countRevisionsInLast($time) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get the date and time of the user's first edit. |
||
| 479 | * @return DateTime|bool The time of the first revision, or false. |
||
| 480 | */ |
||
| 481 | public function datetimeFirstRevision() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get the date and time of the user's first edit. |
||
| 489 | * @return DateTime|bool The time of the last revision, or false. |
||
| 490 | */ |
||
| 491 | public function datetimeLastRevision() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get the number of days between the first and last edits. |
||
| 499 | * If there's only one edit, this is counted as one day. |
||
| 500 | * @return int |
||
| 501 | */ |
||
| 502 | public function getDays() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get the total number of files uploaded (including those now deleted). |
||
| 515 | * @return int |
||
| 516 | */ |
||
| 517 | public function countFilesUploaded() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get the total number of files uploaded to Commons (including those now deleted). |
||
| 525 | * This is only applicable for WMF labs installations. |
||
| 526 | * @return int |
||
| 527 | */ |
||
| 528 | public function countFilesUploadedCommons() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get the total number of revisions the user has sent thanks for. |
||
| 536 | * @return int |
||
| 537 | */ |
||
| 538 | public function thanks() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get the total number of approvals |
||
| 546 | * @return int |
||
| 547 | */ |
||
| 548 | public function approvals() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get the total number of patrols performed by the user. |
||
| 560 | * @return int |
||
| 561 | */ |
||
| 562 | public function patrols() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get the total number of accounts created by the user. |
||
| 570 | * @return int |
||
| 571 | */ |
||
| 572 | public function accountsCreated() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get the given user's total edit counts per namespace. |
||
| 582 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 583 | */ |
||
| 584 | public function namespaceTotals() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 593 | * @return string[] |
||
| 594 | */ |
||
| 595 | public function timeCard() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get the total numbers of edits per month. |
||
| 607 | * @param null|DateTime [$currentTime] - *USED ONLY FOR UNIT TESTING* |
||
| 608 | * so we can mock the current DateTime. |
||
| 609 | * @return mixed[] With keys 'yearLabels', 'monthLabels' and 'totals', |
||
| 610 | * the latter keyed by namespace, year and then month. |
||
| 611 | */ |
||
| 612 | public function monthCounts($currentTime = null) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Get the total numbers of edits per year. |
||
| 711 | * @param null|DateTime [$currentTime] - *USED ONLY FOR UNIT TESTING* |
||
| 712 | * so we can mock the current DateTime. |
||
| 713 | * @return mixed[] With keys 'yearLabels' and 'totals', the latter |
||
| 714 | * keyed by namespace then year. |
||
| 715 | */ |
||
| 716 | public function yearCounts($currentTime = null) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get the total edit counts for the top n projects of this user. |
||
| 731 | * @param int $numProjects |
||
| 732 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 733 | */ |
||
| 734 | public function globalEditCountsTopN($numProjects = 10) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Get the total number of edits excluding the top n. |
||
| 744 | * @param int $numProjects |
||
| 745 | * @return int |
||
| 746 | */ |
||
| 747 | public function globalEditCountWithoutTopN($numProjects = 10) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Get the grand total of all edits on all projects. |
||
| 760 | * @return int |
||
| 761 | */ |
||
| 762 | public function globalEditCount() |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Get the total revision counts for all projects for this user. |
||
| 773 | * @param bool $sorted Whether to sort the list by total, or not. |
||
| 774 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 775 | */ |
||
| 776 | public function globalEditCounts($sorted = false) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Get the most recent n revisions across all projects. |
||
| 793 | * @param int $max The maximum number of revisions to return. |
||
| 794 | * @return Edit[] |
||
| 795 | */ |
||
| 796 | public function globalEdits($max) |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Get average edit size, and number of large and small edits. |
||
| 833 | * @return int[] |
||
| 834 | */ |
||
| 835 | protected function getEditSizeData() |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Get the total edit count of this user or 5,000 if they've made more than 5,000 edits. |
||
| 846 | * This is used to ensure percentages of small and large edits are computed properly. |
||
| 847 | * @return int |
||
| 848 | */ |
||
| 849 | public function countLast5000() |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Get the number of edits under 20 bytes of the user's past 5000 edits. |
||
| 856 | * @return int |
||
| 857 | */ |
||
| 858 | public function countSmallEdits() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Get the total number of edits over 1000 bytes of the user's past 5000 edits. |
||
| 866 | * @return int |
||
| 867 | */ |
||
| 868 | public function countLargeEdits() |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Get the average size of the user's past 5000 edits. |
||
| 876 | * @return float Size in bytes. |
||
| 877 | */ |
||
| 878 | public function averageEditSize() |
||
| 887 | } |
||
| 888 |
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 sub-classes 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 parent class: