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); |
||
29 | class Search |
||
30 | { |
||
31 | use ListItems; |
||
32 | use GeneratorTrait; |
||
33 | |||
34 | /** |
||
35 | * Tmdb object |
||
36 | * @var TmdbInterface |
||
37 | */ |
||
38 | private $tmdb = null; |
||
39 | /** |
||
40 | * Logger |
||
41 | * @var \Psr\Log\LoggerInterface |
||
42 | */ |
||
43 | private $logger = null; |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * @param TmdbInterface $tmdb |
||
48 | */ |
||
49 | 33 | public function __construct(TmdbInterface $tmdb) |
|
55 | |||
56 | /** |
||
57 | * Search specify item |
||
58 | * @param string $item item to search : movie / tv / collection |
||
59 | * @param string $query Query string to search like a $item |
||
60 | * @param array $options Array of options for the request |
||
61 | * @param string $result_class class name of the wanted result |
||
62 | * @return \Generator |
||
63 | * @throws TmdbException |
||
64 | */ |
||
65 | 33 | private function searchItem(string $item, string $query, array $options, string $result_class) : \Generator |
|
88 | |||
89 | /** |
||
90 | * Check search item api option |
||
91 | * @param array $options |
||
92 | * @return array |
||
93 | */ |
||
94 | 28 | private function checkSearchItemOption(array $options) : array |
|
105 | |||
106 | /** |
||
107 | * Search a movie |
||
108 | * @param string $query Query string to search like a movie |
||
109 | * @param array $options Array of options for the search |
||
110 | * @return \Generator|Results\Movie |
||
111 | * @throws TmdbException |
||
112 | */ |
||
113 | 8 | View Code Duplication | public function movie(string $query, array $options = array()) : \Generator |
122 | |||
123 | /** |
||
124 | * Search a TV Show |
||
125 | * @param string $query Query string to search like a TV Show |
||
126 | * @param array $options Array of options for the request |
||
127 | * @return \Generator|Results\TVShow |
||
128 | * @throws TmdbException |
||
129 | */ |
||
130 | 7 | View Code Duplication | public function tvshow(string $query, array $options = array()) : \Generator |
139 | |||
140 | /** |
||
141 | * Search a collection |
||
142 | * @param string $query Query string to search like a collection |
||
143 | * @param array $options Array of option for the request |
||
144 | * @return \Generator|Results\Collection |
||
145 | * @throws TmdbException |
||
146 | */ |
||
147 | 4 | View Code Duplication | public function collection(string $query, array $options = array()) : \Generator |
156 | |||
157 | /** |
||
158 | * Search a people |
||
159 | * @param string $query Query string to search like a people |
||
160 | * @param array $options Array of option for the request |
||
161 | * @return \Generator|Results\People |
||
162 | * @throws TmdbException |
||
163 | */ |
||
164 | 8 | View Code Duplication | public function people(string $query, array $options = array()) : \Generator |
173 | |||
174 | /** |
||
175 | * Search a company |
||
176 | * @param string $query Query string to search like a company |
||
177 | * @param array $options Array of option for the request |
||
178 | * @return \Generator|Results\Company |
||
179 | * @throws TmdbException |
||
180 | */ |
||
181 | 6 | View Code Duplication | public function company(string $query, array $options = array()) : \Generator |
190 | } |
||
191 |
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.