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) |
||
17 | { |
||
18 | $result = Cell::columnIndexFromString(...$args); |
||
|
|||
19 | $this->assertEquals($expectedResult, $result); |
||
20 | } |
||
21 | |||
22 | public function providerColumnString() |
||
23 | { |
||
24 | return require 'data/ColumnString.php'; |
||
25 | } |
||
26 | |||
27 | View Code Duplication | public function testColumnIndexFromStringTooLong() |
|
28 | { |
||
29 | $cellAddress = 'ABCD'; |
||
30 | try { |
||
31 | Cell::columnIndexFromString($cellAddress); |
||
32 | } catch (\Exception $e) { |
||
33 | $this->assertInstanceOf(Exception::class, $e); |
||
34 | $this->assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters'); |
||
35 | |||
36 | return; |
||
37 | } |
||
38 | $this->fail('An expected exception has not been raised.'); |
||
39 | } |
||
40 | |||
41 | View Code Duplication | public function testColumnIndexFromStringTooShort() |
|
42 | { |
||
43 | $cellAddress = ''; |
||
44 | try { |
||
45 | Cell::columnIndexFromString($cellAddress); |
||
46 | } catch (\Exception $e) { |
||
47 | $this->assertInstanceOf(Exception::class, $e); |
||
48 | $this->assertEquals($e->getMessage(), 'Column string index can not be empty'); |
||
49 | |||
50 | return; |
||
51 | } |
||
52 | $this->fail('An expected exception has not been raised.'); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @dataProvider providerColumnIndex |
||
57 | * |
||
58 | * @param mixed $expectedResult |
||
59 | */ |
||
60 | public function testStringFromColumnIndex($expectedResult, ...$args) |
||
61 | { |
||
62 | $result = Cell::stringFromColumnIndex(...$args); |
||
63 | $this->assertEquals($expectedResult, $result); |
||
64 | } |
||
65 | |||
66 | public function providerColumnIndex() |
||
67 | { |
||
68 | return require 'data/ColumnIndex.php'; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @dataProvider providerCoordinates |
||
73 | * |
||
74 | * @param mixed $expectedResult |
||
75 | */ |
||
76 | public function testCoordinateFromString($expectedResult, ...$args) |
||
77 | { |
||
78 | $result = Cell::coordinateFromString(...$args); |
||
79 | $this->assertEquals($expectedResult, $result); |
||
80 | } |
||
81 | |||
82 | public function providerCoordinates() |
||
83 | { |
||
84 | return require 'data/CellCoordinates.php'; |
||
85 | } |
||
86 | |||
87 | View Code Duplication | public function testCoordinateFromStringWithRangeAddress() |
|
88 | { |
||
89 | $cellAddress = 'A1:AI2012'; |
||
90 | try { |
||
91 | Cell::coordinateFromString($cellAddress); |
||
92 | } catch (\Exception $e) { |
||
93 | $this->assertInstanceOf(Exception::class, $e); |
||
94 | $this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
||
95 | |||
96 | return; |
||
97 | } |
||
98 | $this->fail('An expected exception has not been raised.'); |
||
99 | } |
||
100 | |||
101 | View Code Duplication | public function testCoordinateFromStringWithEmptyAddress() |
|
102 | { |
||
103 | $cellAddress = ''; |
||
104 | try { |
||
105 | Cell::coordinateFromString($cellAddress); |
||
106 | } catch (\Exception $e) { |
||
107 | $this->assertInstanceOf(Exception::class, $e); |
||
108 | $this->assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string'); |
||
109 | |||
110 | return; |
||
111 | } |
||
112 | $this->fail('An expected exception has not been raised.'); |
||
113 | } |
||
114 | |||
115 | View Code Duplication | public function testCoordinateFromStringWithInvalidAddress() |
|
116 | { |
||
117 | $cellAddress = 'AI'; |
||
118 | try { |
||
119 | Cell::coordinateFromString($cellAddress); |
||
120 | } catch (\Exception $e) { |
||
121 | $this->assertInstanceOf(Exception::class, $e); |
||
122 | $this->assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress); |
||
123 | |||
124 | return; |
||
125 | } |
||
126 | $this->fail('An expected exception has not been raised.'); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @dataProvider providerAbsoluteCoordinates |
||
131 | * |
||
132 | * @param mixed $expectedResult |
||
133 | */ |
||
134 | public function testAbsoluteCoordinateFromString($expectedResult, ...$args) |
||
135 | { |
||
136 | $result = Cell::absoluteCoordinate(...$args); |
||
137 | $this->assertEquals($expectedResult, $result); |
||
138 | } |
||
139 | |||
140 | public function providerAbsoluteCoordinates() |
||
141 | { |
||
142 | return require 'data/CellAbsoluteCoordinate.php'; |
||
143 | } |
||
144 | |||
145 | View Code Duplication | public function testAbsoluteCoordinateFromStringWithRangeAddress() |
|
146 | { |
||
147 | $cellAddress = 'A1:AI2012'; |
||
148 | try { |
||
149 | Cell::absoluteCoordinate($cellAddress); |
||
150 | } catch (\Exception $e) { |
||
151 | $this->assertInstanceOf(Exception::class, $e); |
||
152 | $this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
||
153 | |||
154 | return; |
||
155 | } |
||
156 | $this->fail('An expected exception has not been raised.'); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @dataProvider providerAbsoluteReferences |
||
161 | * |
||
162 | * @param mixed $expectedResult |
||
163 | */ |
||
164 | public function testAbsoluteReferenceFromString($expectedResult, ...$args) |
||
165 | { |
||
166 | $result = Cell::absoluteReference(...$args); |
||
167 | $this->assertEquals($expectedResult, $result); |
||
168 | } |
||
169 | |||
170 | public function providerAbsoluteReferences() |
||
171 | { |
||
172 | return require 'data/CellAbsoluteReference.php'; |
||
173 | } |
||
174 | |||
175 | View Code Duplication | public function testAbsoluteReferenceFromStringWithRangeAddress() |
|
176 | { |
||
177 | $cellAddress = 'A1:AI2012'; |
||
178 | try { |
||
179 | Cell::absoluteReference($cellAddress); |
||
180 | } catch (\Exception $e) { |
||
181 | $this->assertInstanceOf(Exception::class, $e); |
||
182 | $this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
||
183 | |||
184 | return; |
||
185 | } |
||
186 | $this->fail('An expected exception has not been raised.'); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @dataProvider providerSplitRange |
||
191 | * |
||
192 | * @param mixed $expectedResult |
||
193 | */ |
||
194 | public function testSplitRange($expectedResult, ...$args) |
||
195 | { |
||
196 | $result = Cell::splitRange(...$args); |
||
197 | foreach ($result as $key => $split) { |
||
198 | if (!is_array($expectedResult[$key])) { |
||
199 | $this->assertEquals($expectedResult[$key], $split[0]); |
||
200 | } else { |
||
201 | $this->assertEquals($expectedResult[$key], $split); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | public function providerSplitRange() |
||
207 | { |
||
208 | return require 'data/CellSplitRange.php'; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @dataProvider providerBuildRange |
||
213 | * |
||
214 | * @param mixed $expectedResult |
||
215 | */ |
||
216 | public function testBuildRange($expectedResult, ...$args) |
||
217 | { |
||
218 | $result = Cell::buildRange(...$args); |
||
219 | $this->assertEquals($expectedResult, $result); |
||
220 | } |
||
221 | |||
222 | public function providerBuildRange() |
||
223 | { |
||
224 | return require 'data/CellBuildRange.php'; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @expectedException \TypeError |
||
229 | */ |
||
230 | public function testBuildRangeInvalid() |
||
231 | { |
||
232 | if (PHP_MAJOR_VERSION < 7) { |
||
233 | $this->markTestSkipped('Cannot catch type hinting error with PHP 5.6'); |
||
234 | } |
||
235 | |||
236 | $cellRange = ''; |
||
237 | Cell::buildRange($cellRange); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @dataProvider providerRangeBoundaries |
||
242 | * |
||
243 | * @param mixed $expectedResult |
||
244 | */ |
||
245 | public function testRangeBoundaries($expectedResult, ...$args) |
||
246 | { |
||
247 | $result = Cell::rangeBoundaries(...$args); |
||
248 | $this->assertEquals($expectedResult, $result); |
||
249 | } |
||
250 | |||
251 | public function providerRangeBoundaries() |
||
255 | |||
256 | /** |
||
257 | * @dataProvider providerRangeDimension |
||
258 | * |
||
259 | * @param mixed $expectedResult |
||
260 | */ |
||
261 | public function testRangeDimension($expectedResult, ...$args) |
||
262 | { |
||
263 | $result = Cell::rangeDimension(...$args); |
||
264 | $this->assertEquals($expectedResult, $result); |
||
265 | } |
||
266 | |||
267 | public function providerRangeDimension() |
||
271 | |||
272 | /** |
||
273 | * @dataProvider providerGetRangeBoundaries |
||
274 | * |
||
275 | * @param mixed $expectedResult |
||
276 | */ |
||
277 | public function testGetRangeBoundaries($expectedResult, ...$args) |
||
278 | { |
||
279 | $result = Cell::getRangeBoundaries(...$args); |
||
280 | $this->assertEquals($expectedResult, $result); |
||
281 | } |
||
282 | |||
283 | public function providerGetRangeBoundaries() |
||
287 | |||
288 | /** |
||
289 | * @dataProvider providerExtractAllCellReferencesInRange |
||
290 | * |
||
291 | * @param mixed $expectedResult |
||
292 | */ |
||
293 | public function testExtractAllCellReferencesInRange($expectedResult, ...$args) |
||
294 | { |
||
295 | $result = Cell::extractAllCellReferencesInRange(...$args); |
||
296 | $this->assertEquals($expectedResult, $result); |
||
297 | } |
||
298 | |||
299 | public function providerExtractAllCellReferencesInRange() |
||
303 | |||
304 | /** |
||
305 | * @dataProvider providerMergeRangesInCollection |
||
306 | * |
||
307 | * @param mixed $expectedResult |
||
308 | */ |
||
309 | public function testMergeRangesInCollection($expectedResult, ...$args) |
||
310 | { |
||
311 | $result = Cell::mergeRangesInCollection(...$args); |
||
312 | $this->assertEquals($expectedResult, $result); |
||
313 | } |
||
314 | |||
315 | public function providerMergeRangesInCollection() |
||
316 | { |
||
317 | return require 'data/CellMergeRangesInCollection.php'; |
||
318 | } |
||
319 | } |
||
320 |
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: