Completed
Push — master ( 4a5c62...dba287 )
by Daniel
03:51
created

Integer32Reader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 3
dl 48
loc 48
ccs 0
cts 22
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readUnsigned() 16 16 3
A readSigned() 13 13 3

How to fix   Duplicated Code   

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:

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\Reader;
9
10
use GravityMedia\Stream\Enum\ByteOrder;
11
use GravityMedia\Stream\Exception;
12
13
/**
14
 * 32-bit integer (long) reader
15
 *
16
 * @package GravityMedia\Stream\Reader
17
 */
18 View Code Duplication
class Integer32Reader extends AbstractIntegerReader
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * Use byte order aware trait
22
     */
23
    use ByteOrderAwareTrait;
24
25
    /**
26
     * Read unsigned 32-bit integer (long) data from the stream
27
     *
28
     * @return int
29
     */
30
    protected function readUnsigned()
31
    {
32
        switch ($this->getByteOrder()) {
33
            case ByteOrder::BIG_ENDIAN:
34
                $format = 'N*';
35
                break;
36
            case ByteOrder::LITTLE_ENDIAN:
37
                $format = 'V*';
38
                break;
39
            default:
40
                $format = 'L*';
41
        }
42
43
        list(, $value) = unpack($format, $this->getStream()->read(4));
44
        return $value;
45
    }
46
47
    /**
48
     * Read signed 32-bit integer (long) data from the stream
49
     *
50
     * @return int
51
     */
52
    protected function readSigned()
53
    {
54
        $data = $this->stream->read(4);
55
56
        if (ByteOrder::MACHINE_ENDIAN !== $this->getByteOrder()
57
            && $this->getMachineByteOrder() !== $this->getByteOrder()
58
        ) {
59
            $data = strrev($data);
60
        }
61
62
        list(, $value) = unpack('l*', $data);
63
        return $value;
64
    }
65
}
66