Completed
Push — master ( dd1269...21c3dd )
by Sam
02:11
created

LongStringTest::testLongString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 70
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 47
c 3
b 1
f 0
nc 1
nop 0
dl 0
loc 70
rs 9.1563

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SPSS\Tests;
4
5
use SPSS\Sav\Reader;
6
use SPSS\Sav\Variable;
7
use SPSS\Sav\Writer;
8
9
class LongStringTest extends TestCase
10
{
11
12
    public function testLongString()
13
    {
14
        $firstLong = str_repeat('1234567890', 300);
15
        $secondLong = str_repeat('abcdefghij', 300);
16
        $data   = [
17
            'header'    => [
18
                'prodName'     => '@(#) IBM SPSS STATISTICS',
19
                'layoutCode'   => 2,
20
                'creationDate' => '08 May 19',
21
                'creationTime' => '12:22:16',
22
            ],
23
            'variables' => [
24
                [
25
                    'name'   => 'LONGER1',
26
                    'label'  => 'long label1',
27
                    'width'  => 3000,
28
                    'format' => Variable::FORMAT_TYPE_A,
29
                    'attributes' => [
30
                        '$@Role' => Variable::ROLE_INPUT,
31
                    ],
32
                    'data' => [
33
                        $firstLong,
34
                        $secondLong
35
                    ]
36
                ],
37
                [
38
                    'name'   => 'LONGER2',
39
                    'label'  => 'long label2',
40
                    'width'  => 3000,
41
                    'format' => Variable::FORMAT_TYPE_A,
42
                    'attributes' => [
43
                        '$@Role' => Variable::ROLE_INPUT,
44
                    ],
45
                    'data' => [
46
                        $firstLong,
47
                        $secondLong
48
                    ]
49
                ],
50
                [
51
                    'name'   => 'short',
52
                    'label'  => 'short label',
53
                    'format' => Variable::FORMAT_TYPE_A,
54
                    'width'  => 8,
55
                    'attributes' => [
56
                        '$@Role' => Variable::ROLE_INPUT,
57
                    ],
58
                    'data' => [
59
                        '12345678',
60
                        'abcdefgh'
61
                    ],
62
                ],
63
            ],
64
        ];
65
        $writer = new Writer($data);
66
67
        // Uncomment if you want to really save and check the resulting file in SPSS
68
        //$writer->save('longString.sav');
69
70
        $buffer = $writer->getBuffer();
71
        $buffer->rewind();
72
        
73
        $reader = Reader::fromString($buffer->getStream())->read();
0 ignored issues
show
Bug introduced by
$buffer->getStream() of type resource is incompatible with the type string expected by parameter $str of SPSS\Sav\Reader::fromString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $reader = Reader::fromString(/** @scrutinizer ignore-type */ $buffer->getStream())->read();
Loading history...
74
        
75
        $expected[0][0] = $data['variables'][0]['data'][0];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$expected was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expected = array(); before regardless.
Loading history...
76
        $expected[0][1] = $data['variables'][1]['data'][0];
77
        $expected[0][2] = $data['variables'][2]['data'][0];
78
        $expected[1][0] = $data['variables'][0]['data'][1];
79
        $expected[1][1] = $data['variables'][1]['data'][1];
80
        $expected[1][2] = $data['variables'][2]['data'][1];
81
        $this->assertEquals($expected, $reader->data);
82
    }
83
84
}
85