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 |
||
| 25 | class ClashOfClansApi |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var WebClient to perform requests to the Clash Of Clans API |
||
| 29 | */ |
||
| 30 | protected $webClient; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string $apikey Every call to the Clash Of Clans API needs to contain an Api Key |
||
| 34 | */ |
||
| 35 | public function __construct(string $apiKey) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string $tag The player's tag (with the hasttag) |
||
| 45 | * @return Player |
||
| 46 | */ |
||
| 47 | public function getPlayerByTag(string $tag) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The method return all existing league from the game |
||
| 56 | * |
||
| 57 | * @param SearchFilter $filter |
||
| 58 | * @return array of League objects |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function getLeagues(SearchFilter $filter = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get every information about the given league |
||
| 74 | * |
||
| 75 | * @var int $leagueId id of the League |
||
| 76 | * @return League object |
||
| 77 | */ |
||
| 78 | public function getLeagueById(int $leagueId) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get a league seasons. Note that league season information is available only for Legend League |
||
| 87 | * |
||
| 88 | * @var int $leagueId id of the League |
||
| 89 | * @param SearchFilter $filter |
||
| 90 | * @return array of Season objects |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function getLeagueSeasons(int $leagueId, SearchFilter $filter = null) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get a league season rankings. Note that league season information is available only for Legend League (ID: 29000022) |
||
| 106 | * |
||
| 107 | * @param int $leagueId |
||
| 108 | * @param SeasonPeriod $seasonId |
||
| 109 | * @param SearchFilter $filter |
||
| 110 | * @return array of SeasonPlayer objects |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function getLeagueSeason(int $leagueId, SeasonPeriod $seasonId, SearchFilter $filter = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * List all available locations |
||
| 128 | * |
||
| 129 | * @param SearchFilter $filter |
||
| 130 | * @return array of Location objects |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function getLocations(SearchFilter $filter = null) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Get information about specific location |
||
| 148 | * |
||
| 149 | * @param int $locationId |
||
| 150 | * @return Location |
||
| 151 | */ |
||
| 152 | public function getLocationById(int $locationId) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get clan rankings for a specific location |
||
| 163 | * |
||
| 164 | * @param int $locationId |
||
| 165 | * @param SearchFilter $filter |
||
| 166 | * @return array of LocationClan objects |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function getLocationClanRankings(int $locationId, SearchFilter $filter = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get player rankings for a specific location |
||
| 184 | * |
||
| 185 | * @param int $locationId |
||
| 186 | * @param SearchFilter $filter |
||
| 187 | * @return array of LocationPlayer objects |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function getLocationPlayerRankings(int $locationId, SearchFilter $filter = null) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Get clan versus rankings for a specific location |
||
| 205 | * |
||
| 206 | * @param int $locationId |
||
| 207 | * @param SearchFilter $filter |
||
| 208 | * @return array of LocationVersusClan objects |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function getLocationClanVersusRankings(int $locationId, SearchFilter $filter = null) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get player rankings for a specific location |
||
| 226 | * |
||
| 227 | * @param int $locationId |
||
| 228 | * @param SearchFilter $filter |
||
| 229 | * @return array of LocationVersusPlayer objects |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function getLocationPlayerVersusRankings(int $locationId, SearchFilter $filter = null) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Search all clans by name and/or filtering the results using various criteria |
||
| 247 | * |
||
| 248 | * @param ClanSearchFilter $filter |
||
| 249 | * @return array of SearchClan objects |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function getClans(ClanSearchFilter $filter = null) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get information about a single clan by clan tag |
||
| 267 | * |
||
| 268 | * @param string $clanTag |
||
| 269 | * @return DetailedClan |
||
| 270 | */ |
||
| 271 | public function getClanyByTag(string $clanTag) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get information about a single clan by clan tag |
||
| 282 | * |
||
| 283 | * @param string $clanTag |
||
| 284 | * @param SearchFilter $filter |
||
| 285 | * @return array of DetailedClanPlayer objects |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function getClanMembers(string $clanTag, SearchFilter $filter = null) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Get information about a single clan by clan tag |
||
| 303 | * |
||
| 304 | * @param string $clanTag |
||
| 305 | * @param SearchFilter $filter |
||
| 306 | * @return array of DetailedClanPlayer objects |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function getClanWarlog(string $clanTag, SearchFilter $filter = null) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get information about a single clan by clan tag |
||
| 324 | * |
||
| 325 | * @param string $clanTag |
||
| 326 | * @return array of DetailedClanPlayer objects |
||
| 327 | */ |
||
| 328 | public function getClanCurrentWar(string $clanTag) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param SearchFilter or ClanSearchFilter $filter |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function filterAppendToUrl($filter = null) |
||
| 366 | } |
||
| 367 | } |
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.