Completed
Branch develop (b42baf)
by Adrien
11:23
created

CellTest   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 296
Duplicated Lines 32.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 96
loc 296
rs 8.2608
wmc 40
lcom 1
cbo 1

30 Methods

Rating   Name   Duplication   Size   Complexity  
A testColumnIndexFromString() 0 7 1
A providerColumnString() 0 4 1
A testColumnIndexFromStringTooLong() 12 13 2
A testColumnIndexFromStringTooShort() 12 13 2
A testStringFromColumnIndex() 0 7 1
A providerColumnIndex() 0 4 1
A testCoordinateFromString() 0 7 1
A providerCoordinates() 0 4 1
A testCoordinateFromStringWithRangeAddress() 12 13 2
A testCoordinateFromStringWithEmptyAddress() 12 13 2
A testCoordinateFromStringWithInvalidAddress() 12 13 2
A testAbsoluteCoordinateFromString() 0 7 1
A providerAbsoluteCoordinates() 0 4 1
A testAbsoluteCoordinateFromStringWithRangeAddress() 12 13 2
A testAbsoluteReferenceFromString() 0 7 1
A providerAbsoluteReferences() 0 4 1
A testAbsoluteReferenceFromStringWithRangeAddress() 12 13 2
A testSplitRange() 0 13 3
A providerSplitRange() 0 4 1
A testBuildRange() 0 7 1
A providerBuildRange() 0 4 1
A testBuildRangeInvalid() 12 13 2
A testRangeBoundaries() 0 7 1
A providerRangeBoundaries() 0 4 1
A testRangeDimension() 0 7 1
A providerRangeDimension() 0 4 1
A testGetRangeBoundaries() 0 7 1
A providerGetRangeBoundaries() 0 4 1
A testExtractAllCellReferencesInRange() 0 7 1
A providerExtractAllCellReferencesInRange() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
3
namespace PhpSpreadsheetTests;
4
5
use PhpSpreadsheet\Cell;
6
use PhpSpreadsheet\Exception;
7
8
class CellTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @dataProvider providerColumnString
12
     */
13
    public function testColumnIndexFromString()
14
    {
15
        $args = func_get_args();
16
        $expectedResult = array_pop($args);
17
        $result = call_user_func_array([Cell::class, 'columnIndexFromString'], $args);
18
        $this->assertEquals($expectedResult, $result);
19
    }
20
21
    public function providerColumnString()
22
    {
23
        return require 'data/ColumnString.php';
24
    }
25
26 View Code Duplication
    public function testColumnIndexFromStringTooLong()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
