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 |
||
| 13 | class EditCounter extends Model |
||
| 14 | { |
||
| 15 | |||
| 16 | /** @var Project The project. */ |
||
| 17 | protected $project; |
||
| 18 | |||
| 19 | /** @var User The user. */ |
||
| 20 | protected $user; |
||
| 21 | |||
| 22 | /** @var int[] Revision and page counts etc. */ |
||
| 23 | protected $pairData; |
||
| 24 | |||
| 25 | /** @var string[] The start and end dates of revisions. */ |
||
| 26 | protected $revisionDates; |
||
| 27 | |||
| 28 | /** @var int[] The total page counts. */ |
||
| 29 | protected $pageCounts; |
||
| 30 | |||
| 31 | /** @var int[] The lot totals. */ |
||
| 32 | protected $logCounts; |
||
| 33 | |||
| 34 | /** @var int[] Keys are project DB names. */ |
||
| 35 | protected $globalEditCounts; |
||
| 36 | |||
| 37 | /** @var array Block data, with keys 'set' and 'received'. */ |
||
| 38 | protected $blocks; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Revision size data, with keys 'average_size', 'large_edits' and 'small_edits'. |
||
| 42 | * @var string[] As returned by the DB, unconverted to int or float |
||
| 43 | */ |
||
| 44 | protected $editSizeData; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Duration of the longest block in days; -1 if indefinite, |
||
| 48 | * or false if could not be parsed from log params |
||
| 49 | * @var int|bool |
||
| 50 | */ |
||
| 51 | protected $longestBlockDays; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * EditCounter constructor. |
||
| 55 | * @param Project $project The base project to count edits |
||
| 56 | * @param User $user |
||
| 57 | */ |
||
| 58 | public function __construct(Project $project, User $user) |
||
| 59 | { |
||
| 60 | $this->project = $project; |
||
| 61 | $this->user = $user; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get revision and page counts etc. |
||
| 66 | * @return int[] |
||
| 67 | */ |
||
| 68 | protected function getPairData() |
||
| 69 | { |
||
| 70 | if (! is_array($this->pairData)) { |
||
| 71 | $this->pairData = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 72 | ->getPairData($this->project, $this->user); |
||
| 73 | } |
||
| 74 | return $this->pairData; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get revision dates. |
||
| 79 | * @return int[] |
||
| 80 | */ |
||
| 81 | protected function getLogCounts() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get block data. |
||
| 92 | * @param string $type Either 'set' or 'received'. |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | protected function getBlocks($type) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the total number of currently-live revisions. |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function countLiveRevisions() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the total number of the user's revisions that have been deleted. |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public function countDeletedRevisions() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the total edit count (live + deleted). |
||
| 128 | * @return int |
||
| 129 | */ |
||
| 130 | public function countAllRevisions() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the total number of revisions with comments. |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function countRevisionsWithComments() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the total number of revisions without comments. |
||
| 147 | * @return int |
||
| 148 | */ |
||
| 149 | public function countRevisionsWithoutComments() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | public function countMinorRevisions() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get the total number of non-deleted pages edited by the user. |
||
| 166 | * @return int |
||
| 167 | */ |
||
| 168 | public function countLivePagesEdited() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the total number of deleted pages ever edited by the user. |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function countDeletedPagesEdited() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | public function countAllPagesEdited() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the total number of pages (both still live and those that have been deleted) created |
||
| 195 | * by the user. |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | public function countPagesCreated() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get the total number of pages created by the user, that have not been deleted. |
||
| 205 | * @return int |
||
| 206 | */ |
||
| 207 | public function countCreatedPagesLive() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get the total number of pages created by the user, that have since been deleted. |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | public function countPagesCreatedDeleted() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the total number of pages that have been deleted by the user. |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | public function countPagesDeleted() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get the total number of pages moved by the user. |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function countPagesMoved() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the total number of times the user has blocked a user. |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | public function countBlocksSet() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the total number of times the user has re-blocked a user. |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | public function countReblocksSet() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the total number of times the user has unblocked a user. |
||
| 267 | * @return int |
||
| 268 | */ |
||
| 269 | public function countUnblocksSet() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the total number of blocks that have been lifted (i.e. unblocks) by this user. |
||
| 277 | * @return int |
||
| 278 | */ |
||
| 279 | public function countBlocksLifted() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the total number of times the user has been blocked. |
||
| 287 | * @return int |
||
| 288 | */ |
||
| 289 | public function countBlocksReceived() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the length of the longest block the user received. |
||
| 297 | * @return int|bool Number of days or false if it could not be determined. |
||
| 298 | * If the longest duration is indefinite, -1 is returned. |
||
| 299 | */ |
||
| 300 | public function getLongestBlockDays() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get the total number of pages protected by the user. |
||
| 343 | * @return int |
||
| 344 | */ |
||
| 345 | public function countPagesProtected() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the total number of pages reprotected by the user. |
||
| 353 | * @return int |
||
| 354 | */ |
||
| 355 | public function countPagesReprotected() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the total number of pages unprotected by the user. |
||
| 363 | * @return int |
||
| 364 | */ |
||
| 365 | public function countPagesUnprotected() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the total number of edits deleted by the user. |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | public function countEditsDeleted() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the total number of pages restored by the user. |
||
| 383 | * @return int |
||
| 384 | */ |
||
| 385 | public function countPagesRestored() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get the total number of times the user has modified the rights of a user. |
||
| 393 | * @return int |
||
| 394 | */ |
||
| 395 | public function countRightsModified() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the total number of pages imported by the user (through any import mechanism: |
||
| 403 | * interwiki, or XML upload). |
||
| 404 | * @return int |
||
| 405 | */ |
||
| 406 | public function countPagesImported() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 417 | * @return float |
||
| 418 | */ |
||
| 419 | public function averageRevisionsPerPage() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Average number of edits made per day. |
||
| 429 | * @return float |
||
| 430 | */ |
||
| 431 | public function averageRevisionsPerDay() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 441 | */ |
||
| 442 | public function countAutomatedRevisions() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get a summary of the numbers of edits made by the user with semi-automating tools. |
||
| 454 | */ |
||
| 455 | public function automatedRevisionsSummary() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 462 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 463 | * @return int The total number of live edits. |
||
| 464 | */ |
||
| 465 | public function countRevisionsInLast($time) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the date and time of the user's first edit. |
||
| 473 | * @return DateTime|bool The time of the first revision, or false. |
||
| 474 | */ |
||
| 475 | public function datetimeFirstRevision() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get the date and time of the user's first edit. |
||
| 483 | * @return DateTime|bool The time of the last revision, or false. |
||
| 484 | */ |
||
| 485 | public function datetimeLastRevision() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get the number of days between the first and last edits. |
||
| 493 | * If there's only one edit, this is counted as one day. |
||
| 494 | * @return int |
||
| 495 | */ |
||
| 496 | public function getDays() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get the total number of files uploaded (including those now deleted). |
||
| 509 | * @return int |
||
| 510 | */ |
||
| 511 | public function countFilesUploaded() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Get the total number of files uploaded to Commons (including those now deleted). |
||
| 519 | * This is only applicable for WMF labs installations. |
||
| 520 | * @return int |
||
| 521 | */ |
||
| 522 | public function countFilesUploadedCommons() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get the total number of revisions the user has sent thanks for. |
||
| 530 | * @return int |
||
| 531 | */ |
||
| 532 | public function thanks() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get the total number of approvals |
||
| 540 | * @return int |
||
| 541 | */ |
||
| 542 | public function approvals() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the total number of patrols performed by the user. |
||
| 554 | * @return int |
||
| 555 | */ |
||
| 556 | public function patrols() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get the total number of accounts created by the user. |
||
| 564 | * @return int |
||
| 565 | */ |
||
| 566 | public function accountsCreated() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get the given user's total edit counts per namespace. |
||
| 576 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 577 | */ |
||
| 578 | public function namespaceTotals() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 587 | * @return string[] |
||
| 588 | */ |
||
| 589 | public function timeCard() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Get the total numbers of edits per year. |
||
| 596 | * @return int[] |
||
| 597 | */ |
||
| 598 | public function yearCounts() |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Get the total numbers of edits per month. |
||
| 624 | * @return mixed[] With keys 'years', 'namespaces' and 'totals'. |
||
| 625 | */ |
||
| 626 | public function monthCounts() |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Get the total edit counts for the top n projects of this user. |
||
| 665 | * @param int $numProjects |
||
| 666 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 667 | */ |
||
| 668 | public function globalEditCountsTopN($numProjects = 10) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Get the total number of edits excluding the top n. |
||
| 678 | * @param int $numProjects |
||
| 679 | * @return int |
||
| 680 | */ |
||
| 681 | public function globalEditCountWithoutTopN($numProjects = 10) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Get the grand total of all edits on all projects. |
||
| 694 | * @return int |
||
| 695 | */ |
||
| 696 | public function globalEditCount() |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Get the total revision counts for all projects for this user. |
||
| 707 | * @param bool $sorted Whether to sort the list by total, or not. |
||
| 708 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 709 | */ |
||
| 710 | public function globalEditCounts($sorted = false) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Get the most recent n revisions across all projects. |
||
| 727 | * @param int $max The maximum number of revisions to return. |
||
| 728 | * @return Edit[] |
||
| 729 | */ |
||
| 730 | public function globalEdits($max) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Get average edit size, and number of large and small edits. |
||
| 769 | * @return int[] |
||
| 770 | */ |
||
| 771 | protected function getEditSizeData() |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Get the total edit count of this user or 5,000 if they've made more than 5,000 edits. |
||
| 782 | * This is used to ensure percentages of small and large edits are computed properly. |
||
| 783 | * @return int |
||
| 784 | */ |
||
| 785 | public function countLast5000() |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Get the number of edits under 20 bytes of the user's past 5000 edits. |
||
| 792 | * @return int |
||
| 793 | */ |
||
| 794 | public function countSmallEdits() |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Get the total number of edits over 1000 bytes of the user's past 5000 edits. |
||
| 802 | * @return int |
||
| 803 | */ |
||
| 804 | public function countLargeEdits() |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Get the average size of the user's past 5000 edits. |
||
| 812 | * @return float Size in bytes. |
||
| 813 | */ |
||
| 814 | public function averageEditSize() |
||
| 823 | } |
||
| 824 |
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: