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 CellTest 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 CellTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CellTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | /** |
||
11 | * @dataProvider providerColumnString |
||
12 | */ |
||
13 | public function testColumnIndexFromString() |
||
20 | |||
21 | public function providerColumnString() |
||
25 | |||
26 | View Code Duplication | public function testColumnIndexFromStringTooLong() |
|
39 | |||
40 | View Code Duplication | public function testColumnIndexFromStringTooShort() |
|
53 | |||
54 | /** |
||
55 | * @dataProvider providerColumnIndex |
||
56 | */ |
||
57 | public function testStringFromColumnIndex() |
||
64 | |||
65 | public function providerColumnIndex() |
||
69 | |||
70 | /** |
||
71 | * @dataProvider providerCoordinates |
||
72 | */ |
||
73 | public function testCoordinateFromString() |
||
80 | |||
81 | public function providerCoordinates() |
||
85 | |||
86 | View Code Duplication | public function testCoordinateFromStringWithRangeAddress() |
|
99 | |||
100 | View Code Duplication | public function testCoordinateFromStringWithEmptyAddress() |
|
113 | |||
114 | View Code Duplication | public function testCoordinateFromStringWithInvalidAddress() |
|
127 | |||
128 | /** |
||
129 | * @dataProvider providerAbsoluteCoordinates |
||
130 | */ |
||
131 | public function testAbsoluteCoordinateFromString() |
||
138 | |||
139 | public function providerAbsoluteCoordinates() |
||
143 | |||
144 | View Code Duplication | public function testAbsoluteCoordinateFromStringWithRangeAddress() |
|
157 | |||
158 | /** |
||
159 | * @dataProvider providerAbsoluteReferences |
||
160 | */ |
||
161 | public function testAbsoluteReferenceFromString() |
||
168 | |||
169 | public function providerAbsoluteReferences() |
||
173 | |||
174 | View Code Duplication | public function testAbsoluteReferenceFromStringWithRangeAddress() |
|
187 | |||
188 | /** |
||
189 | * @dataProvider providerSplitRange |
||
190 | */ |
||
191 | public function testSplitRange() |
||
204 | |||
205 | public function providerSplitRange() |
||
209 | |||
210 | /** |
||
211 | * @dataProvider providerBuildRange |
||
212 | */ |
||
213 | public function testBuildRange() |
||
220 | |||
221 | public function providerBuildRange() |
||
225 | |||
226 | View Code Duplication | public function testBuildRangeInvalid() |
|
239 | |||
240 | /** |
||
241 | * @dataProvider providerRangeBoundaries |
||
242 | */ |
||
243 | public function testRangeBoundaries() |
||
250 | |||
251 | public function providerRangeBoundaries() |
||
255 | |||
256 | /** |
||
257 | * @dataProvider providerRangeDimension |
||
258 | */ |
||
259 | public function testRangeDimension() |
||
266 | |||
267 | public function providerRangeDimension() |
||
271 | |||
272 | /** |
||
273 | * @dataProvider providerGetRangeBoundaries |
||
274 | */ |
||
275 | public function testGetRangeBoundaries() |
||
282 | |||
283 | public function providerGetRangeBoundaries() |
||
287 | |||
288 | /** |
||
289 | * @dataProvider providerExtractAllCellReferencesInRange |
||
290 | */ |
||
291 | public function testExtractAllCellReferencesInRange() |
||
298 | |||
299 | public function providerExtractAllCellReferencesInRange() |
||
303 | } |
||
304 |
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.