27
    {
28
        $cellAddress = 'ABCD';
29
        try {
30
            $result = call_user_func([Cell::class, 'columnIndexFromString'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
        } catch (\Exception $e) {
32
            $this->assertInstanceOf(Exception::class, $e);
33
            $this->assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters');
34
35
            return;
36
        }
37
        $this->fail('An expected exception has not been raised.');
38
    }
39
40 View Code Duplication
    public function testColumnIndexFromStringTooShort()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
41
    {
42
        $cellAddress = '';
43
        try {
44
            $result = call_user_func([Cell::class, 'columnIndexFromString'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
        } catch (\Exception $e) {
46
            $this->assertInstanceOf(Exception::class, $e);
47
            $this->assertEquals($e->getMessage(), 'Column string index can not be empty');
48
49
            return;
50
        }
51
        $this->fail('An expected exception has not been raised.');
52
    }
53
54
    /**
55
     * @dataProvider providerColumnIndex
56
     */
57
    public function testStringFromColumnIndex()
58
    {
59
        $args = func_get_args();
60
        $expectedResult = array_pop($args);
61
        $result = call_user_func_array([Cell::class, 'stringFromColumnIndex'], $args);
62
        $this->assertEquals($expectedResult, $result);
63
    }
64
65
    public function providerColumnIndex()
66
    {
67
        return require 'data/ColumnIndex.php';
68
    }
69
70
    /**
71
     * @dataProvider providerCoordinates
72
     */
73
    public function testCoordinateFromString()
74
    {
75
        $args = func_get_args();
76
        $expectedResult = array_pop($args);
77
        $result = call_user_func_array([Cell::class, 'coordinateFromString'], $args);
78
        $this->assertEquals($expectedResult, $result);
79
    }
80
81
    public function providerCoordinates()
82
    {
83
        return require 'data/CellCoordinates.php';
84
    }
85
86 View Code Duplication
    public function testCoordinateFromStringWithRangeAddress()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
    {
88
        $cellAddress = 'A1:AI2012';
89
        try {
90
            $result = call_user_func([Cell::class, 'coordinateFromString'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        } catch (\Exception $e) {
92
            $this->assertInstanceOf(Exception::class, $e);
93
            $this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
94
95
            return;
96
        }
97
        $this->fail('An expected exception has not been raised.');
98
    }
99
100 View Code Duplication
    public function testCoordinateFromStringWithEmptyAddress()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102
        $cellAddress = '';
103
        try {
104
            $result = call_user_func([Cell::class, 'coordinateFromString'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
        } catch (\Exception $e) {
106
            $this->assertInstanceOf(Exception::class, $e);
107
            $this->assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string');
108
109
            return;
110
        }
111
        $this->fail('An expected exception has not been raised.');
112
    }
113
114 View Code Duplication
    public function testCoordinateFromStringWithInvalidAddress()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
115
    {
116
        $cellAddress = 'AI';
117
        try {
118
            $result = call_user_func([Cell::class, 'coordinateFromString'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
        } catch (\Exception $e) {
120
            $this->assertInstanceOf(Exception::class, $e);
121
            $this->assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress);
122
123
            return;
124
        }
125
        $this->fail('An expected exception has not been raised.');
126
    }
127
128
    /**
129
     * @dataProvider providerAbsoluteCoordinates
130
     */
131
    public function testAbsoluteCoordinateFromString()
132
    {
133
        $args = func_get_args();
134
        $expectedResult = array_pop($args);
135
        $result = call_user_func_array([Cell::class, 'absoluteCoordinate'], $args);
136
        $this->assertEquals($expectedResult, $result);
137
    }
138
139
    public function providerAbsoluteCoordinates()
140
    {
141
        return require 'data/CellAbsoluteCoordinate.php';
142
    }
143
144 View Code Duplication
    public function testAbsoluteCoordinateFromStringWithRangeAddress()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
145
    {
146
        $cellAddress = 'A1:AI2012';
147
        try {
148
            $result = call_user_func([Cell::class, 'absoluteCoordinate'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
        } catch (\Exception $e) {
150
            $this->assertInstanceOf(Exception::class, $e);
151
            $this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
152
153
            return;
154
        }
155
        $this->fail('An expected exception has not been raised.');
156
    }
157
158
    /**
159
     * @dataProvider providerAbsoluteReferences
160
     */
161
    public function testAbsoluteReferenceFromString()
162
    {
163
        $args = func_get_args();
164
        $expectedResult = array_pop($args);
165
        $result = call_user_func_array([Cell::class, 'absoluteReference'], $args);
166
        $this->assertEquals($expectedResult, $result);
167
    }
168
169
    public function providerAbsoluteReferences()
170
    {
171
        return require 'data/CellAbsoluteReference.php';
172
    }
173
174 View Code Duplication
    public function testAbsoluteReferenceFromStringWithRangeAddress()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
175
    {
176
        $cellAddress = 'A1:AI2012';
177
        try {
178
            $result = call_user_func([Cell::class, 'absoluteReference'], $cellAddress);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
        } catch (\Exception $e) {
180
            $this->assertInstanceOf(Exception::class, $e);
181
            $this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
182
183
            return;
184
        }
185
        $this->fail('An expected exception has not been raised.');
186
    }
187
188
    /**
189
     * @dataProvider providerSplitRange
190
     */
191
    public function testSplitRange()
192
    {
193
        $args = func_get_args();
194
        $expectedResult = array_pop($args);
195
        $result = call_user_func_array([Cell::class, 'splitRange'], $args);
196
        foreach ($result as $key => $split) {
197
            if (!is_array($expectedResult[$key])) {
198
                $this->assertEquals($expectedResult[$key], $split[0]);
199
            } else {
200
                $this->assertEquals($expectedResult[$key], $split);
201
            }
202
        }
203
    }
204
205
    public function providerSplitRange()
206
    {
207
        return require 'data/CellSplitRange.php';
208
    }
209
210
    /**
211
     * @dataProvider providerBuildRange
212
     */
213
    public function testBuildRange()
214
    {
215
        $args = func_get_args();
216
        $expectedResult = array_pop($args);
217
        $result = call_user_func_array([Cell::class, 'buildRange'], $args);
218
        $this->assertEquals($expectedResult, $result);
219
    }
220
221
    public function providerBuildRange()
222
    {
223
        return require 'data/CellBuildRange.php';
224
    }
225
226 View Code Duplication
    public function testBuildRangeInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
227
    {
228
        $cellRange = '';
229
        try {
230
            $result = call_user_func([Cell::class, 'buildRange'], $cellRange);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
231
        } catch (\Exception $e) {
232
            $this->assertInstanceOf(Exception::class, $e);
233
            $this->assertEquals($e->getMessage(), 'Range does not contain any information');
234
235
            return;
236
        }
237
        $this->fail('An expected exception has not been raised.');
238
    }
239
240
    /**
241
     * @dataProvider providerRangeBoundaries
242
     */
243
    public function testRangeBoundaries()
244
    {
245
        $args = func_get_args();
246
        $expectedResult = array_pop($args);
247
        $result = call_user_func_array([Cell::class, 'rangeBoundaries'], $args);
248
        $this->assertEquals($expectedResult, $result);
249
    }
250
251
    public function providerRangeBoundaries()
252
    {
253
        return require 'data/CellRangeBoundaries.php';
254
    }
255
256
    /**
257
     * @dataProvider providerRangeDimension
258
     */
259
    public function testRangeDimension()
260
    {
261
        $args = func_get_args();
262
        $expectedResult = array_pop($args);
263
        $result = call_user_func_array([Cell::class, 'rangeDimension'], $args);
264
        $this->assertEquals($expectedResult, $result);
265
    }
266
267
    public function providerRangeDimension()
268
    {
269
        return require 'data/CellRangeDimension.php';
270
    }
271
272
    /**
273
     * @dataProvider providerGetRangeBoundaries
274
     */
275
    public function testGetRangeBoundaries()
276
    {
277
        $args = func_get_args();
278
        $expectedResult = array_pop($args);
279
        $result = call_user_func_array([Cell::class, 'getRangeBoundaries'], $args);
280
        $this->assertEquals($expectedResult, $result);
281
    }
282
283
    public function providerGetRangeBoundaries()
284
    {
285
        return require 'data/CellGetRangeBoundaries.php';
286
    }
287
288
    /**
289
     * @dataProvider providerExtractAllCellReferencesInRange
290
     */
291
    public function testExtractAllCellReferencesInRange()
292
    {
293
        $args = func_get_args();
294
        $expectedResult = array_pop($args);
295
        $result = call_user_func_array([Cell::class, 'extractAllCellReferencesInRange'], $args);
296
        $this->assertEquals($expectedResult, $result);
297
    }
298
299
    public function providerExtractAllCellReferencesInRange()
300
    {
301
        return require 'data/CellExtractAllCellReferencesInRange.php';
302
    }
303
}
304