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 | * @TODO |
||
| 285 | */ |
||
| 286 | public function countAutomatedRevisions() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 293 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 294 | * @return int The total number of live edits. |
||
| 295 | */ |
||
| 296 | public function countRevisionsInLast($time) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the date and time of the user's first edit. |
||
| 304 | */ |
||
| 305 | public function datetimeFirstRevision() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the date and time of the user's first edit. |
||
| 313 | * @return DateTime |
||
| 314 | */ |
||
| 315 | public function datetimeLastRevision() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the number of days between the first and last edits. |
||
| 323 | * If there's only one edit, this is counted as one day. |
||
| 324 | * @return int |
||
| 325 | */ |
||
| 326 | public function getDays() |
||
| 331 | |||
| 332 | public function countFilesUploaded() |
||
| 337 | |||
| 338 | public function countFilesUploadedCommons() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the total number of revisions the user has sent thanks for. |
||
| 346 | * @return int |
||
| 347 | */ |
||
| 348 | public function thanks() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get the total number of approvals |
||
| 356 | * @return int |
||
| 357 | */ |
||
| 358 | public function approvals() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | public function patrols() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the total edit counts for the top n projects of this user. |
||
| 379 | * @param User $user |
||
| 380 | * @param Project $project |
||
| 381 | * @param int $numProjects |
||
| 382 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
| 383 | */ |
||
| 384 | public function topProjectsEditCounts(User $user, Project $project, $numProjects = 10) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the given user's total edit counts per namespace. |
||
| 398 | */ |
||
| 399 | public function namespaceTotals() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
| 408 | */ |
||
| 409 | public function timeCard() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * |
||
| 416 | */ |
||
| 417 | public function yearCounts() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * |
||
| 439 | */ |
||
| 440 | public function monthCounts() |
||
| 473 | |||
| 474 | public function latestGlobalRevisions($max = 40) |
||
| 507 | } |
||
| 508 |
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: