Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TheSportsDb 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 TheSportsDb, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class TheSportsDb implements EntityManagerConsumerInterface { |
||
20 | |||
21 | use EntityManagerConsumerTrait; |
||
22 | |||
23 | /** |
||
24 | * Creates a new TheSportsDb instance |
||
25 | */ |
||
26 | public function __construct(EntityManagerInterface $entityManager = NULL) { |
||
31 | |||
32 | /** |
||
33 | * Gets all sports. |
||
34 | * |
||
35 | * @return \TheSportsDb\Entity\SportInterface[] |
||
36 | * The sports. |
||
37 | */ |
||
38 | 1 | public function getSports() { |
|
41 | |||
42 | /** |
||
43 | * Get a sport by name. |
||
44 | * |
||
45 | * @param string $name |
||
46 | * The sport name. |
||
47 | * |
||
48 | * @return \TheSportsDb\Entity\SportInterface |
||
49 | * The sport. |
||
50 | */ |
||
51 | 1 | View Code Duplication | public function getSport($name) { |
63 | |||
64 | /** |
||
65 | * Gets all leagues. |
||
66 | * |
||
67 | * @return \TheSportsDb\Entity\LeagueInterface[] |
||
68 | * The leagues. |
||
69 | */ |
||
70 | 1 | public function getLeagues() { |
|
73 | |||
74 | /** |
||
75 | * Get a league by id. |
||
76 | * |
||
77 | * @param int $leagueId |
||
78 | * The league id. |
||
79 | * |
||
80 | * @return \TheSportsDb\Entity\LeagueInterface |
||
81 | * The league. |
||
82 | */ |
||
83 | 1 | View Code Duplication | public function getLeague($leagueId) { |
95 | |||
96 | /** |
||
97 | * Gets all leagues by country. |
||
98 | * |
||
99 | * @param string $country |
||
100 | * The country of which to get the leagues. |
||
101 | * |
||
102 | * @return \TheSportsDb\Entity\LeagueInterface[] |
||
103 | * The leagues. |
||
104 | */ |
||
105 | 1 | public function getLeaguesByCountry($country) { |
|
108 | |||
109 | /** |
||
110 | * Gets all leagues by sport. |
||
111 | * |
||
112 | * @param mixed $sport |
||
113 | * The sport of which to get the leagues. |
||
114 | * |
||
115 | * @return \TheSportsDb\Entity\LeagueInterface[] |
||
116 | * The leagues. |
||
117 | */ |
||
118 | 1 | public function getLeaguesBySport($sport) { |
|
121 | |||
122 | /** |
||
123 | * Gets all leagues by country and sport. |
||
124 | * |
||
125 | * @param string $country |
||
126 | * The country of which to get the leagues. |
||
127 | * @param mixed $sport |
||
128 | * The sport of which to get the leagues. |
||
129 | * |
||
130 | * @return \TheSportsDb\Entity\LeagueInterface[] |
||
131 | * The leagues. |
||
132 | */ |
||
133 | 1 | public function getLeaguesByCountryAndSport($country, $sport) { |
|
136 | |||
137 | /** |
||
138 | * Gets a team by id. |
||
139 | * |
||
140 | * @param mixed $teamId |
||
141 | * The team id. |
||
142 | * |
||
143 | * @return \TheSportsDb\Entity\TeamInterface |
||
144 | * The team. |
||
145 | */ |
||
146 | 1 | View Code Duplication | public function getTeam($teamId) { |
158 | |||
159 | /** |
||
160 | * Gets a team by name. |
||
161 | * |
||
162 | * @param string $teamName |
||
163 | * The team name. |
||
164 | * |
||
165 | * @return \TheSportsDb\Entity\TeamInterface[] |
||
166 | * The team. |
||
167 | */ |
||
168 | 1 | public function getTeamsByName($teamName) { |
|
171 | |||
172 | /** |
||
173 | * Gets a teams by league id. |
||
174 | * |
||
175 | * @param mixed $leagueId |
||
176 | * The league id. |
||
177 | * |
||
178 | * @return \TheSportsDb\Entity\TeamInterface[] |
||
179 | * The teams. |
||
180 | */ |
||
181 | 1 | public function getTeamsByLeague($leagueId) { |
|
184 | |||
185 | /** |
||
186 | * Gets a teams by league name. |
||
187 | * |
||
188 | * @param string $leagueName |
||
189 | * The league name. |
||
190 | * |
||
191 | * @return \TheSportsDb\Entity\TeamInterface[] |
||
192 | * The teams. |
||
193 | */ |
||
194 | 1 | public function getTeamsByLeagueName($leagueName) { |
|
197 | |||
198 | /** |
||
199 | * Gets a teams by sport and country. |
||
200 | * |
||
201 | * @param mixed $sport |
||
202 | * The sport. |
||
203 | * @param string $country |
||
204 | * The country. |
||
205 | * |
||
206 | * @return \TheSportsDb\Entity\TeamInterface[] |
||
207 | * The teams. |
||
208 | */ |
||
209 | 1 | public function getTeamsBySportAndCountry($sport, $country) { |
|
212 | |||
213 | /** |
||
214 | * Get a player by id. |
||
215 | * |
||
216 | * @param mixed $playerId |
||
217 | * The player id. |
||
218 | * |
||
219 | * @return \TheSportsDb\Entity\PlayerInterface |
||
220 | * The player. |
||
221 | */ |
||
222 | 1 | View Code Duplication | public function getPlayer($playerId) { |
234 | |||
235 | /** |
||
236 | * Get a players by team id. |
||
237 | * |
||
238 | * @param mixed $teamId |
||
239 | * The team id. |
||
240 | * |
||
241 | * @return \TheSportsDb\Entity\PlayerInterface[] |
||
242 | * The players. |
||
243 | */ |
||
244 | 1 | public function getPlayersByTeam($teamId) { |
|
247 | |||
248 | /** |
||
249 | * Get a players by team name. |
||
250 | * |
||
251 | * @param mixed $teamName |
||
252 | * The team name. |
||
253 | * |
||
254 | * @return \TheSportsDb\Entity\PlayerInterface[] |
||
255 | * The players. |
||
256 | */ |
||
257 | 1 | public function getPlayersByTeamName($teamName) { |
|
260 | |||
261 | /** |
||
262 | * Get a players by name. |
||
263 | * |
||
264 | * @param mixed $name |
||
265 | * The name. |
||
266 | * |
||
267 | * @return \TheSportsDb\Entity\PlayerInterface[] |
||
268 | * The players. |
||
269 | */ |
||
270 | 1 | public function getPlayersByName($name) { |
|
273 | |||
274 | /** |
||
275 | * Get a players by team and name. |
||
276 | * |
||
277 | * @param string $teamName |
||
278 | * The team name. |
||
279 | * @param string $name |
||
280 | * The name. |
||
281 | * |
||
282 | * @return \TheSportsDb\Entity\PlayerInterface[] |
||
283 | * The players. |
||
284 | */ |
||
285 | 1 | public function getPlayersByTeamNameAndName($teamName, $name) { |
|
288 | |||
289 | /** |
||
290 | * Get an event by id. |
||
291 | * |
||
292 | * @param mixed $eventId |
||
293 | * The event id. |
||
294 | * |
||
295 | * @return \TheSportsDb\Entity\EventInterface |
||
296 | * The event. |
||
297 | */ |
||
298 | 1 | View Code Duplication | public function getEvent($eventId) { |
310 | |||
311 | /** |
||
312 | * Get events by name. |
||
313 | * |
||
314 | * @param string $name |
||
315 | * The event name. |
||
316 | * |
||
317 | * @return \TheSportsDb\Entity\EventInterface[] |
||
318 | * The events. |
||
319 | */ |
||
320 | 1 | public function getEventsByName($name) { |
|
323 | |||
324 | /** |
||
325 | * Get events by file name. |
||
326 | * |
||
327 | * @param string $name |
||
328 | * The file name. |
||
329 | * |
||
330 | * @return \TheSportsDb\Entity\EventInterface[] |
||
331 | * The events. |
||
332 | */ |
||
333 | 1 | public function getEventsByFileName($name) { |
|
336 | |||
337 | /** |
||
338 | * Get events by name and season. |
||
339 | * |
||
340 | * @param string $name |
||
341 | * The event name. |
||
342 | * @param mixed $season |
||
343 | * The season. |
||
344 | * |
||
345 | * @return \TheSportsDb\Entity\EventInterface[] |
||
346 | * The events. |
||
347 | */ |
||
348 | 1 | public function getEventsByNameAndSeason($name, $season) { |
|
351 | |||
352 | /** |
||
353 | * Get next five events by team. |
||
354 | * |
||
355 | * @param mixed $teamId |
||
356 | * The team id. |
||
357 | * |
||
358 | * @return \TheSportsDb\Entity\EventInterface[] |
||
359 | * The events. |
||
360 | */ |
||
361 | 1 | public function getNextFiveEventsByTeam($teamId) { |
|
364 | |||
365 | /** |
||
366 | * Get next fifteen events by league. |
||
367 | * |
||
368 | * @param mixed $leagueId |
||
369 | * The league id. |
||
370 | * |
||
371 | * @return \TheSportsDb\Entity\EventInterface[] |
||
372 | * The events. |
||
373 | */ |
||
374 | 1 | public function getNextFifteenEventsByLeague($leagueId) { |
|
377 | |||
378 | /** |
||
379 | * Get next fifteen events by league and round. |
||
380 | * |
||
381 | * @param mixed $leagueId |
||
382 | * The league id. |
||
383 | * @param int $round |
||
384 | * The round. |
||
385 | * |
||
386 | * @return \TheSportsDb\Entity\EventInterface[] |
||
387 | * The events. |
||
388 | */ |
||
389 | 1 | public function getNextFifteenEventsByLeagueAndRound($leagueId, $round) { |
|
392 | |||
393 | /** |
||
394 | * Get last five events by team. |
||
395 | * |
||
396 | * @param mixed $teamId |
||
397 | * The team id. |
||
398 | * |
||
399 | * @return \TheSportsDb\Entity\EventInterface[] |
||
400 | * The events. |
||
401 | */ |
||
402 | 1 | public function getLastFiveEventsByTeam($teamId) { |
|
405 | |||
406 | /** |
||
407 | * Get last fifteen events by league. |
||
408 | * |
||
409 | * @param mixed $leagueId |
||
410 | * The league id. |
||
411 | * |
||
412 | * @return \TheSportsDb\Entity\EventInterface[] |
||
413 | * The events. |
||
414 | */ |
||
415 | 1 | public function getLastFifteenEventsByLeague($leagueId) { |
|
418 | |||
419 | /** |
||
420 | * Get events by day. |
||
421 | * |
||
422 | * @param \DateTime $date |
||
423 | * The day. |
||
424 | * @param string|null $sport |
||
425 | * The sport. |
||
426 | * @param string|null $leagueName |
||
427 | * The league name. |
||
428 | * |
||
429 | * @return \TheSportsDb\Entity\EventInterface[] |
||
430 | * The events. |
||
431 | */ |
||
432 | 1 | public function getEventsByDay(\DateTime $date, $sport = NULL, $leagueName = NULL) { |
|
435 | |||
436 | /** |
||
437 | * Get events by league, round and season. |
||
438 | * |
||
439 | * @param mixed $leagueId |
||
440 | * The league id. |
||
441 | * @param int $round |
||
442 | * The round. |
||
443 | * @param mixed $season |
||
444 | * The season. |
||
445 | * |
||
446 | * @return \TheSportsDb\Entity\EventInterface[] |
||
447 | * The events. |
||
448 | */ |
||
449 | 1 | public function getEventsByLeagueRoundAndSeason($leagueId, $round, $season) { |
|
452 | |||
453 | /** |
||
454 | * Get events by league and season. |
||
455 | * |
||
456 | * @param mixed $leagueId |
||
457 | * The league id. |
||
458 | * @param mixed $season |
||
459 | * The season. |
||
460 | * |
||
461 | * @return \TheSportsDb\Entity\EventInterface[] |
||
462 | * The events. |
||
463 | */ |
||
464 | 1 | public function getEventsByLeagueAndSeason($leagueId, $season) { |
|
467 | |||
468 | /** |
||
469 | * Get seasons by league. |
||
470 | * |
||
471 | * @param mixed $leagueId |
||
472 | * The league id. |
||
473 | * |
||
474 | * @return \TheSportsDb\Entity\SeasonInterface[] |
||
475 | * The seasons. |
||
476 | */ |
||
477 | 1 | public function getSeasonsByLeague($leagueId) { |
|
480 | } |
||
481 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.