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 |
||
12 | class TrelloApiClient implements HttpClientInterface |
||
13 | { |
||
14 | const GET_ALL_CARDS_URL = 'https://api.trello.com/1/boards/{boardId}/cards/?key={apiKey}&token={token}'; |
||
15 | |||
16 | const GET_CREATION_CARD_INFO_URL = 'https://api.trello.com/1/cards/{cardId}/actions?filter=createCard&key={apiKey}&token={token}'; |
||
17 | |||
18 | const GET_HISTORY_CARD_URL = 'https://api.trello.com/1/cards/{cardId}/actions?filter=updateCard:idList&key={apiKey}&token={token}'; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $apiKey; |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $token; |
||
28 | |||
29 | 3 | public function __construct(string $apiKey, string $token) |
|
34 | |||
35 | 1 | public function findAllCards(string $boardId): array |
|
41 | |||
42 | 1 | public function findCreationCard(string $cardId): array |
|
48 | |||
49 | 1 | public function findAllCardHistory(string $cardId): array |
|
55 | |||
56 | public function createRequest(string $url) |
||
67 | |||
68 | 2 | View Code Duplication | protected function urlBuilderWithCardId($url, $cardId = null) |
75 | |||
76 | 1 | View Code Duplication | protected function urlBuilderWithBoardId($url, $boardId = null) |
83 | } |
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.