Passed
Push — master ( 7a8e6a...cf836d )
by William
02:48
created

StringReader::readint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 5
c 3
b 0
f 0
dl 0
loc 16
ccs 5
cts 6
cp 0.8333
rs 10
cc 3
nc 3
nop 2
crap 3.0416
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
    Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <[email protected]>.
7
    Copyright (c) 2016 Michal Čihař <[email protected]>
8
9
    This file is part of MoTranslator.
10
11
    This program is free software; you can redistribute it and/or modify
12
    it under the terms of the GNU General Public License as published by
13
    the Free Software Foundation; either version 2 of the License, or
14
    (at your option) any later version.
15
16
    This program is distributed in the hope that it will be useful,
17
    but WITHOUT ANY WARRANTY; without even the implied warranty of
18
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
    GNU General Public License for more details.
20
21
    You should have received a copy of the GNU General Public License along
22
    with this program; if not, write to the Free Software Foundation, Inc.,
23
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
*/
25
26
namespace PhpMyAdmin\MoTranslator;
27
28
use const PHP_INT_MAX;
29
use function file_get_contents;
30
use function strlen;
31
use function substr;
32
use function unpack;
33
34
/**
35
 * Simple wrapper around string buffer for
36
 * random access and values parsing.
37
 */
38
class StringReader
39
{
40
    /** @var string */
41
    private $string;
42
    /** @var int */
43
    private $length;
44
45
    /**
46
     * @param string $filename Name of file to load
47
     */
48 156
    public function __construct(string $filename)
49
    {
50 156
        $this->string = (string) file_get_contents($filename);
51 156
        $this->length = strlen($this->string);
52 156
    }
53
54
    /**
55
     * Read number of bytes from given offset.
56
     *
57
     * @param int $pos   Offset
58
     * @param int $bytes Number of bytes to read
59
     */
60 156
    public function read(int $pos, int $bytes): string
61
    {
62 156
        if ($pos + $bytes > $this->length) {
63 20
            throw new ReaderException('Not enough bytes!');
64
        }
65
66 152
        return substr($this->string, $pos, $bytes);
67
    }
68
69
    /**
70
     * Reads a 32bit integer from the stream.
71
     *
72
     * @param string $unpack Unpack string
73
     * @param int    $pos    Position
74
     *
75
     * @return int Integer from the stream
76
     */
77 148
    public function readint(string $unpack, int $pos): int
78
    {
79 148
        $data = unpack($unpack, $this->read($pos, 4));
80 148
        if ($data === false) {
81
            return PHP_INT_MAX;
82
        }
83
84 148
        $result = $data[1];
85
86
        /* We're reading unsigned int, but PHP will happily
87
         * give us negative number on 32-bit platforms.
88
         *
89
         * See also documentation:
90
         * https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes
91
         */
92 148
        return $result < 0 ? PHP_INT_MAX : $result;
93
    }
94
95
    /**
96
     * Reads an array of integers from the stream.
97
     *
98
     * @param string $unpack Unpack string
99
     * @param int    $pos    Position
100
     * @param int    $count  How many elements should be read
101
     *
102
     * @return int[] Array of Integers
103
     */
104 136
    public function readintarray(string $unpack, int $pos, int $count): array
105
    {
106 136
        $data = unpack($unpack . $count, $this->read($pos, 4 * $count));
107 132
        if ($data === false) {
108
            return [];
109
        }
110
111 132
        return $data;
112
    }
113
}
114