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 |
||
12 | class PreCommitConfigTest extends \PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | const RIGHT_MESSAGE_KEY = 'right-message'; |
||
15 | const ERROR_MESSAGE_KEY = 'error-message'; |
||
16 | const RIGHT_MESSAGE = 'ok'; |
||
17 | const ERROR_MESSAGE = 'error'; |
||
18 | /** @var PreCommitConfig */ |
||
19 | private $preCommitConfig; |
||
20 | /** @var InMemoryConfigFileReader */ |
||
21 | private $configFileReader; |
||
22 | |||
23 | protected function setUp() |
||
28 | |||
29 | /** |
||
30 | * @test |
||
31 | */ |
||
32 | View Code Duplication | public function invalidConfigData() |
|
44 | |||
45 | /** |
||
46 | * @test |
||
47 | */ |
||
48 | View Code Duplication | public function serviceNameNotExists() |
|
60 | |||
61 | /** |
||
62 | * @test |
||
63 | */ |
||
64 | View Code Duplication | public function serviceIsEnabled() |
|
76 | |||
77 | /** |
||
78 | * @test |
||
79 | */ |
||
80 | public function noServiceDataThrowsException() |
||
88 | |||
89 | /** |
||
90 | * @test |
||
91 | */ |
||
92 | public function getExtraOptions() |
||
107 | |||
108 | /** |
||
109 | * @test |
||
110 | */ |
||
111 | public function getMessagesIsNotNull() |
||
129 | } |
||
130 |
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.