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 | /** |
||
| 35 | * EditCounter constructor. |
||
| 36 | * @param Project $project The base project to count edits |
||
| 37 | * @param User $user |
||
| 38 | */ |
||
| 39 | public function __construct(Project $project, User $user) |
||
| 40 | { |
||
| 41 | $this->project = $project; |
||
| 42 | $this->user = $user; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get revision count data. |
||
| 47 | * @return int[] |
||
| 48 | */ |
||
| 49 | protected function getRevisionCounts() |
||
| 50 | { |
||
| 51 | if (! is_array($this->revisionCounts)) { |
||
| 52 | $this->revisionCounts = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 53 | ->getRevisionCounts($this->project, $this->user); |
||
| 54 | } |
||
| 55 | return $this->revisionCounts; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get revision dates. |
||
| 60 | * @return int[] |
||
| 61 | */ |
||
| 62 | protected function getRevisionDates() |
||
| 63 | { |
||
| 64 | if (! is_array($this->revisionDates)) { |
||
| 65 | $this->revisionDates = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 66 | ->getRevisionDates($this->project, $this->user); |
||
| 67 | } |
||
| 68 | return $this->revisionDates; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get page count data. |
||
| 73 | * @return int[] |
||
| 74 | */ |
||
| 75 | protected function getPageCounts() |
||
| 76 | { |
||
| 77 | if (! is_array($this->pageCounts)) { |
||
| 78 | $this->pageCounts = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 79 | ->getPageCounts($this->project, $this->user); |
||
| 80 | } |
||
| 81 | return $this->pageCounts; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get revision dates. |
||
| 86 | * @return int[] |
||
| 87 | */ |
||
| 88 | protected function getLogCounts() |
||
| 89 | { |
||
| 90 | if (! is_array($this->logCounts)) { |
||
| 91 | $this->logCounts = $this->getRepository() |
||
|
1 ignored issue
–
show
|
|||
| 92 | ->getLogCounts($this->project, $this->user); |
||
| 93 | } |
||
| 94 | return $this->logCounts; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function countLiveRevisions() |
||
| 98 | { |
||
| 99 | $revCounts = $this->getRevisionCounts(); |
||
| 100 | return isset($revCounts['live']) ? $revCounts['live'] : 0; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the total number of revisions that have been deleted. |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | public function countDeletedRevisions() |
||
| 108 | { |
||
| 109 | $revCounts = $this->getRevisionCounts(); |
||
| 110 | return isset($revCounts['deleted']) ? $revCounts['deleted'] : 0; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get the total edit count (live + deleted). |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function countAllRevisions() |
||
| 118 | { |
||
| 119 | return $this->countLiveRevisions() + $this->countDeletedRevisions(); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the total number of revisions with comments. |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function countRevisionsWithComments() |
||
| 127 | { |
||
| 128 | $revCounts = $this->getRevisionCounts(); |
||
| 129 | return isset($revCounts['with_comments']) ? $revCounts['with_comments'] : 0; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the total number of revisions without comments. |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | public function countRevisionsWithoutComments() |
||
| 137 | { |
||
| 138 | return $this->countAllRevisions() - $this->countRevisionsWithComments(); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the total number of revisions marked as 'minor' by the user. |
||
| 143 | * @return int |
||
| 144 | */ |
||
| 145 | public function countMinorRevisions() |
||
| 146 | { |
||
| 147 | $revCounts = $this->getRevisionCounts(); |
||
| 148 | return isset($revCounts['minor']) ? $revCounts['minor'] : 0; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the total number of revisions under 20 bytes. |
||
| 153 | */ |
||
| 154 | public function countSmallRevisions() |
||
| 155 | { |
||
| 156 | $revCounts = $this->getRevisionCounts(); |
||
| 157 | return isset($revCounts['small']) ? $revCounts['small'] : 0; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the total number of revisions over 1000 bytes. |
||
| 162 | */ |
||
| 163 | public function countLargeRevisions() |
||
| 164 | { |
||
| 165 | $revCounts = $this->getRevisionCounts(); |
||
| 166 | return isset($revCounts['large']) ? $revCounts['large'] : 0; |
||
| 167 | } |
||
| 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->getRevisionCounts(); |
||
| 176 | return round($revisionCounts['average_size'], 3); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the total number of non-deleted pages edited by the user. |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | public function countLivePagesEdited() |
||
| 184 | { |
||
| 185 | $pageCounts = $this->getPageCounts(); |
||
| 186 | return isset($pageCounts['edited-live']) ? $pageCounts['edited-live'] : 0; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the total number of deleted pages ever edited by the user. |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function countDeletedPagesEdited() |
||
| 194 | { |
||
| 195 | $pageCounts = $this->getPageCounts(); |
||
| 196 | return isset($pageCounts['edited-deleted']) ? $pageCounts['edited-deleted'] : 0; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
| 201 | * @return int |
||
| 202 | */ |
||
| 203 | public function countAllPagesEdited() |
||
| 204 | { |
||
| 205 | return $this->countLivePagesEdited() + $this->countDeletedPagesEdited(); |
||
| 206 | } |
||
| 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: