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 */ |
||
| 17 | protected $project; |
||
| 18 | |||
| 19 | /** @var User */ |
||
| 20 | protected $user; |
||
| 21 | |||
| 22 | /** @var int[] */ |
||
| 23 | protected $revisionCounts; |
||
| 24 | |||
| 25 | /** @var string[] */ |
||
| 26 | protected $revisionDates; |
||
| 27 | |||
| 28 | /** @var int[] */ |
||
| 29 | protected $pageCounts; |
||
| 30 | |||
| 31 | /** @var int[] */ |
||
| 32 | protected $logCounts; |
||
| 33 | |||
| 34 | /** @var int[] Keys are project DB names. */ |
||
| 35 | protected $globalEditCounts; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * EditCounter constructor. |
||
| 39 | * @param Project $project The base project to count edits |
||
| 40 | * @param User $user |
||
| 41 | */ |
||
| 42 | public function __construct(Project $project, User $user) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get revision count data. |
||
| 50 | * @return int[] |
||
| 51 | */ |
||
| 52 | protected function getRevisionCounts() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get revision dates. |
||
| 63 | * @return int[] |
||
| 64 | */ |
||
| 65 | protected function getRevisionDates() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get page count data. |
||
| 76 | * @return int[] |
||
| 77 | */ |
||
| 78 | protected function getPageCounts() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get revision dates. |
||
| 89 | * @return int[] |
||
| 90 | */ |
||
| 91 | protected function getLogCounts() |
||
| 99 | |||
| 100 | public function countLiveRevisions() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the total number of revisions that have been deleted. |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function countDeletedRevisions() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the total edit count (live + deleted). |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public function countAllRevisions() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the total number of revisions with comments. |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function countRevisionsWithComments() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the total number of revisions without comments. |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function countRevisionsWithoutComments() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | public function countMinorRevisions() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the total number of revisions under 20 bytes. |
||
| 156 | */ |
||
| 157 | public function countSmallRevisions() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the total number of revisions over 1000 bytes. |
||
| 165 | */ |
||
| 166 | public function countLargeRevisions() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the average revision size for the user. |
||
| 174 | * @return float Size in bytes. |
||
| 175 | */ |
||
| 176 | public function averageRevisionSize() |
||
| 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 average number of edits per page (including deleted revisions and pages). |
||
| 263 | * @return float |
||
| 264 | */ |
||
| 265 | public function averageRevisionsPerPage() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Average number of edits made per day. |
||
| 275 | * @return float |
||
| 276 | */ |
||
| 277 | public function averageRevisionsPerDay() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 287 | */ |
||
| 288 | public function countAutomatedRevisions() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get a summary of the numbers of edits made by the user with semi-automating tools. |
||
| 300 | */ |
||
| 301 | public function automatedRevisionsSummary() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 308 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 309 | * @return int The total number of live edits. |
||
| 310 | */ |
||
| 311 | public function countRevisionsInLast($time) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the date and time of the user's first edit. |
||
| 319 | */ |
||
| 320 | public function datetimeFirstRevision() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get the date and time of the user's first edit. |
||
| 328 | * @return DateTime |
||
| 329 | */ |
||
| 330 | public function datetimeLastRevision() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the number of days between the first and last edits. |
||
| 338 | * If there's only one edit, this is counted as one day. |
||
| 339 | * @return int |
||
| 340 | */ |
||
| 341 | public function getDays() |
||
| 346 | |||
| 347 | public function countFilesUploaded() |
||
| 352 | |||
| 353 | public function countFilesUploadedCommons() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the total number of revisions the user has sent thanks for. |
||
| 361 | * @return int |
||
| 362 | */ |
||
| 363 | public function thanks() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the total number of approvals |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | public function approvals() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return int |
||
| 385 | */ |
||
| 386 | public function patrols() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the given user's total edit counts per namespace. |
||
| 394 | */ |
||
| 395 | public function namespaceTotals() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 404 | */ |
||
| 405 | public function timeCard() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * |
||
| 412 | */ |
||
| 413 | public function yearCounts() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * |
||
| 435 | */ |
||
| 436 | public function monthCounts() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get the total edit counts for the top n projects of this user. |
||
| 472 | * @param int $numProjects |
||
| 473 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 474 | */ |
||
| 475 | public function globalEditCountsTopN($numProjects = 10) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get the grand total of all edits on all projects. |
||
| 489 | * @return int |
||
| 490 | */ |
||
| 491 | public function globalEditCount() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get the total revision counts for all projects for this user. |
||
| 502 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 503 | */ |
||
| 504 | public function globalEditCounts() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get the most recent n revisions across all projects. |
||
| 515 | * @param int $max |
||
| 516 | * @return Edit[] |
||
| 517 | */ |
||
| 518 | public function globalEdits($max) { |
||
| 552 | } |
||
| 553 |
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: