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 | * @todo |
||
| 333 | * @return int |
||
| 334 | */ |
||
| 335 | public function countUsersUnblocked() |
||
| 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. |
||
| 382 | * @return int |
||
| 383 | */ |
||
| 384 | public function countPagesImported() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 392 | * @return float |
||
| 393 | */ |
||
| 394 | public function averageRevisionsPerPage() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Average number of edits made per day. |
||
| 404 | * @return float |
||
| 405 | */ |
||
| 406 | public function averageRevisionsPerDay() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 416 | */ |
||
| 417 | public function countAutomatedRevisions() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get a summary of the numbers of edits made by the user with semi-automating tools. |
||
| 429 | */ |
||
| 430 | public function automatedRevisionsSummary() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 437 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 438 | * @return int The total number of live edits. |
||
| 439 | */ |
||
| 440 | public function countRevisionsInLast($time) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get the date and time of the user's first edit. |
||
| 448 | * @return DateTime|bool The time of the first revision, or false. |
||
| 449 | */ |
||
| 450 | public function datetimeFirstRevision() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get the date and time of the user's first edit. |
||
| 458 | * @return DateTime|bool The time of the last revision, or false. |
||
| 459 | */ |
||
| 460 | public function datetimeLastRevision() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the number of days between the first and last edits. |
||
| 468 | * If there's only one edit, this is counted as one day. |
||
| 469 | * @return int |
||
| 470 | */ |
||
| 471 | public function getDays() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get the total number of files uploaded (including those now deleted). |
||
| 484 | * @return int |
||
| 485 | */ |
||
| 486 | public function countFilesUploaded() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get the total number of files uploaded to Commons (including those now deleted). |
||
| 494 | * This is only applicable for WMF labs installations. |
||
| 495 | * @return int |
||
| 496 | */ |
||
| 497 | public function countFilesUploadedCommons() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the total number of revisions the user has sent thanks for. |
||
| 505 | * @return int |
||
| 506 | */ |
||
| 507 | public function thanks() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get the total number of approvals |
||
| 515 | * @return int |
||
| 516 | */ |
||
| 517 | public function approvals() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Get the total number of patrols performed by the user. |
||
| 529 | * @return int |
||
| 530 | */ |
||
| 531 | public function patrols() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get the given user's total edit counts per namespace. |
||
| 539 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 540 | */ |
||
| 541 | public function namespaceTotals() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 550 | * @return string[] |
||
| 551 | */ |
||
| 552 | public function timeCard() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Get the total numbers of edits per year. |
||
| 559 | * @return int[] |
||
| 560 | */ |
||
| 561 | public function yearCounts() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get the total numbers of edits per month. |
||
| 583 | * @return mixed[] With keys 'years', 'namespaces' and 'totals'. |
||
| 584 | */ |
||
| 585 | public function monthCounts() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Get the total edit counts for the top n projects of this user. |
||
| 621 | * @param int $numProjects |
||
| 622 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 623 | */ |
||
| 624 | public function globalEditCountsTopN($numProjects = 10) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the grand total of all edits on all projects. |
||
| 638 | * @return int |
||
| 639 | */ |
||
| 640 | public function globalEditCount() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Get the total revision counts for all projects for this user. |
||
| 651 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 652 | */ |
||
| 653 | public function globalEditCounts() |
||
| 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) |
||
| 705 | } |
||
| 706 |
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: