Completed
Push — master ( 88acd9...44b0be )
by Daniel
02:07
created

UnsignedShortReader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 17.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 2
dl 13
loc 75
ccs 0
cts 19
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 3
A getStream() 0 4 1
A getEndian() 0 4 1
A read() 0 11 2

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\Endian;
11
use GravityMedia\Stream\Exception;
12
use GravityMedia\Stream\StreamInterface;
13
14
/**
15
 * Unsigned short (16-bit integer) reader
16
 *
17
 * @package GravityMedia\Stream\Reader
18
 */
19
class UnsignedShortReader
20
{
21
    /**
22
     * @var StreamInterface
23
     */
24
    protected $stream;
25
26
    /**
27
     * @var int
28
     */
29
    protected $endian;
30
31
    /**
32
     * Create unsigned short (16-bit integer) reader object
33
     *
34
     * @throws Exception\InvalidArgumentException   An exception will be thrown for non-readable streams or an invalid
35
     *                                              endian value
36
     *
37
     * @param StreamInterface $stream
38
     * @param int $endian
39
     */
40 View Code Duplication
    public function __construct(StreamInterface $stream, $endian)
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
        if (!$stream->isReadable()) {
43
            throw new Exception\InvalidArgumentException('Stream not readable');
44
        }
45
46
        if (!in_array($endian, [Endian::ENDIAN_BIG, Endian::ENDIAN_LITTLE])) {
47
            throw new Exception\InvalidArgumentException('Invalid endian');
48
        }
49
50
        $this->stream = $stream;
51
        $this->endian = $endian;
52
    }
53
54
    /**
55
     * Get stream
56
     *
57
     * @return StreamInterface
58
     */
59
    public function getStream()
60
    {
61
        return $this->stream;
62
    }
63
64
    /**
65
     * Get endian
66
     *
67
     * @return int
68
     */
69
    public function getEndian()
70
    {
71
        return $this->endian;
72
    }
73
74
    /**
75
     * Read string data from the stream
76
     *
77
     * @throws Exception\IOException    An exception will be thrown for invalid stream resources or when the data could
78
     *                                  not be read
79
     *
80
     * @return int
81
     */
82
    public function read()
83
    {
84
        $format = 'n';
85
        if (Endian::ENDIAN_LITTLE === $this->endian) {
86
            $format = 'v';
87
        }
88
89
        $data = unpack($format, $this->stream->read(2));
90
91
        return $data[1];
92
    }
93
}
94