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 |
||
9 | class CellTest extends PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @dataProvider providerColumnString |
||
13 | * |
||
14 | * @param mixed $expectedResult |
||
15 | */ |
||
16 | public function testColumnIndexFromString($expectedResult, ...$args) |
||
21 | |||
22 | public function providerColumnString() |
||
26 | |||
27 | View Code Duplication | public function testColumnIndexFromStringTooLong() |
|
41 | |||
42 | View Code Duplication | public function testColumnIndexFromStringTooShort() |
|
56 | |||
57 | /** |
||
58 | * @dataProvider providerColumnIndex |
||
59 | * |
||
60 | * @param mixed $expectedResult |
||
61 | */ |
||
62 | public function testStringFromColumnIndex($expectedResult, ...$args) |
||
67 | |||
68 | public function providerColumnIndex() |
||
72 | |||
73 | /** |
||
74 | * @dataProvider providerCoordinates |
||
75 | * |
||
76 | * @param mixed $expectedResult |
||
77 | */ |
||
78 | public function testCoordinateFromString($expectedResult, ...$args) |
||
83 | |||
84 | public function providerCoordinates() |
||
88 | |||
89 | View Code Duplication | public function testCoordinateFromStringWithRangeAddress() |
|
103 | |||
104 | View Code Duplication | public function testCoordinateFromStringWithEmptyAddress() |
|
118 | |||
119 | View Code Duplication | public function testCoordinateFromStringWithInvalidAddress() |
|
133 | |||
134 | /** |
||
135 | * @dataProvider providerAbsoluteCoordinates |
||
136 | * |
||
137 | * @param mixed $expectedResult |
||
138 | */ |
||
139 | public function testAbsoluteCoordinateFromString($expectedResult, ...$args) |
||
144 | |||
145 | public function providerAbsoluteCoordinates() |
||
149 | |||
150 | View Code Duplication | public function testAbsoluteCoordinateFromStringWithRangeAddress() |
|
164 | |||
165 | /** |
||
166 | * @dataProvider providerAbsoluteReferences |
||
167 | * |
||
168 | * @param mixed $expectedResult |
||
169 | */ |
||
170 | public function testAbsoluteReferenceFromString($expectedResult, ...$args) |
||
175 | |||
176 | public function providerAbsoluteReferences() |
||
180 | |||
181 | View Code Duplication | public function testAbsoluteReferenceFromStringWithRangeAddress() |
|
195 | |||
196 | /** |
||
197 | * @dataProvider providerSplitRange |
||
198 | * |
||
199 | * @param mixed $expectedResult |
||
200 | */ |
||
201 | public function testSplitRange($expectedResult, ...$args) |
||
212 | |||
213 | public function providerSplitRange() |
||
217 | |||
218 | /** |
||
219 | * @dataProvider providerBuildRange |
||
220 | * |
||
221 | * @param mixed $expectedResult |
||
222 | */ |
||
223 | public function testBuildRange($expectedResult, ...$args) |
||
228 | |||
229 | public function providerBuildRange() |
||
233 | |||
234 | /** |
||
235 | * @expectedException \TypeError |
||
236 | */ |
||
237 | public function testBuildRangeInvalid() |
||
246 | |||
247 | /** |
||
248 | * @dataProvider providerRangeBoundaries |
||
249 | * |
||
250 | * @param mixed $expectedResult |
||
251 | */ |
||
252 | public function testRangeBoundaries($expectedResult, ...$args) |
||
257 | |||
258 | public function providerRangeBoundaries() |
||
262 | |||
263 | /** |
||
264 | * @dataProvider providerRangeDimension |
||
265 | * |
||
266 | * @param mixed $expectedResult |
||
267 | */ |
||
268 | public function testRangeDimension($expectedResult, ...$args) |
||
273 | |||
274 | public function providerRangeDimension() |
||
278 | |||
279 | /** |
||
280 | * @dataProvider providerGetRangeBoundaries |
||
281 | * |
||
282 | * @param mixed $expectedResult |
||
283 | */ |
||
284 | public function testGetRangeBoundaries($expectedResult, ...$args) |
||
289 | |||
290 | public function providerGetRangeBoundaries() |
||
294 | |||
295 | /** |
||
296 | * @dataProvider providerExtractAllCellReferencesInRange |
||
297 | * |
||
298 | * @param mixed $expectedResult |
||
299 | */ |
||
300 | public function testExtractAllCellReferencesInRange($expectedResult, ...$args) |
||
305 | |||
306 | public function providerExtractAllCellReferencesInRange() |
||
310 | |||
311 | /** |
||
312 | * @dataProvider providerMergeRangesInCollection |
||
313 | * |
||
314 | * @param mixed $expectedResult |
||
315 | */ |
||
316 | public function testMergeRangesInCollection($expectedResult, ...$args) |
||
321 | |||
322 | public function providerMergeRangesInCollection() |
||
326 | } |
||
327 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: