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 |
||
14 | class MyAccountTest extends WebDriverTestCase |
||
15 | { |
||
16 | View Code Duplication | public function setUp() |
|
32 | |||
33 | public function testProfileDetails() |
||
56 | |||
57 | public function testApiKeyHidden() |
||
64 | |||
65 | public function testApiKeyVisibilityCanBeToggled() |
||
66 | { |
||
67 | $this->webDriver->findElement( |
||
68 | WebDriverBy::cssSelector(".password-toggle .toggle-btn") |
||
69 | )->click(); |
||
70 | $this->assertEquals( |
||
71 | 'text', |
||
72 | $this->getApiKeyField()->getAttribute('type') |
||
73 | ); |
||
74 | |||
75 | $this->webDriver->findElement( |
||
76 | WebDriverBy::cssSelector(".password-toggle .toggle-btn") |
||
77 | )->click(); |
||
78 | $this->assertEquals( |
||
79 | 'password', |
||
80 | $this->getApiKeyField()->getAttribute('type') |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | public function testApiKeyCanBeReset() |
||
98 | |||
99 | private function getApiKeyField() |
||
105 | } |
||
106 |
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.