Util::loadData()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * phpMyAdmin ShapeFile library
7
 * <https://github.com/phpmyadmin/shapefile/>.
8
 *
9
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
10
 * Copyright 2016 - 2017 Michal Čihař <[email protected]>
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation.
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
22
 * along with this program; if not, you can download one from
23
 * https://www.gnu.org/copyleft/gpl.html.
24
 */
25
26
namespace PhpMyAdmin\ShapeFile;
27
28
use function current;
29
use function pack;
30
use function strrev;
31
use function unpack;
32
33
class Util
34
{
35
    private static bool|null $littleEndian = null;
36
37
    /**
38
     * Reads data.
39
     *
40
     * @param string       $type type for unpack()
41
     * @param string|false $data Data to process
42
     */
43 102
    public static function loadData(string $type, string|false $data): mixed
44
    {
45 102
        if ($data === false || $data === '') {
46 97
            return false;
47
        }
48
49 93
        $tmp = unpack($type, $data);
50
51 93
        return $tmp === false ? false : current($tmp);
52
    }
53
54
    /**
55
     * Encodes double value to correct endianity.
56
     */
57 61
    public static function packDouble(float $value): string
58
    {
59 61
        $bin = pack('d', $value);
60
61 61
        if (self::$littleEndian === null) {
62 4
            self::$littleEndian = (pack('L', 1) === pack('V', 1));
63
        }
64
65 61
        if (self::$littleEndian) {
66 61
            return $bin;
67
        }
68
69
        return strrev($bin);
70
    }
71
}
72