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 |
||
13 | class KProxy implements GoogleProxy |
||
14 | { |
||
15 | /** @var string $endpoint */ |
||
16 | protected $endpoint; |
||
17 | /** @var int $serverNumber */ |
||
18 | protected $serverNumber; |
||
19 | |||
20 | /** |
||
21 | * Constructor that initializes the proxy service in one of its servers, which go from 1 to 9 |
||
22 | * |
||
23 | * @param int $serverNumber |
||
24 | */ |
||
25 | 13 | public function __construct(int $serverNumber) |
|
33 | |||
34 | /** {@inheritdoc} */ |
||
35 | 9 | public function getHttpResponse(string $url): ResponseInterface |
|
47 | |||
48 | /** |
||
49 | * Accesses the main page of the kproxy.com server. This is mandatory. |
||
50 | * |
||
51 | * @param Client $httpClient |
||
52 | */ |
||
53 | 9 | private function accessMainPage(Client $httpClient): void |
|
57 | |||
58 | /** {@inheritdoc} */ |
||
59 | 10 | View Code Duplication | public function parseUrl(string $url): string |
72 | |||
73 | /** |
||
74 | * Sends the request to the proxy service that saves the info in session. After this we can redirect |
||
75 | * the user to the search results |
||
76 | * |
||
77 | * @param Client $httpClient |
||
78 | * @param string $url |
||
79 | */ |
||
80 | 9 | private function sendRequestToProxy(Client $httpClient, string $url): void |
|
93 | } |
||
94 |
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.