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 |
||
11 | class ConfigurationTest extends \PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @var Processor |
||
15 | */ |
||
16 | private $processor; |
||
17 | |||
18 | public function setUp() |
||
22 | |||
23 | private function getConfigs(array $configArray) |
||
29 | |||
30 | public function testUnconfiguredConfiguration() |
||
40 | |||
41 | public function testClientConfiguration() |
||
42 | { |
||
43 | $configuration = $this->getConfigs(array( |
||
44 | 'clients' => array( |
||
45 | 'default' => array( |
||
46 | 'url' => 'http://localhost:9200', |
||
47 | 'retryOnConflict' => 5, |
||
48 | ), |
||
49 | 'clustered' => array( |
||
50 | 'connections' => array( |
||
51 | array( |
||
52 | 'url' => 'http://es1:9200', |
||
53 | 'headers' => array( |
||
54 | 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', |
||
55 | ), |
||
56 | ), |
||
57 | array( |
||
58 | 'url' => 'http://es2:9200', |
||
59 | 'headers' => array( |
||
60 | 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', |
||
61 | ), |
||
62 | ), |
||
63 | ), |
||
64 | ), |
||
65 | ), |
||
66 | )); |
||
67 | |||
68 | $this->assertCount(2, $configuration['clients']); |
||
69 | $this->assertCount(1, $configuration['clients']['default']['connections']); |
||
70 | $this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']); |
||
71 | $this->assertEquals(5, $configuration['clients']['default']['connections'][0]['retryOnConflict']); |
||
72 | |||
73 | $this->assertCount(2, $configuration['clients']['clustered']['connections']); |
||
74 | $this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']); |
||
75 | $this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']); |
||
76 | $this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]); |
||
77 | } |
||
78 | |||
79 | public function testLogging() |
||
108 | |||
109 | View Code Duplication | public function testSlashIsAddedAtTheEndOfServerUrl() |
|
120 | |||
121 | public function testTypeConfig() |
||
122 | { |
||
123 | $this->getConfigs(array( |
||
124 | 'clients' => array( |
||
125 | 'default' => array('url' => 'http://localhost:9200'), |
||
126 | ), |
||
127 | 'indexes' => array( |
||
128 | 'test' => array( |
||
129 | 'type_prototype' => array( |
||
130 | 'analyzer' => 'custom_analyzer', |
||
131 | 'persistence' => array( |
||
132 | 'identifier' => 'ID', |
||
133 | ), |
||
134 | 'serializer' => array( |
||
135 | 'groups' => array('Search'), |
||
136 | 'version' => 1, |
||
137 | 'serialize_null' => false, |
||
138 | ), |
||
139 | ), |
||
140 | 'types' => array( |
||
141 | 'test' => array( |
||
142 | 'mappings' => array( |
||
143 | 'title' => array(), |
||
144 | 'published' => array('type' => 'datetime'), |
||
145 | 'body' => null, |
||
146 | ), |
||
147 | 'persistence' => array( |
||
148 | 'listener' => array( |
||
149 | 'logger' => true, |
||
150 | ), |
||
151 | ), |
||
152 | ), |
||
153 | 'test2' => array( |
||
154 | 'mappings' => array( |
||
155 | 'title' => null, |
||
156 | 'children' => array( |
||
157 | 'type' => 'nested', |
||
158 | ), |
||
159 | ), |
||
160 | ), |
||
161 | ), |
||
162 | ), |
||
163 | ), |
||
164 | )); |
||
165 | } |
||
166 | |||
167 | View Code Duplication | public function testClientConfigurationNoUrl() |
|
180 | |||
181 | public function testMappingsRenamedToProperties() |
||
204 | |||
205 | public function testUnconfiguredType() |
||
222 | |||
223 | public function testNestedProperties() |
||
224 | { |
||
267 | |||
268 | public function testCompressionConfig() |
||
284 | |||
285 | public function testCompressionDefaultConfig() |
||
295 | |||
296 | public function testTimeoutConfig() |
||
314 | |||
315 | public function testAWSConfig() |
||
334 | } |
||
335 |
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.