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 |
||
| 10 | class EditCounter extends Model |
||
| 11 | { |
||
| 12 | |||
| 13 | /** @var Project */ |
||
| 14 | protected $project; |
||
| 15 | |||
| 16 | /** @var User */ |
||
| 17 | protected $user; |
||
| 18 | |||
| 19 | /** @var int[] */ |
||
| 20 | protected $revisionCounts; |
||
| 21 | |||
| 22 | /** @var string[] */ |
||
| 23 | protected $revisionDates; |
||
| 24 | |||
| 25 | /** @var int[] */ |
||
| 26 | protected $pageCounts; |
||
| 27 | |||
| 28 | /** @var int[] */ |
||
| 29 | protected $logCounts; |
||
| 30 | |||
| 31 | public function __construct(Project $project, User $user) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get revision count data. |
||
| 39 | * @return int[] |
||
| 40 | */ |
||
| 41 | protected function getRevisionCounts() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get revision dates. |
||
| 52 | * @return int[] |
||
| 53 | */ |
||
| 54 | protected function getRevisionDates() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get page count data. |
||
| 65 | * @return int[] |
||
| 66 | */ |
||
| 67 | protected function getPageCounts() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get revision dates. |
||
| 78 | * @return int[] |
||
| 79 | */ |
||
| 80 | protected function getLogCounts() |
||
| 88 | |||
| 89 | public function countLiveRevisions() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the total number of revisions that have been deleted. |
||
| 97 | * @return int |
||
| 98 | */ |
||
| 99 | public function countDeletedRevisions() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the total edit count (live + deleted). |
||
| 107 | * @return int |
||
| 108 | */ |
||
| 109 | public function countAllRevisions() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the total number of revisions with comments. |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | public function countRevisionsWithComments() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the total number of revisions without comments. |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | public function countRevisionsWithoutComments() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | public function countMinorRevisions() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the total number of revisions under 20 bytes. |
||
| 145 | */ |
||
| 146 | public function countSmallRevisions() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the total number of revisions over 1000 bytes. |
||
| 154 | */ |
||
| 155 | public function countLargeRevisions() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the average revision size for the user. |
||
| 163 | * @return float Size in bytes. |
||
| 164 | */ |
||
| 165 | public function averageRevisionSize() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the total number of non-deleted pages edited by the user. |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | public function countLivePagesEdited() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the total number of deleted pages ever edited by the user. |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function countDeletedPagesEdited() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | public function countAllPagesEdited() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the total number of semi-automated edits. |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | public function countAutomatedEdits() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the total number of pages (both still live and those that have been deleted) created |
||
| 210 | * by the user. |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function countPagesCreated() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the total number of pages created by the user, that have not been deleted. |
||
| 220 | * @return int |
||
| 221 | */ |
||
| 222 | public function countCreatedPagesLive() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the total number of pages created by the user, that have since been deleted. |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | public function countPagesCreatedDeleted() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the total number of pages that have been deleted by the user. |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | public function countPagesDeleted() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the total number of pages moved by the user. |
||
| 250 | * @return int |
||
| 251 | */ |
||
| 252 | public function countPagesMoved() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the average number of edits per page (including deleted revisions and pages). |
||
| 260 | * @return float |
||
| 261 | */ |
||
| 262 | public function averageRevisionsPerPage() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Average number of edits made per day. |
||
| 272 | * @return float |
||
| 273 | */ |
||
| 274 | public function averageRevisionsPerDay() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 284 | */ |
||
| 285 | public function countAutomatedRevisions() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get a summary of the numbers of edits made by the user with semi-automating tools. |
||
| 297 | */ |
||
| 298 | public function automatedRevisionsSummary() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 305 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 306 | * @return int The total number of live edits. |
||
| 307 | */ |
||
| 308 | public function countRevisionsInLast($time) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the date and time of the user's first edit. |
||
| 316 | */ |
||
| 317 | public function datetimeFirstRevision() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the date and time of the user's first edit. |
||
| 325 | * @return DateTime |
||
| 326 | */ |
||
| 327 | public function datetimeLastRevision() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get the number of days between the first and last edits. |
||
| 335 | * If there's only one edit, this is counted as one day. |
||
| 336 | * @return int |
||
| 337 | */ |
||
| 338 | public function getDays() |
||
| 343 | |||
| 344 | public function countFilesUploaded() |
||
| 349 | |||
| 350 | public function countFilesUploadedCommons() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the total number of revisions the user has sent thanks for. |
||
| 358 | * @return int |
||
| 359 | */ |
||
| 360 | public function thanks() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the total number of approvals |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | public function approvals() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return int |
||
| 382 | */ |
||
| 383 | public function patrols() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the total edit counts for the top n projects of this user. |
||
| 391 | * @param User $user |
||
| 392 | * @param Project $project |
||
| 393 | * @param int $numProjects |
||
| 394 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 395 | */ |
||
| 396 | public function topProjectsEditCounts(User $user, Project $project, $numProjects = 10) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get the given user's total edit counts per namespace. |
||
| 410 | */ |
||
| 411 | public function namespaceTotals() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 420 | */ |
||
| 421 | public function timeCard() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * |
||
| 428 | */ |
||
| 429 | public function yearCounts() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * |
||
| 451 | */ |
||
| 452 | public function monthCounts() |
||
| 485 | |||
| 486 | public function latestGlobalRevisions($max = 40) |
||
| 519 | } |
||
| 520 |
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: