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 |
||
22 | class Client |
||
23 | { |
||
24 | const BASE_URI = 'https://api.telegra.ph/'; |
||
25 | |||
26 | /** |
||
27 | * @var \GuzzleHttp\Client |
||
28 | */ |
||
29 | private $httpClient; |
||
30 | |||
31 | /** |
||
32 | * Client constructor. |
||
33 | */ |
||
34 | public function __construct() |
||
38 | |||
39 | /** |
||
40 | * @param string $shortName |
||
41 | * @param string $authorName |
||
42 | * @param string $authorUrl |
||
43 | * @return Account |
||
44 | */ |
||
45 | View Code Duplication | public function createAccount(string $shortName, string $authorName = '', string $authorUrl = ''): Account |
|
57 | |||
58 | /** |
||
59 | * @param string $accessToken |
||
60 | * @param string $shortName |
||
61 | * @param string $authorName |
||
62 | * @param string $authorUrl |
||
63 | * @return Account |
||
64 | */ |
||
65 | View Code Duplication | public function editAccountInfo(string $accessToken, string $shortName, string $authorName = '', string $authorUrl = ''): Account |
|
78 | |||
79 | /** |
||
80 | * @param string $accessToken |
||
81 | * @param array $fields |
||
82 | * @return Account |
||
83 | */ |
||
84 | View Code Duplication | public function getAccountInfo(string $accessToken, array $fields = ['short_name', 'author_name', 'author_url']): Account |
|
95 | |||
96 | /** |
||
97 | * @param string $accessToken |
||
98 | * @return Account |
||
99 | */ |
||
100 | public function revokeAccessToken(string $accessToken): Account |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param string $accessToken |
||
114 | * @param string $title |
||
115 | * @param string|NodeElement[] $content |
||
116 | * @param string $authorName |
||
117 | * @param string $authorUrl |
||
118 | * @param bool $returnContent |
||
119 | * @return Page |
||
120 | */ |
||
121 | public function createPage(string $accessToken, string $title, $content, string $authorName = '', string $authorUrl = '', bool $returnContent = false): Page |
||
136 | |||
137 | public function editPage(string $accessToken, string $path, string $title, $content, string $authorName = null, string $authorUrl = null, bool $returnContent = false) |
||
152 | |||
153 | public function getPage(string $path, bool $returnContent = false) |
||
163 | |||
164 | public function getPageList(string $accessToken, int $offset = 0, int $limit = 50) |
||
176 | |||
177 | public function getViews(string $path, int $year = null, int $month = null, int $day = null, int $hour = null) |
||
191 | |||
192 | /** |
||
193 | * @param ResponseInterface $response |
||
194 | * @return array |
||
195 | * @throws RequestException |
||
196 | */ |
||
197 | private static function getDecodedResponseResult(ResponseInterface $response): array |
||
207 | |||
208 | /** |
||
209 | * @param ResponseInterface $response |
||
210 | * @return mixed |
||
211 | */ |
||
212 | private static function decodeResponse(ResponseInterface $response) |
||
216 | |||
217 | /** |
||
218 | * @param NodeElement[]|string $content |
||
219 | * @return array |
||
220 | * @throws InvalidContentTypeException |
||
221 | */ |
||
222 | private static function decoratePageContent($content): array |
||
240 | } |
||
241 |
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.