Str::readAligned()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace PhpBinaryReader\Type;
4
5
use PhpBinaryReader\BinaryReader;
6
use PhpBinaryReader\Exception\InvalidDataException;
7
8 View Code Duplication
class Str implements TypeInterface
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...
9
{
10
    /**
11
     * @param  \PhpBinaryReader\BinaryReader $br
12
     * @param  int                           $length
13
     * @return string
14
     * @throws \OutOfBoundsException
15
     * @throws InvalidDataException
16
     */
17 16
    public function read(BinaryReader &$br, $length)
18
    {
19 16
        if (!is_int($length)) {
20 4
            throw new InvalidDataException('The length parameter must be an integer');
21
        }
22
23 12
        if (!$br->canReadBytes($length)) {
24 4
            throw new \OutOfBoundsException('Cannot read string, it exceeds the boundary of the file');
25
        }
26
27 8
        $str = $br->readFromHandle($length);
28
29 8
        return $str;
30
    }
31
32
    /**
33
     * @param  \PhpBinaryReader\BinaryReader $br
34
     * @param  int                           $length
35
     * @return string
36
     */
37 4
    public function readAligned(BinaryReader &$br, $length)
38
    {
39 4
        $br->align();
40
41 4
        return $this->read($br, $length);
42
    }
43
}
44