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); |
||
30 | class TVEpisode extends Abstracts\Results implements TVEpisodeResultsInterface |
||
31 | { |
||
32 | use ElementTrait; |
||
33 | use TVEpisodeTrait; |
||
34 | |||
35 | /** |
||
36 | * Episode number |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $episode_number = 0; |
||
40 | /** |
||
41 | * Name |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $name = ''; |
||
45 | /** |
||
46 | * Air date |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $air_date = null; |
||
50 | /** |
||
51 | * Season number |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $season_number = 0; |
||
55 | /** |
||
56 | * Vote average |
||
57 | * @var float |
||
58 | */ |
||
59 | protected $vote_average = 0; |
||
60 | /** |
||
61 | * Vote count |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $vote_count = 0; |
||
65 | /** |
||
66 | * Overview |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $overview = ''; |
||
70 | /** |
||
71 | * Production code |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $production_code = ''; |
||
75 | /** |
||
76 | * Image still path |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $still_path = ''; |
||
80 | /** |
||
81 | * Id |
||
82 | * @var int |
||
83 | */ |
||
84 | protected $id = null; |
||
85 | /** |
||
86 | * Guest stars |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $guest_stars = []; |
||
90 | |||
91 | /** |
||
92 | * Constructor |
||
93 | * @param TmdbInterface $tmdb |
||
94 | * @param \stdClass $result |
||
95 | * @throws \Exception |
||
96 | */ |
||
97 | 21 | public function __construct(TmdbInterface $tmdb, \stdClass $result) |
|
119 | |||
120 | /** |
||
121 | * initResultObject |
||
122 | * @param \stdClass $result |
||
123 | * @return \stdClass |
||
124 | */ |
||
125 | 21 | private function initResultObject(\stdClass $result) : \stdClass |
|
138 | |||
139 | /** |
||
140 | * Air date |
||
141 | * @return string |
||
142 | */ |
||
143 | 1 | public function getAirDate() : string |
|
147 | |||
148 | /** |
||
149 | * Episode number |
||
150 | * @return int |
||
151 | */ |
||
152 | 1 | public function getEpisodeNumber() : int |
|
156 | |||
157 | /** |
||
158 | * Guests stars |
||
159 | * @return \Generator|Results\Cast |
||
160 | */ |
||
161 | 1 | View Code Duplication | public function getGuestStars() : \Generator |
173 | |||
174 | /** |
||
175 | * Id |
||
176 | * @return int |
||
177 | */ |
||
178 | 1 | public function getId() : int |
|
182 | |||
183 | /** |
||
184 | * Name |
||
185 | * @return string |
||
186 | */ |
||
187 | 1 | public function getName() : string |
|
191 | |||
192 | /** |
||
193 | * Note |
||
194 | * @return float |
||
195 | */ |
||
196 | 1 | public function getNote() : float |
|
200 | |||
201 | /** |
||
202 | * Note count |
||
203 | * @return int |
||
204 | */ |
||
205 | 1 | public function getNoteCount() : int |
|
209 | |||
210 | /** |
||
211 | * Overview |
||
212 | * @return string |
||
213 | */ |
||
214 | 1 | public function getOverview() : string |
|
218 | |||
219 | /** |
||
220 | * Production code |
||
221 | * @return string |
||
222 | */ |
||
223 | 1 | public function getProductionCode() : string |
|
227 | |||
228 | /** |
||
229 | * Season number |
||
230 | * @return int |
||
231 | */ |
||
232 | 1 | public function getSeasonNumber() : int |
|
236 | |||
237 | /** |
||
238 | * Image still path |
||
239 | * @return string |
||
240 | */ |
||
241 | 1 | public function getStillPath() : string |
|
245 | } |
||
246 |
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.