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 |
||
33 | class TVEpisode extends Item implements TVEpisodeInterface |
||
34 | { |
||
35 | |||
36 | use ElementTrait; |
||
37 | use TVEpisodeTrait; |
||
38 | |||
39 | /** |
||
40 | * Season number |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $season_number; |
||
44 | /** |
||
45 | * Episode number |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $episode_number; |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * @param \vfalies\tmdb\Interfaces\TmdbInterface $tmdb |
||
53 | * @param int $tv_id |
||
54 | * @param int $season_number |
||
55 | * @param int $episode_number |
||
56 | * @param array $options |
||
57 | */ |
||
58 | 23 | public function __construct(TmdbInterface $tmdb, $tv_id, $season_number, $episode_number, array $options = array()) |
|
65 | |||
66 | /** |
||
67 | * Get TV show id |
||
68 | * @return int |
||
69 | */ |
||
70 | 2 | public function getId() |
|
78 | |||
79 | /** |
||
80 | * Air date |
||
81 | * @return string |
||
82 | */ |
||
83 | 2 | public function getAirDate() |
|
91 | |||
92 | /** |
||
93 | * Episode number |
||
94 | * @return int |
||
95 | */ |
||
96 | 2 | public function getEpisodeNumber() |
|
104 | |||
105 | /** |
||
106 | * Guests stars |
||
107 | * @return \Generator|Results\Cast |
||
108 | */ |
||
109 | 1 | View Code Duplication | public function getGuestStars() |
|
|||
110 | { |
||
111 | 1 | if (isset($this->data->guest_stars)) |
|
112 | { |
||
113 | 1 | foreach ($this->data->guest_stars as $gs) |
|
114 | { |
||
115 | 1 | $gs->gender = null; |
|
116 | 1 | $gs->cast_id = null; |
|
117 | |||
118 | 1 | $star = new Cast($this->tmdb, $gs); |
|
119 | 1 | yield $star; |
|
120 | } |
||
121 | } |
||
122 | 1 | } |
|
123 | |||
124 | /** |
||
125 | * Name |
||
126 | * @return string |
||
127 | */ |
||
128 | 2 | public function getName() |
|
136 | |||
137 | /** |
||
138 | * Note |
||
139 | * @return float |
||
140 | */ |
||
141 | 2 | public function getNote() |
|
149 | |||
150 | /** |
||
151 | * Note count |
||
152 | * @return int |
||
153 | */ |
||
154 | 2 | public function getNoteCount() |
|
162 | |||
163 | /** |
||
164 | * Overview |
||
165 | * @return string |
||
166 | */ |
||
167 | 2 | public function getOverview() |
|
175 | |||
176 | /** |
||
177 | * Production code |
||
178 | * @return string |
||
179 | */ |
||
180 | 2 | public function getProductionCode() |
|
188 | |||
189 | /** |
||
190 | * Season number |
||
191 | * @return int |
||
192 | */ |
||
193 | 2 | public function getSeasonNumber() |
|
201 | |||
202 | /** |
||
203 | * Image still path |
||
204 | * @return string |
||
205 | */ |
||
206 | 2 | public function getStillPath() |
|
214 | |||
215 | /** |
||
216 | * Image posters |
||
217 | * @return \Generator|Results\Image |
||
218 | */ |
||
219 | 1 | View Code Duplication | public function getPosters() |
229 | |||
230 | } |
||
231 |
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.