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:
Complex classes like TextDataTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TextDataTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class TextDataTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | public function setUp() |
||
15 | |||
16 | /** |
||
17 | * @dataProvider providerCHAR |
||
18 | */ |
||
19 | public function testCHAR() |
||
26 | |||
27 | public function providerCHAR() |
||
31 | |||
32 | /** |
||
33 | * @dataProvider providerCODE |
||
34 | */ |
||
35 | public function testCODE() |
||
42 | |||
43 | public function providerCODE() |
||
47 | |||
48 | /** |
||
49 | * @dataProvider providerCONCATENATE |
||
50 | */ |
||
51 | public function testCONCATENATE() |
||
58 | |||
59 | public function providerCONCATENATE() |
||
63 | |||
64 | /** |
||
65 | * @dataProvider providerLEFT |
||
66 | */ |
||
67 | public function testLEFT() |
||
74 | |||
75 | public function providerLEFT() |
||
79 | |||
80 | /** |
||
81 | * @dataProvider providerMID |
||
82 | */ |
||
83 | public function testMID() |
||
90 | |||
91 | public function providerMID() |
||
95 | |||
96 | /** |
||
97 | * @dataProvider providerRIGHT |
||
98 | */ |
||
99 | public function testRIGHT() |
||
106 | |||
107 | public function providerRIGHT() |
||
111 | |||
112 | /** |
||
113 | * @dataProvider providerLOWER |
||
114 | */ |
||
115 | public function testLOWER() |
||
122 | |||
123 | public function providerLOWER() |
||
127 | |||
128 | /** |
||
129 | * @dataProvider providerUPPER |
||
130 | */ |
||
131 | public function testUPPER() |
||
138 | |||
139 | public function providerUPPER() |
||
143 | |||
144 | /** |
||
145 | * @dataProvider providerPROPER |
||
146 | */ |
||
147 | public function testPROPER() |
||
154 | |||
155 | public function providerPROPER() |
||
159 | |||
160 | /** |
||
161 | * @dataProvider providerLEN |
||
162 | */ |
||
163 | public function testLEN() |
||
170 | |||
171 | public function providerLEN() |
||
175 | |||
176 | /** |
||
177 | * @dataProvider providerSEARCH |
||
178 | */ |
||
179 | public function testSEARCH() |
||
186 | |||
187 | public function providerSEARCH() |
||
191 | |||
192 | /** |
||
193 | * @dataProvider providerFIND |
||
194 | */ |
||
195 | public function testFIND() |
||
202 | |||
203 | public function providerFIND() |
||
207 | |||
208 | /** |
||
209 | * @dataProvider providerREPLACE |
||
210 | */ |
||
211 | public function testREPLACE() |
||
218 | |||
219 | public function providerREPLACE() |
||
223 | |||
224 | /** |
||
225 | * @dataProvider providerSUBSTITUTE |
||
226 | */ |
||
227 | public function testSUBSTITUTE() |
||
234 | |||
235 | public function providerSUBSTITUTE() |
||
239 | |||
240 | /** |
||
241 | * @dataProvider providerTRIM |
||
242 | */ |
||
243 | public function testTRIM() |
||
250 | |||
251 | public function providerTRIM() |
||
255 | |||
256 | /** |
||
257 | * @dataProvider providerCLEAN |
||
258 | */ |
||
259 | public function testCLEAN() |
||
266 | |||
267 | public function providerCLEAN() |
||
271 | |||
272 | /** |
||
273 | * @dataProvider providerDOLLAR |
||
274 | */ |
||
275 | public function testDOLLAR() |
||
282 | |||
283 | public function providerDOLLAR() |
||
287 | |||
288 | /** |
||
289 | * @dataProvider providerFIXED |
||
290 | */ |
||
291 | public function testFIXED() |
||
298 | |||
299 | public function providerFIXED() |
||
303 | |||
304 | /** |
||
305 | * @dataProvider providerT |
||
306 | */ |
||
307 | public function testT() |
||
314 | |||
315 | public function providerT() |
||
319 | |||
320 | /** |
||
321 | * @dataProvider providerTEXT |
||
322 | */ |
||
323 | View Code Duplication | public function testTEXT() |
|
335 | |||
336 | public function providerTEXT() |
||
340 | |||
341 | /** |
||
342 | * @dataProvider providerVALUE |
||
343 | */ |
||
344 | View Code Duplication | public function testVALUE() |
|
355 | |||
356 | public function providerVALUE() |
||
360 | } |
||
361 |
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.