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:
1 | <?php declare(strict_types=1); |
||
27 | View Code Duplication | class WatchList extends Account |
|
|
|||
28 | { |
||
29 | use ListItems; |
||
30 | |||
31 | /** |
||
32 | * Get movies watchlist |
||
33 | * @return \Generator|Results\Movie |
||
34 | */ |
||
35 | 1 | public function getMovies() : \Generator |
|
39 | |||
40 | /** |
||
41 | * Get TV shows watchlist |
||
42 | * @return \Generator|Results\TVShow |
||
43 | */ |
||
44 | 1 | public function getTVShows() : \Generator |
|
48 | |||
49 | /** |
||
50 | * Add movie in watchlist |
||
51 | * @param int $movie_id movie id |
||
52 | * @return WatchList |
||
53 | */ |
||
54 | 2 | public function addMovie(int $movie_id) : WatchList |
|
58 | |||
59 | /** |
||
60 | * Remove movie from watchlist |
||
61 | * @param int $movie_id movie id |
||
62 | * @return WatchList |
||
63 | */ |
||
64 | 1 | public function removeMovie(int $movie_id) : WatchList |
|
68 | |||
69 | /** |
||
70 | * Add TV show in watchlist |
||
71 | * @param int $tvshow_id TV show id |
||
72 | * @return WatchList |
||
73 | */ |
||
74 | 1 | public function addTVShow(int $tvshow_id) : WatchList |
|
78 | |||
79 | /** |
||
80 | * Remove TV show from watchlist |
||
81 | * @param int $tvshow_id TV show id |
||
82 | * @return WatchList |
||
83 | */ |
||
84 | 1 | public function removeTVShow(int $tvshow_id) : WatchList |
|
88 | } |
||
89 |
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.