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 |
||
19 | class RoundRobinTest extends Base |
||
20 | { |
||
21 | /** |
||
22 | * @var int Number of seconds to wait before timeout is called. Is set low for tests to have fast tests. |
||
23 | */ |
||
24 | protected $_timeout = 1; |
||
25 | |||
26 | /** |
||
27 | * @group functional |
||
28 | */ |
||
29 | public function testConnection(): void |
||
30 | { |
||
31 | $config = ['connectionStrategy' => 'RoundRobin']; |
||
32 | $client = $this->_getClient($config); |
||
33 | $response = $client->request('_aliases'); |
||
34 | |||
35 | $this->_checkResponse($response); |
||
36 | |||
37 | $this->_checkStrategy($client); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @group unit |
||
42 | */ |
||
43 | public function testOldStrategySet(): void |
||
50 | |||
51 | /** |
||
52 | * @group functional |
||
53 | */ |
||
54 | View Code Duplication | public function testFailConnection(): void |
|
65 | |||
66 | /** |
||
67 | * @group functional |
||
68 | */ |
||
69 | View Code Duplication | public function testWithOneFailConnection(): void |
|
92 | |||
93 | /** |
||
94 | * @group functional |
||
95 | */ |
||
96 | View Code Duplication | public function testWithNoValidConnection(): void |
|
119 | |||
120 | protected function _checkStrategy(Client $client): void |
||
126 | |||
127 | protected function _checkResponse(Response $response): void |
||
131 | } |
||
132 |
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.