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 |
||
11 | class EditCounter extends Model |
||
12 | { |
||
13 | |||
14 | /** @var Project */ |
||
15 | protected $project; |
||
16 | |||
17 | /** @var User */ |
||
18 | protected $user; |
||
19 | |||
20 | /** @var int[] */ |
||
21 | protected $revisionCounts; |
||
22 | |||
23 | /** @var string[] */ |
||
24 | protected $revisionDates; |
||
25 | |||
26 | /** @var int[] */ |
||
27 | protected $pageCounts; |
||
28 | |||
29 | /** @var int[] */ |
||
30 | protected $logCounts; |
||
31 | |||
32 | public function __construct(Project $project, User $user) |
||
37 | |||
38 | /** |
||
39 | * Get revision count data. |
||
40 | * @return int[] |
||
41 | */ |
||
42 | protected function getRevisionCounts() |
||
50 | |||
51 | /** |
||
52 | * Get revision dates. |
||
53 | * @return int[] |
||
54 | */ |
||
55 | protected function getRevisionDates() |
||
63 | |||
64 | /** |
||
65 | * Get page count data. |
||
66 | * @return int[] |
||
67 | */ |
||
68 | protected function getPageCounts() |
||
76 | |||
77 | /** |
||
78 | * Get revision dates. |
||
79 | * @return int[] |
||
80 | */ |
||
81 | protected function getLogCounts() |
||
89 | |||
90 | public function countLiveRevisions() |
||
95 | |||
96 | /** |
||
97 | * Get the total number of revisions that have been deleted. |
||
98 | * @return int |
||
99 | */ |
||
100 | public function countDeletedRevisions() |
||
105 | |||
106 | /** |
||
107 | * Get the total edit count (live + deleted). |
||
108 | * @return int |
||
109 | */ |
||
110 | public function countAllRevisions() |
||
114 | |||
115 | /** |
||
116 | * Get the total number of revisions with comments. |
||
117 | * @return int |
||
118 | */ |
||
119 | public function countRevisionsWithComments() |
||
124 | |||
125 | /** |
||
126 | * Get the total number of revisions without comments. |
||
127 | * @return int |
||
128 | */ |
||
129 | public function countRevisionsWithoutComments() |
||
133 | |||
134 | /** |
||
135 | * Get the total number of revisions marked as 'minor' by the user. |
||
136 | * @return int |
||
137 | */ |
||
138 | public function countMinorRevisions() |
||
143 | |||
144 | /** |
||
145 | * Get the total number of revisions under 20 bytes. |
||
146 | */ |
||
147 | public function countSmallRevisions() |
||
152 | |||
153 | /** |
||
154 | * Get the total number of revisions over 1000 bytes. |
||
155 | */ |
||
156 | public function countLargeRevisions() |
||
161 | |||
162 | /** |
||
163 | * Get the average revision size for the user. |
||
164 | * @return float Size in bytes. |
||
165 | */ |
||
166 | public function averageRevisionSize() |
||
171 | |||
172 | /** |
||
173 | * Get the total number of non-deleted pages edited by the user. |
||
174 | * @return int |
||
175 | */ |
||
176 | public function countLivePagesEdited() |
||
181 | |||
182 | /** |
||
183 | * Get the total number of deleted pages ever edited by the user. |
||
184 | * @return int |
||
185 | */ |
||
186 | public function countDeletedPagesEdited() |
||
191 | |||
192 | /** |
||
193 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
194 | * @return int |
||
195 | */ |
||
196 | public function countAllPagesEdited() |
||
200 | |||
201 | /** |
||
202 | * Get the total number of semi-automated edits. |
||
203 | * @return int |
||
204 | */ |
||
205 | public function countAutomatedEdits() |
||
208 | |||
209 | /** |
||
210 | * Get the total number of pages (both still live and those that have been deleted) created |
||
211 | * by the user. |
||
212 | * @return int |
||
213 | */ |
||
214 | public function countPagesCreated() |
||
218 | |||
219 | /** |
||
220 | * Get the total number of pages created by the user, that have not been deleted. |
||
221 | * @return int |
||
222 | */ |
||
223 | public function countCreatedPagesLive() |
||
228 | |||
229 | /** |
||
230 | * Get the total number of pages created by the user, that have since been deleted. |
||
231 | * @return int |
||
232 | */ |
||
233 | public function countPagesCreatedDeleted() |
||
238 | |||
239 | /** |
||
240 | * Get the total number of pages that have been deleted by the user. |
||
241 | * @return int |
||
242 | */ |
||
243 | public function countPagesDeleted() |
||
248 | |||
249 | /** |
||
250 | * Get the total number of pages moved by the user. |
||
251 | * @return int |
||
252 | */ |
||
253 | public function countPagesMoved() |
||
258 | |||
259 | /** |
||
260 | * Get the average number of edits per page (including deleted revisions and pages). |
||
261 | * @return float |
||
262 | */ |
||
263 | public function averageRevisionsPerPage() |
||
270 | |||
271 | /** |
||
272 | * Average number of edits made per day. |
||
273 | * @return float |
||
274 | */ |
||
275 | public function averageRevisionsPerDay() |
||
282 | |||
283 | /** |
||
284 | * Get the total number of edits made by the user with semi-automating tools. |
||
285 | * @TODO |
||
286 | */ |
||
287 | public function countAutomatedRevisions() |
||
291 | |||
292 | /** |
||
293 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
294 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
295 | * @return int The total number of live edits. |
||
296 | */ |
||
297 | public function countRevisionsInLast($time) |
||
302 | |||
303 | /** |
||
304 | * Get the date and time of the user's first edit. |
||
305 | */ |
||
306 | public function datetimeFirstRevision() |
||
311 | |||
312 | /** |
||
313 | * Get the date and time of the user's first edit. |
||
314 | * @return DateTime |
||
315 | */ |
||
316 | public function datetimeLastRevision() |
||
321 | |||
322 | /** |
||
323 | * Get the number of days between the first and last edits. |
||
324 | * If there's only one edit, this is counted as one day. |
||
325 | * @return int |
||
326 | */ |
||
327 | public function getDays() |
||
332 | |||
333 | public function countFilesUploaded() |
||
338 | |||
339 | public function countFilesUploadedCommons() |
||
344 | |||
345 | /** |
||
346 | * Get the total number of revisions the user has sent thanks for. |
||
347 | * @return int |
||
348 | */ |
||
349 | public function thanks() |
||
354 | |||
355 | /** |
||
356 | * Get the total number of approvals |
||
357 | * @return int |
||
358 | */ |
||
359 | public function approvals() |
||
368 | |||
369 | /** |
||
370 | * @return int |
||
371 | */ |
||
372 | public function patrols() |
||
377 | |||
378 | /** |
||
379 | * Get the total edit counts for the top n projects of this user. |
||
380 | * @param User $user |
||
381 | * @param Project $project |
||
382 | * @param int $numProjects |
||
383 | * @return mixed[] Each element has 'total' and 'project' keys. |
||
384 | */ |
||
385 | public function topProjectsEditCounts(User $user, Project $project, $numProjects = 10) |
||
396 | |||
397 | /** |
||
398 | * Get the given user's total edit counts per namespace. |
||
399 | */ |
||
400 | public function namespaceTotals() |
||
406 | |||
407 | /** |
||
408 | * Get a summary of the times of day and the days of the week that the user has edited. |
||
409 | */ |
||
410 | public function timeCard() |
||
414 | |||
415 | /** |
||
416 | * |
||
417 | */ |
||
418 | public function yearCounts() |
||
437 | } |
||
438 |
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: