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 total number of non-deleted pages edited by the user. |
||
163 | * @return int |
||
164 | */ |
||
165 | public function countLivePagesEdited() |
||
170 | |||
171 | public function countDeletedPagesEdited() |
||
176 | |||
177 | /** |
||
178 | * Get the total number of pages ever edited by this user (both live and deleted). |
||
179 | * @return int |
||
180 | */ |
||
181 | public function countAllPagesEdited() |
||
185 | |||
186 | /** |
||
187 | * Get the total number of semi-automated edits. |
||
188 | * @return int |
||
189 | */ |
||
190 | public function countAutomatedEdits() |
||
193 | |||
194 | /** |
||
195 | * Get the total number of pages (both still live and those that have been deleted) created |
||
196 | * by the user. |
||
197 | * @return int |
||
198 | */ |
||
199 | public function countPagesCreated() |
||
203 | |||
204 | /** |
||
205 | * Get the total number of pages created by the user, that have not been deleted. |
||
206 | * @return int |
||
207 | */ |
||
208 | public function countCreatedPagesLive() |
||
213 | |||
214 | /** |
||
215 | * Get the total number of pages created by the user, that have since been deleted. |
||
216 | * @return int |
||
217 | */ |
||
218 | public function countPagesCreatedDeleted() |
||
223 | |||
224 | /** |
||
225 | * Get the total number of pages moved by the user. |
||
226 | * @return int |
||
227 | */ |
||
228 | public function countPagesMoved() |
||
233 | |||
234 | /** |
||
235 | * Get the average number of edits per page (including deleted revisions and pages). |
||
236 | * @return float |
||
237 | */ |
||
238 | public function averageRevisionsPerPage() |
||
242 | |||
243 | /** |
||
244 | * Average number of edits made per day. |
||
245 | * @return float |
||
246 | */ |
||
247 | public function averageRevisionsPerDay() |
||
251 | |||
252 | /** |
||
253 | * Get the total number of edits made by the user with semi-automating tools. |
||
254 | * @TODO |
||
255 | */ |
||
256 | public function countAutomatedRevisions() |
||
260 | |||
261 | /** |
||
262 | * Get the count of (non-deleted) edits made in the given timeframe to now. |
||
263 | * @param string $time One of 'day', 'week', 'month', or 'year'. |
||
264 | * @return int The total number of live edits. |
||
265 | */ |
||
266 | public function countRevisionsInLast($time) |
||
271 | |||
272 | /** |
||
273 | * Get the date and time of the user's first edit. |
||
274 | */ |
||
275 | public function datetimeFirstRevision() |
||
280 | |||
281 | /** |
||
282 | * Get the date and time of the user's first edit. |
||
283 | * @return DateTime |
||
284 | */ |
||
285 | public function datetimeLastRevision() |
||
290 | |||
291 | /** |
||
292 | * Get the number of days between the first and last edits. |
||
293 | * If there's only one edit, this is counted as one day. |
||
294 | * @return int |
||
295 | */ |
||
296 | public function getDays() |
||
301 | |||
302 | public function countFilesUploaded() |
||
307 | |||
308 | public function countFilesUploadedCommons() |
||
313 | |||
314 | /** |
||
315 | * Get the total number of revisions the user has sent thanks for. |
||
316 | * @return int |
||
317 | */ |
||
318 | public function thanks() |
||
323 | |||
324 | /** |
||
325 | * Get the total number of approvals |
||
326 | * @return int |
||
327 | */ |
||
328 | public function approvals() |
||
337 | |||
338 | /** |
||
339 | * @return int |
||
340 | */ |
||
341 | public function patrols() |
||
346 | } |
||
347 |
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: