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 |
||
23 | class GuzzleAdapter implements AdapterInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var ClientInterface |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * @var Response |
||
33 | */ |
||
34 | protected $response; |
||
35 | |||
36 | /** |
||
37 | * @param string $username |
||
38 | * @param string $password |
||
39 | * @param string $accept |
||
40 | * @param ClientInterface|null $client |
||
41 | */ |
||
42 | public function __construct($username, $password, $accept, ClientInterface $client = null) |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | View Code Duplication | public function get($url) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | View Code Duplication | public function delete($url) |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | View Code Duplication | public function put($url, $content = '') |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | View Code Duplication | public function post($url, $content = '') |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | View Code Duplication | public function getLatestResponseHeaders() |
|
132 | |||
133 | /** |
||
134 | * @throws HttpException |
||
135 | */ |
||
136 | View Code Duplication | protected function handleError() |
|
143 | } |
||
144 |
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.