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 |
||
30 | class TVSeason extends Item implements TVSeasonInterface |
||
31 | { |
||
32 | |||
33 | use ElementTrait; |
||
34 | |||
35 | /** |
||
36 | * Season number |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $season_number; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * @param \vfalies\tmdb\Interfaces\TmdbInterface $tmdb |
||
44 | * @param int $tv_id |
||
45 | * @param int $season_number |
||
46 | * @param array $options |
||
47 | * @throws Exception |
||
48 | */ |
||
49 | 34 | public function __construct(TmdbInterface $tmdb, $tv_id, $season_number, array $options = array()) |
|
55 | |||
56 | /** |
||
57 | * Id |
||
58 | * @return int |
||
59 | */ |
||
60 | 2 | public function getId() |
|
68 | |||
69 | /** |
||
70 | * Air date |
||
71 | * @return string |
||
72 | */ |
||
73 | 2 | public function getAirDate() |
|
81 | |||
82 | /** |
||
83 | * Episode count |
||
84 | * @return int |
||
85 | */ |
||
86 | 2 | public function getEpisodeCount() |
|
94 | |||
95 | /** |
||
96 | * Season number |
||
97 | * @return int |
||
98 | */ |
||
99 | 2 | public function getSeasonNumber() |
|
107 | |||
108 | /** |
||
109 | * Episodes list |
||
110 | * @return \Generator|Results\TVEpisode |
||
111 | */ |
||
112 | 21 | public function getEpisodes() |
|
123 | |||
124 | /** |
||
125 | * Name |
||
126 | * @return string |
||
127 | */ |
||
128 | 2 | public function getName() |
|
136 | |||
137 | /** |
||
138 | * Overview |
||
139 | * @return string |
||
140 | */ |
||
141 | 2 | public function getOverview() |
|
149 | |||
150 | /** |
||
151 | * Posters list |
||
152 | * @return \Generator|Results\Image |
||
153 | */ |
||
154 | 1 | View Code Duplication | public function getPosters() |
164 | |||
165 | } |
||
166 |
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.