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); |
||
28 | class Rated extends Account |
||
29 | { |
||
30 | use ListItems; |
||
31 | |||
32 | /** |
||
33 | * Get movies rated |
||
34 | * @return \Generator|Results\Movie |
||
35 | */ |
||
36 | 1 | View Code Duplication | public function getMovies() : \Generator |
46 | |||
47 | /** |
||
48 | * Get TV shows rated |
||
49 | * @return \Generator|Results\TVShow |
||
50 | */ |
||
51 | 1 | View Code Duplication | public function getTVShows() : \Generator |
61 | |||
62 | /** |
||
63 | * Get TV episodes rated |
||
64 | * @return \Generator|Results\TVEpisode |
||
65 | */ |
||
66 | 1 | View Code Duplication | public function getTVEpisodes() : \Generator |
76 | |||
77 | /** |
||
78 | * Add movie rate |
||
79 | * @param int $movie_id |
||
80 | * @param float $rate |
||
81 | * @return Rated |
||
82 | */ |
||
83 | 2 | public function addMovieRate(int $movie_id, float $rate) : Rated |
|
87 | |||
88 | /** |
||
89 | * Remove movie rate |
||
90 | * @param int $movie_id |
||
91 | * @return Rated |
||
92 | */ |
||
93 | 2 | public function removeMovieRate(int $movie_id) : Rated |
|
97 | |||
98 | /** |
||
99 | * Add TVShow rate |
||
100 | * @param int $tv_id |
||
101 | * @param float $rate |
||
102 | * @return Rated |
||
103 | */ |
||
104 | 1 | public function addTVShowRate(int $tv_id, float $rate) : Rated |
|
108 | |||
109 | /** |
||
110 | * Remove TVShow rate |
||
111 | * @param int $tv_id |
||
112 | * @return Rated |
||
113 | */ |
||
114 | 1 | public function removeTVShowRate(int $tv_id) : Rated |
|
118 | |||
119 | /** |
||
120 | * Add TVShow episode rate |
||
121 | * @param int $tv_id |
||
122 | * @param int $season_number |
||
123 | * @param int $episode_number |
||
124 | * @param float $rate |
||
125 | * @return Rated |
||
126 | */ |
||
127 | 1 | public function addTVShowEpisodeRate(int $tv_id, int $season_number, int $episode_number, float $rate) : Rated |
|
131 | |||
132 | /** |
||
133 | * Remove TVSHow episode rate |
||
134 | * @param int $tv_id |
||
135 | * @param int $season_number |
||
136 | * @param int $episode_number |
||
137 | * @return Rated |
||
138 | */ |
||
139 | 1 | public function removeTVShowEpisodeRate(int $tv_id, int $season_number, int $episode_number) : Rated |
|
143 | |||
144 | /** |
||
145 | * Add rate |
||
146 | * @param string $action uri action |
||
147 | * @param float $rate |
||
148 | * @return Rated |
||
149 | */ |
||
150 | 4 | private function addRate(string $action, float $rate) : Rated |
|
163 | |||
164 | /** |
||
165 | * Remove Rate |
||
166 | * @param string $action uri action |
||
167 | * @return Rated |
||
168 | */ |
||
169 | 4 | private function removeRate(string $action) : Rated |
|
179 | } |
||
180 |
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.