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[] The total revision counts. */ |
||
| 23 | protected $revisionCounts; |
||
| 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 | * EditCounter constructor. |
||
| 42 | * @param Project $project The base project to count edits |
||
| 43 | * @param User $user |
||
| 44 | */ |
||
| 45 | public function __construct(Project $project, User $user) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get revision count data. |
||
| 53 | * @return int[] |
||
| 54 | */ |
||
| 55 | protected function getRevisionCounts() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get revision dates. |
||
| 66 | * @return int[] |
||
| 67 | */ |
||
| 68 | protected function getRevisionDates() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get page count data. |
||
| 79 | * @return int[] |
||
| 80 | */ |
||
| 81 | protected function getPageCounts() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get revision dates. |
||
| 92 | * @return int[] |
||
| 93 | */ |
||
| 94 | protected function getLogCounts() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get block data. |
||
| 105 | * @param string $type Either 'set' or 'received'. |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | protected function getBlocks($type) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the total number of currently-live revisions. |
||
| 121 | * @return int |
||
| 122 | */ |
||
| 123 | public function countLiveRevisions() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the total number of the user's revisions that have been deleted. |
||
| 131 | * @return int |
||
| 132 | */ |
||
| 133 | public function countDeletedRevisions() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the total edit count (live + deleted). |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function countAllRevisions() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the total number of revisions with comments. |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function countRevisionsWithComments() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the total number of revisions without comments. |
||
| 160 | * @return int |
||
| 161 | */ |
||
| 162 | public function countRevisionsWithoutComments() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | public function countMinorRevisions() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the total number of revisions under 20 bytes. |
||
| 179 | */ |
||
| 180 | public function countSmallRevisions() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the total number of revisions over 1000 bytes. |
||
| 188 | */ |
||
| 189 | public function countLargeRevisions() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the average revision size for the user. |
||
| 197 | * @return float Size in bytes. |
||
| 198 | */ |
||
| 199 | public function averageRevisionSize() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get the total number of non-deleted pages edited by the user. |
||
| 207 | * @return int |
||
| 208 | */ |
||
| 209 | public function countLivePagesEdited() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the total number of deleted pages ever edited by the user. |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | public function countDeletedPagesEdited() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function countAllPagesEdited() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the total number of pages (both still live and those that have been deleted) created |
||
| 236 | * by the user. |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | public function countPagesCreated() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the total number of pages created by the user, that have not been deleted. |
||
| 246 | * @return int |
||
| 247 | */ |
||
| 248 | public function countCreatedPagesLive() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the total number of pages created by the user, that have since been deleted. |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | public function countPagesCreatedDeleted() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the total number of pages that have been deleted by the user. |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | public function countPagesDeleted() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the total number of pages moved by the user. |
||
| 276 | * @return int |
||
| 277 | */ |
||
| 278 | public function countPagesMoved() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the total number of times the user has blocked or re-blocked a user. |
||
| 286 | * @return int |
||
| 287 | */ |
||
| 288 | public function countBlocksSet() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the total number of blocks that have been lifted (i.e. unblocks) by this user. |
||
| 298 | * @return int |
||
| 299 | */ |
||
| 300 | public function countBlocksLifted() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the total number of times the user has been blocked. |
||
| 308 | * @return int |
||
| 309 | */ |
||
| 310 | public function countBlocksReceived() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the total number of users blocked by this user. |
||
| 318 | * @return int |
||
| 319 | */ |
||
| 320 | public function countUsersBlocked() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the total number of users that this user has unblocked. |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | public function countUsersUnblocked() |
||
| 335 | { |
||
| 336 | $logCounts = $this->getLogCounts(); |
||
| 337 | return isset($logCounts['users-unblocked']) ? (int)$logCounts['users-unblocked'] : 0; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the total number of pages protected by the user. |
||
| 342 | * @return int |
||
| 343 | */ |
||
| 344 | public function countPagesProtected() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the total number of pages unprotected by the user. |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | public function countPagesUnprotected() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the total number of edits deleted by the user. |
||
| 362 | * @return int |
||
| 363 | */ |
||
| 364 | public function countEditsDeleted() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get the total number of pages restored by the user. |
||
| 372 | * @return int |
||
| 373 | */ |
||
| 374 | public function countPagesRestored() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get the total number of pages imported by the user (through any import mechanism: |
||
| 382 | * interwiki, or XML upload). |
||
| 383 | * @return int |
||
| 384 | */ |
||
| 385 | public function countPagesImported() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 396 | * @return float |
||
| 397 | */ |
||
| 398 | public function averageRevisionsPerPage() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Average number of edits made per day. |
||
| 408 | * @return float |
||
| 409 | */ |
||
| 410 | public function averageRevisionsPerDay() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 420 | */ |
||
| 421 | public function countAutomatedRevisions() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get a summary of the numbers of edits made by the user with semi-automating tools. |
||
| 433 | */ |
||
| 434 | public function automatedRevisionsSummary() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 441 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 442 | * @return int The total number of live edits. |
||
| 443 | */ |
||
| 444 | public function countRevisionsInLast($time) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get the date and time of the user's first edit. |
||
| 452 | * @return DateTime|bool The time of the first revision, or false. |
||
| 453 | */ |
||
| 454 | public function datetimeFirstRevision() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the date and time of the user's first edit. |
||
| 462 | * @return DateTime|bool The time of the last revision, or false. |
||
| 463 | */ |
||
| 464 | public function datetimeLastRevision() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get the number of days between the first and last edits. |
||
| 472 | * If there's only one edit, this is counted as one day. |
||
| 473 | * @return int |
||
| 474 | */ |
||
| 475 | public function getDays() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the total number of files uploaded (including those now deleted). |
||
| 488 | * @return int |
||
| 489 | */ |
||
| 490 | public function countFilesUploaded() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the total number of files uploaded to Commons (including those now deleted). |
||
| 498 | * This is only applicable for WMF labs installations. |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | public function countFilesUploadedCommons() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get the total number of revisions the user has sent thanks for. |
||
| 509 | * @return int |
||
| 510 | */ |
||
| 511 | public function thanks() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Get the total number of approvals |
||
| 519 | * @return int |
||
| 520 | */ |
||
| 521 | public function approvals() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get the total number of patrols performed by the user. |
||
| 533 | * @return int |
||
| 534 | */ |
||
| 535 | public function patrols() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Get the given user's total edit counts per namespace. |
||
| 543 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 544 | */ |
||
| 545 | public function namespaceTotals() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 554 | * @return string[] |
||
| 555 | */ |
||
| 556 | public function timeCard() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get the total numbers of edits per year. |
||
| 563 | * @return int[] |
||
| 564 | */ |
||
| 565 | public function yearCounts() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Get the total numbers of edits per month. |
||
| 587 | * @return mixed[] With keys 'years', 'namespaces' and 'totals'. |
||
| 588 | */ |
||
| 589 | public function monthCounts() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the total edit counts for the top n projects of this user. |
||
| 625 | * @param int $numProjects |
||
| 626 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 627 | */ |
||
| 628 | public function globalEditCountsTopN($numProjects = 10) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the total number of edits excluding the top n. |
||
| 638 | * @param int $numProjects |
||
| 639 | * @return int |
||
| 640 | */ |
||
| 641 | public function globalEditCountWithoutTopN($numProjects = 10) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get the grand total of all edits on all projects. |
||
| 654 | * @return int |
||
| 655 | */ |
||
| 656 | public function globalEditCount() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Get the total revision counts for all projects for this user. |
||
| 667 | * @param bool $sorted Whether to sort the list by total, or not. |
||
| 668 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 669 | */ |
||
| 670 | public function globalEditCounts($sorted = false) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get the most recent n revisions across all projects. |
||
| 687 | * @param int $max The maximum number of revisions to return. |
||
| 688 | * @return Edit[] |
||
| 689 | */ |
||
| 690 | public function globalEdits($max) |
||
| 726 | } |
||
| 727 |
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: