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); |
||
26 | class Item |
||
27 | { |
||
28 | /** |
||
29 | * Tmdb object |
||
30 | * @var TmdbInterface |
||
31 | */ |
||
32 | private $tmdb = null; |
||
33 | /** |
||
34 | * Logger object |
||
35 | * @var \Psr\Log\LoggerInterface |
||
36 | */ |
||
37 | private $logger = null; |
||
38 | |||
39 | /** |
||
40 | * Constructor |
||
41 | * @param TmdbInterface $tmdb |
||
42 | */ |
||
43 | |||
44 | 7 | public function __construct(TmdbInterface $tmdb) |
|
49 | |||
50 | /** |
||
51 | * Get movie details |
||
52 | * @param int $movie_id |
||
53 | * @param array $options |
||
54 | * @return Items\Movie |
||
55 | */ |
||
56 | 1 | public function getMovie(int $movie_id, array $options = array()) : Items\Movie |
|
63 | |||
64 | /** |
||
65 | * Get collection details |
||
66 | * @param int $collection_id |
||
67 | * @param array $options |
||
68 | * @return Items\Collection |
||
69 | */ |
||
70 | 1 | public function getCollection(int $collection_id, array $options = array()) : Items\Collection |
|
77 | |||
78 | /** |
||
79 | * Get TV Show details |
||
80 | * @param int $tv_id |
||
81 | * @param array $options |
||
82 | * @return Items\TVShow |
||
83 | */ |
||
84 | 1 | View Code Duplication | public function getTVShow(int $tv_id, array $options = array()) : Items\TVShow |
91 | |||
92 | /** |
||
93 | * Get TV Show episode details |
||
94 | * @param int $tv_id |
||
95 | * @param int $season_number |
||
96 | * @param array $options |
||
97 | * @return Items\TVEpisode |
||
98 | */ |
||
99 | 1 | View Code Duplication | public function getTVSeason(int $tv_id, int $season_number, array $options = array()) : Items\TVSeason |
106 | |||
107 | /** |
||
108 | * Get TV Show episode details |
||
109 | * @param int $tv_id |
||
110 | * @param int $season_number |
||
111 | * @param int $episode_number |
||
112 | * @param array $options |
||
113 | * @return Items\TVEpisode |
||
114 | */ |
||
115 | 1 | View Code Duplication | public function getTVEpisode(int $tv_id, int $season_number, int $episode_number, array $options = array()) : Items\TVEpisode |
122 | |||
123 | /** |
||
124 | * Get People details |
||
125 | * @param int $people_id |
||
126 | * @param array $options |
||
127 | * @return Items\People |
||
128 | */ |
||
129 | 1 | public function getPeople(int $people_id, array $options = array()) : Items\People |
|
136 | |||
137 | /** |
||
138 | * Get Company details |
||
139 | * @param int $company_id |
||
140 | * @param array $options |
||
141 | * @return Items\Company |
||
142 | */ |
||
143 | 1 | public function getCompany(int $company_id, array $options = array()) : Items\Company |
|
150 | } |
||
151 |
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.