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() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Average number of edits made per day. |
||
| 269 | * @return float |
||
| 270 | */ |
||
| 271 | public function averageRevisionsPerDay() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the total number of edits made by the user with semi-automating tools. |
||
| 278 | * @TODO |
||
| 279 | */ |
||
| 280 | public function countAutomatedRevisions() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
| 287 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
| 288 | * @return int The total number of live edits. |
||
| 289 | */ |
||
| 290 | public function countRevisionsInLast($time) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the date and time of the user's first edit. |
||
| 298 | */ |
||
| 299 | public function datetimeFirstRevision() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the date and time of the user's first edit. |
||
| 307 | * @return DateTime |
||
| 308 | */ |
||
| 309 | public function datetimeLastRevision() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get the number of days between the first and last edits. |
||
| 317 | * If there's only one edit, this is counted as one day. |
||
| 318 | * @return int |
||
| 319 | */ |
||
| 320 | public function getDays() |
||
| 325 | |||
| 326 | public function countFilesUploaded() |
||
| 331 | |||
| 332 | public function countFilesUploadedCommons() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get the total number of revisions the user has sent thanks for. |
||
| 340 | * @return int |
||
| 341 | */ |
||
| 342 | public function thanks() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get the total number of approvals |
||
| 350 | * @return int |
||
| 351 | */ |
||
| 352 | public function approvals() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return int |
||
| 364 | */ |
||
| 365 | public function patrols() |
||
| 370 | } |
||
| 371 |
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: