LongStringTest::testLongString()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 148
Code Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 5
eloc 100
c 3
b 1
f 0
nc 5
nop 0
dl 0
loc 148
rs 7.6888

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\Buffer;
6
use SPSS\ByteCodeReader;
7
use SPSS\Sav\Reader;
8
use SPSS\Sav\Variable;
9
use SPSS\Sav\Writer;
10
11
class LongStringTest extends TestCase
12
{
13
14
    public function testLongString()
15
    {
16
//        $reader = new ByteCodeReader(Buffer::factory(fopen('/tmp/test.bin', 'r')));
17
//        var_dump($reader->read(104));
18
//        var_dump($reader->read(152));
19
//        var_dump($reader->read(256));
20
//        var_dump($reader->read(264));
21
//        var_dump($reader->read(104));
22
//        var_dump($reader->read(152));
23
//        var_dump($reader->read(256));
24
//        var_dump($reader->read(256));
25
//        var_dump($reader->read(8));
26
//        die();
27
        $firstLong = str_repeat('1234567890', 3000);
28
        $secondLong = str_repeat('abcdefghij', 3000);
29
        $data   = [
30
            'header'    => [
31
                'prodName'     => '@(#) IBM SPSS STATISTICS',
32
                'layoutCode'   => 2,
33
                'creationDate' => '08 May 19',
34
                'creationTime' => '12:22:16',
35
            ],
36
            'variables' => [
37
                new Variable('SHORT', [
38
                    'label' => 'short label',
39
                    'width' => 100,
40
                    'format' => Variable::FORMAT_TYPE_A,
41
                    'attributes' => [
42
                        '$@Role' => Variable::ROLE_INPUT,
43
                    ],
44
                    'data' => [
45
                        '20202020202020202020',
46
                        '303030303030303030303030303030',
47
                    ]
48
                ]),
49
                new Variable('MEDIUM', [
50
                    'label' => 'medium label',
51
                    'width' => 150,
52
                    'format' => Variable::FORMAT_TYPE_A,
53
                    'attributes' => [
54
                        '$@Role' => Variable::ROLE_INPUT,
55
                    ],
56
                    'data' => [
57
                        '210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210',
58
                        '02600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260',
59
                    ]
60
                ]),
61
                new Variable('MEDIUMER', [
62
                    'name' => 'MEDIUMER',
63
                    'label' => 'mediumer label',
64
                    'width' => 250,
65
                    'format' => Variable::FORMAT_TYPE_A,
66
                    'attributes' => [
67
                        '$@Role' => Variable::ROLE_INPUT,
68
                    ],
69
                    'data' => [
70
                        '210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210',
71
                        '02600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260',
72
                    ]
73
                ]),
74
                new Variable('SHORTLONG', [
75
                    'name' => 'SHORTLONG',
76
                    'label' => 'short long label',
77
                    'width' => 265,
78
                    'format' => Variable::FORMAT_TYPE_A,
79
                    'attributes' => [
80
                        '$@Role' => Variable::ROLE_INPUT,
81
                    ],
82
                    'data' => [
83
                        '210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210210',
84
                        '0260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026002600260026012345',
85
                    ]
86
                ]),
87
                new Variable('LONGER1', [
88
                    'label'  => 'long label1',
89
                    'width'  => 30000,
90
                    'format' => Variable::FORMAT_TYPE_A,
91
                    'attributes' => [
92
                        '$@Role' => Variable::ROLE_INPUT,
93
                    ],
94
                    'data' => [
95
                        $firstLong,
96
                        $secondLong
97
                    ]
98
                ]),
99
                new Variable('LONGER2', [
100
                    'name'   => 'LONGER2',
101
                    'label'  => 'long label2',
102
                    'width'  => 30000,
103
                    'format' => Variable::FORMAT_TYPE_A,
104
                    'attributes' => [
105
                        '$@Role' => Variable::ROLE_INPUT,
106
                    ],
107
                    'data' => [
108
                        $firstLong,
109
                        $secondLong
110
                    ]
111
                ]),
112
                new Variable('short', [
113
                    'name'   => 'short',
114
                    'label'  => 'short label',
115
                    'format' => Variable::FORMAT_TYPE_F,
116
                    'width'  => 8,
117
                    'attributes' => [
118
                        '$@Role' => Variable::ROLE_INPUT,
119
                    ],
120
                    'data' => [
121
                        12135.12,
122
                        123
123
                    ],
124
                ]),
125
                new Variable('short', [
126
                    'label'  => 'short label',
127
                    'format' => Variable::FORMAT_TYPE_A,
128
                    'width'  => 8,
129
                    'attributes' => [
130
                        '$@Role' => Variable::ROLE_INPUT,
131
                    ],
132
                    'data' => [
133
                        '12345678',
134
                        'abcdefgh'
135
                    ],
136
                ]),
137
            ],
138
        ];
139
        $writer = new Writer($data);
140
        $this->assertSame(15, $writer->header->nominalCaseSize);
141
142
        // Uncomment if you want to really save and check the resulting file in SPSS
143
        $writer->save('/tmp/longString.sav');
144
145
        $buffer = $writer->getBuffer();
146
        $buffer->rewind();
147
        
148
        $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

148
        $reader = Reader::fromString(/** @scrutinizer ignore-type */ $buffer->getStream())->read();
Loading history...
149
        /**
150
         * @var  $i
151
         * @var Variable $variable
152
         */
153
        foreach ($data['variables'] as $i => $variable) {
154
            foreach($variable->data as $case => $value) {
155
                if (is_string($value)) {
156
                    $this->assertSame(substr($value, 0, $variable->getWidth()), $reader->data[$case][$i],
157
                        "Position ($case, $i)");
158
                } elseif (is_numeric($value)) {
159
                    $this->assertEqualsWithDelta($value, $reader->data[$case][$i], 0.00001);
160
                } else {
161
                    var_dump($value); die();
0 ignored issues
show
Security Debugging Code introduced by
var_dump($value) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
162
                }
163
            }
164
        }
165
166
    }
167
168
}
169