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 | * 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) |
||
| 46 | { |
||
| 47 | $this->project = $project; |
||
| 48 | $this->user = $user; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get revision and page counts etc. |
||
| 53 | * @return int[] |
||
| 54 | */ |
||
| 55 | protected function getPairData() |
||
| 56 | { |
||
| 57 | if (! is_array($this->pairData)) { |
||
| 58 | $this->pairData = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 59 | ->getPairData($this->project, $this->user); |
||
| 60 | } |
||
| 61 | return $this->pairData; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get revision dates. |
||
| 66 | * @return int[] |
||
| 67 | */ |
||
| 68 | protected function getLogCounts() |
||
| 69 | { |
||
| 70 | if (! is_array($this->logCounts)) { |
||
| 71 | $this->logCounts = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 72 | ->getLogCounts($this->project, $this->user); |
||
| 73 | } |
||
| 74 | return $this->logCounts; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get block data. |
||
| 79 | * @param string $type Either 'set' or 'received'. |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | protected function getBlocks($type) |
||
| 83 | { |
||
| 84 | if (isset($this->blocks[$type]) && is_array($this->blocks[$type])) { |
||
| 85 | return $this->blocks[$type]; |
||
| 86 | } |
||
| 87 | $method = "getBlocks".ucfirst($type); |
||
| 88 | $blocks = $this->getRepository()->$method($this->project, $this->user); |
||
| 89 | $this->blocks[$type] = $blocks; |
||
| 90 | return $this->blocks[$type]; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the total number of currently-live revisions. |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | public function countLiveRevisions() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the total number of the user's revisions that have been deleted. |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | public function countDeletedRevisions() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get the total edit count (live + deleted). |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function countAllRevisions() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the total number of revisions with comments. |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function countRevisionsWithComments() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the total number of revisions without comments. |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | public function countRevisionsWithoutComments() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 143 | * @return int |
||
| 144 | */ |
||
| 145 | public function countMinorRevisions() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the total number of revisions under 20 bytes. |
||
| 153 | */ |
||
| 154 | public function countSmallRevisions() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the total number of revisions over 1000 bytes. |
||
| 162 | */ |
||
| 163 | public function countLargeRevisions() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get the average revision size for the user. |
||
| 171 | * @return float Size in bytes. |
||
| 172 | */ |
||
| 173 | public function averageRevisionSize() |
||
| 174 | { |
||
| 175 | $revisionCounts = $this->getPairData(); |
||
| 176 | if (!isset($revisionCounts['average_size'])) { |
||
| 177 | return 0; |
||
| 178 | } |
||
| 179 | return round($revisionCounts['average_size'], 3); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the total number of non-deleted pages edited by the user. |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | public function countLivePagesEdited() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the total number of deleted pages ever edited by the user. |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | public function countDeletedPagesEdited() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | public function countAllPagesEdited() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the total number of pages (both still live and those that have been deleted) created |
||
| 213 | * by the user. |
||
| 214 | * @return int |
||
| 215 | */ |
||
| 216 | public function countPagesCreated() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the total number of pages created by the user, that have not been deleted. |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function countCreatedPagesLive() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get the total number of pages created by the user, that have since been deleted. |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | public function countPagesCreatedDeleted() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the total number of pages that have been deleted by the user. |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | public function countPagesDeleted() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the total number of pages moved by the user. |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | public function countPagesMoved() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the total number of times the user has blocked or re-blocked a user. |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | public function countBlocksSet() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the total number of blocks that have been lifted (i.e. unblocks) by this user. |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | public function countBlocksLifted() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the total number of times the user has been blocked. |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | public function countBlocksReceived() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get the total number of users blocked by this user. |
||
| 295 | * @return int |
||
| 296 | */ |
||
| 297 | public function countUsersBlocked() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get the total number of users that this user has unblocked. |
||
| 309 | * @return int |
||
| 310 | */ |
||
| 311 | public function countUsersUnblocked() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the total number of pages protected by the user. |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | public function countPagesProtected() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the total number of pages unprotected by the user. |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | public function countPagesUnprotected() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the total number of edits deleted by the user. |
||
| 339 | * @return int |
||
| 340 | */ |
||
| 341 | public function countEditsDeleted() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the total number of pages restored by the user. |
||
| 349 | * @return int |
||
| 350 | */ |
||
| 351 | public function countPagesRestored() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get the total number of pages imported by the user (through any import mechanism: |
||
| 359 | * interwiki, or XML upload). |
||
| 360 | * @return int |
||
| 361 | */ |
||
| 362 | public function countPagesImported() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 373 | * @return float |
||
| 374 | */ |
||
| 375 | public function averageRevisionsPerPage() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Average number of edits made per day. |
||
| 385 | * @return float |
||
| 386 | */ |
||
| 387 | public function averageRevisionsPerDay() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 397 | */ |
||
| 398 | public function countAutomatedRevisions() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get a summary of the numbers of edits made by the user with semi-automating tools. |
||
| 410 | */ |
||
| 411 | public function automatedRevisionsSummary() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 418 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 419 | * @return int The total number of live edits. |
||
| 420 | */ |
||
| 421 | public function countRevisionsInLast($time) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get the date and time of the user's first edit. |
||
| 429 | * @return DateTime|bool The time of the first revision, or false. |
||
| 430 | */ |
||
| 431 | public function datetimeFirstRevision() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get the date and time of the user's first edit. |
||
| 439 | * @return DateTime|bool The time of the last revision, or false. |
||
| 440 | */ |
||
| 441 | public function datetimeLastRevision() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the number of days between the first and last edits. |
||
| 449 | * If there's only one edit, this is counted as one day. |
||
| 450 | * @return int |
||
| 451 | */ |
||
| 452 | public function getDays() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Get the total number of files uploaded (including those now deleted). |
||
| 465 | * @return int |
||
| 466 | */ |
||
| 467 | public function countFilesUploaded() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get the total number of files uploaded to Commons (including those now deleted). |
||
| 475 | * This is only applicable for WMF labs installations. |
||
| 476 | * @return int |
||
| 477 | */ |
||
| 478 | public function countFilesUploadedCommons() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the total number of revisions the user has sent thanks for. |
||
| 486 | * @return int |
||
| 487 | */ |
||
| 488 | public function thanks() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Get the total number of approvals |
||
| 496 | * @return int |
||
| 497 | */ |
||
| 498 | public function approvals() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get the total number of patrols performed by the user. |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | public function patrols() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get the given user's total edit counts per namespace. |
||
| 520 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 521 | */ |
||
| 522 | public function namespaceTotals() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 531 | * @return string[] |
||
| 532 | */ |
||
| 533 | public function timeCard() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get the total numbers of edits per year. |
||
| 540 | * @return int[] |
||
| 541 | */ |
||
| 542 | public function yearCounts() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get the total numbers of edits per month. |
||
| 564 | * @return mixed[] With keys 'years', 'namespaces' and 'totals'. |
||
| 565 | */ |
||
| 566 | public function monthCounts() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Get the total edit counts for the top n projects of this user. |
||
| 602 | * @param int $numProjects |
||
| 603 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 604 | */ |
||
| 605 | public function globalEditCountsTopN($numProjects = 10) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Get the total number of edits excluding the top n. |
||
| 615 | * @param int $numProjects |
||
| 616 | * @return int |
||
| 617 | */ |
||
| 618 | public function globalEditCountWithoutTopN($numProjects = 10) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get the grand total of all edits on all projects. |
||
| 631 | * @return int |
||
| 632 | */ |
||
| 633 | public function globalEditCount() |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get the total revision counts for all projects for this user. |
||
| 644 | * @param bool $sorted Whether to sort the list by total, or not. |
||
| 645 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 646 | */ |
||
| 647 | public function globalEditCounts($sorted = false) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get the most recent n revisions across all projects. |
||
| 664 | * @param int $max The maximum number of revisions to return. |
||
| 665 | * @return Edit[] |
||
| 666 | */ |
||
| 667 | public function globalEdits($max) |
||
| 703 | } |
||
| 704 |
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: