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 |
||
9 | class Search |
||
10 | { |
||
11 | |||
12 | private $tmdb = null; |
||
13 | private $logger = null; |
||
14 | private $page = 1; // Page number of the search result |
||
15 | private $total_pages = 1; // Total pages of the search result |
||
16 | private $total_results = 0; // Total results of the search result |
||
17 | |||
18 | /** |
||
19 | * Constructor |
||
20 | * @param \vfalies\tmdb\Tmdb $tmdb |
||
21 | */ |
||
22 | |||
23 | 38 | public function __construct(Tmdb $tmdb) |
|
28 | |||
29 | /** |
||
30 | * Search specify item |
||
31 | * @param string $item item to search : movie / tv / collection |
||
32 | * @param string $query Query string to search like a $item |
||
33 | * @param array $options Array of options for the request |
||
34 | * @param string $result_class class name of the wanted result |
||
35 | * @return \Generator |
||
36 | * @throws TmdbException |
||
37 | */ |
||
38 | 38 | private function searchItem($item, $query, array $options, $result_class) |
|
63 | |||
64 | /** |
||
65 | * Search Item generator method |
||
66 | * @param array $results |
||
67 | * @param string $class |
||
68 | */ |
||
69 | 28 | private function searchItemGenerator(array $results, $class) |
|
79 | |||
80 | /** |
||
81 | * Search a movie |
||
82 | * @param string $query Query string to search like a movie |
||
83 | * @param array $options Array of options for the search |
||
84 | * @return \Generator|Results\Movie |
||
85 | * @throws TmdbException |
||
86 | */ |
||
87 | 9 | View Code Duplication | public function searchMovie($query, array $options = array()) |
97 | |||
98 | /** |
||
99 | * Search a TV Show |
||
100 | * @param string $query Query string to search like a TV Show |
||
101 | * @param array $options Array of options for the request |
||
102 | * @return \Generator|Results\TVShow |
||
103 | * @throws TmdbException |
||
104 | */ |
||
105 | 8 | View Code Duplication | public function searchTVShow($query, array $options = array()) |
115 | |||
116 | /** |
||
117 | * Search a collection |
||
118 | * @param string $query Query string to search like a collection |
||
119 | * @param array $options Array of option for the request |
||
120 | * @return \Generator|Results\Collection |
||
121 | * @throws TmdbException |
||
122 | */ |
||
123 | 5 | View Code Duplication | public function searchCollection($query, array $options = array()) |
133 | |||
134 | /** |
||
135 | * Search a people |
||
136 | * @param string $query Query string to search like a people |
||
137 | * @param array $options Array of option for the request |
||
138 | * @return \Generator|Results\People |
||
139 | * @throws TmdbException |
||
140 | */ |
||
141 | 9 | View Code Duplication | public function searchPeople($query, array $options = array()) |
153 | |||
154 | /** |
||
155 | * Search a company |
||
156 | * @param string $query Query string to search like a company |
||
157 | * @param array $options Array of option for the request |
||
158 | * @return \Generator|Results\Company |
||
159 | * @throws TmdbException |
||
160 | */ |
||
161 | 7 | View Code Duplication | public function searchCompany($query, array $options = array()) |
173 | |||
174 | /** |
||
175 | * Get page from result search |
||
176 | * @return int |
||
177 | */ |
||
178 | 1 | public function getPage() |
|
182 | |||
183 | /** |
||
184 | * Get total page from result search |
||
185 | * @return int |
||
186 | */ |
||
187 | 1 | public function getTotalPages() |
|
191 | |||
192 | /** |
||
193 | * Get total results from search |
||
194 | * @return int |
||
195 | */ |
||
196 | 1 | public function getTotalResults() |
|
200 | |||
201 | } |
||
202 |
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.