Passed
Push — master ( e789fa...7285b3 )
by William
02:37
created

StringReader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 17
c 3
b 0
f 0
dl 0
loc 76
rs 10
ccs 18
cts 20
cp 0.9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A read() 0 9 3
A readint() 0 16 3
A readintarray() 0 8 2
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 function file_get_contents;
29
use function strlen;
30
use function substr;
31
use function unpack;
32
33
use const PHP_INT_MAX;
34
35
/**
36
 * Simple wrapper around string buffer for
37
 * random access and values parsing.
38
 */
39
class StringReader
40
{
41
    /** @var string */
42
    private $string;
43
    /** @var int */
44
    private $length;
45
46
    /**
47
     * @param string $filename Name of file to load
48
     */
49 168
    public function __construct(string $filename)
50
    {
51 168
        $this->string = (string) file_get_contents($filename);
52 168
        $this->length = strlen($this->string);
53 42
    }
54
55
    /**
56
     * Read number of bytes from given offset.
57
     *
58
     * @param int $pos   Offset
59
     * @param int $bytes Number of bytes to read
60
     */
61 168
    public function read(int $pos, int $bytes): string
62
    {
63 168
        if ($pos + $bytes > $this->length) {
64 20
            throw new ReaderException('Not enough bytes!');
65
        }
66
67 164
        $data = substr($this->string, $pos, $bytes);
68
69 164
        return $data === false ? '' : $data;
70
    }
71
72
    /**
73
     * Reads a 32bit integer from the stream.
74
     *
75
     * @param string $unpack Unpack string
76
     * @param int    $pos    Position
77
     *
78
     * @return int Integer from the stream
79
     */
80 152
    public function readint(string $unpack, int $pos): int
81
    {
82 152
        $data = unpack($unpack, $this->read($pos, 4));
83 152
        if ($data === false) {
84
            return PHP_INT_MAX;
85
        }
86
87 152
        $result = $data[1];
88
89
        /* We're reading unsigned int, but PHP will happily
90
         * give us negative number on 32-bit platforms.
91
         *
92
         * See also documentation:
93
         * https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes
94
         */
95 152
        return $result < 0 ? PHP_INT_MAX : $result;
96
    }
97
98
    /**
99
     * Reads an array of integers from the stream.
100
     *
101
     * @param string $unpack Unpack string
102
     * @param int    $pos    Position
103
     * @param int    $count  How many elements should be read
104
     *
105
     * @return int[] Array of Integers
106
     */
107 144
    public function readintarray(string $unpack, int $pos, int $count): array
108
    {
109 144
        $data = unpack($unpack . $count, $this->read($pos, 4 * $count));
110 140
        if ($data === false) {
111
            return [];
112
        }
113
114 140
        return $data;
115
    }
116
}
117