Completed
Push — master ( 8d738d...8ee41c )
by Daniel
14:24
created

IntegerWriter::writeSignedInteger32()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the stream package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Stream\Writer;
9
10
use GravityMedia\Stream\ByteOrder\ByteOrder;
11
use GravityMedia\Stream\ByteOrder\ByteOrderAwareTrait;
12
13
/**
14
 * Integer writer
15
 *
16
 * @package GravityMedia\Stream\Writer
17
 */
18
class IntegerWriter extends Writer
19
{
20
    /**
21
     * Use byte order aware trait
22
     */
23
    use ByteOrderAwareTrait;
24
25
    /**
26
     * Write unsigned 8-bit integer (char) data to the stream
27
     *
28
     * @param string $data The data
29
     *
30
     * @return int
31
     */
32
    public function writeUnsignedInteger8($data)
33
    {
34
        return $this->write(pack('C', $data));
35
    }
36
37
    /**
38
     * Write signed 8-bit integer (char) data to the stream
39
     *
40
     * @param string $data The data
41
     *
42
     * @return int
43
     */
44
    public function writeSignedInteger8($data)
45
    {
46
        return $this->write(pack('c', $data));
47
    }
48
49
    /**
50
     * Write unsigned 16-bit integer (short) data to the stream
51
     *
52
     * @param string $data The data
53
     *
54
     * @return int
55
     */
56 View Code Duplication
    public function writeUnsignedInteger16($data)
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...
57
    {
58
        switch ($this->getByteOrder()) {
59
            case ByteOrder::BIG_ENDIAN:
60
                $format = 'n';
61
                break;
62
            case ByteOrder::LITTLE_ENDIAN:
63
                $format = 'v';
64
                break;
65
            default:
66
                $format = 'S';
67
        }
68
69
        return $this->write(pack($format, $data));
70
    }
71
72
    /**
73
     * Write signed 16-bit integer (short) data to the stream
74
     *
75
     * @param string $data The data
76
     *
77
     * @return int
78
     */
79 View Code Duplication
    public function writeSignedInteger16($data)
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...
80
    {
81
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
82
            && $this->getByteOrder() !== $this->getMachineByteOrder()
83
        ) {
84
            $data = strrev($data);
85
        }
86
87
        return $this->write(pack('s', $data));
88
    }
89
90
    /**
91
     * Write unsigned 32-bit integer (long) data to the stream
92
     *
93
     * @param string $data The data
94
     *
95
     * @return int
96
     */
97 View Code Duplication
    public function writeUnsignedInteger32($data)
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...
98
    {
99
        switch ($this->getByteOrder()) {
100
            case ByteOrder::BIG_ENDIAN:
101
                $format = 'N';
102
                break;
103
            case ByteOrder::LITTLE_ENDIAN:
104
                $format = 'V';
105
                break;
106
            default:
107
                $format = 'L';
108
        }
109
110
        return $this->write(pack($format, $data));
111
    }
112
113
    /**
114
     * Write signed 32-bit integer (long) data to the stream
115
     *
116
     * @param string $data The data
117
     *
118
     * @return int
119
     */
120 View Code Duplication
    public function writeSignedInteger32($data)
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...
121
    {
122
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
123
            && $this->getByteOrder() !== $this->getMachineByteOrder()
124
        ) {
125
            $data = strrev($data);
126
        }
127
128
        return $this->write(pack('l', $data));
129
    }
130
131
    /**
132
     * Write unsigned 64-bit integer (long long) data to the stream
133
     *
134
     * @param string $data The data
135
     *
136
     * @return int
137
     */
138 View Code Duplication
    public function writeUnsignedInteger64($data)
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...
139
    {
140
        switch ($this->getByteOrder()) {
141
            case ByteOrder::BIG_ENDIAN:
142
                $format = 'J';
143
                break;
144
            case ByteOrder::LITTLE_ENDIAN:
145
                $format = 'P';
146
                break;
147
            default:
148
                $format = 'Q';
149
        }
150
151
        return $this->write(pack($format, $data));
152
    }
153
154
    /**
155
     * Write signed 64-bit integer (long long) data to the stream
156
     *
157
     * @param string $data The data
158
     *
159
     * @return int
160
     */
161 View Code Duplication
    public function writeSignedInteger64($data)
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...
162
    {
163
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
164
            && $this->getByteOrder() !== $this->getMachineByteOrder()
165
        ) {
166
            $data = strrev($data);
167
        }
168
169
        return $this->write(pack('q', $data));
170
    }
171
}
172