BinLogServerInfo::parsePackage()   B
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 69
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 36
nc 24
nop 2
dl 0
loc 69
ccs 37
cts 37
cp 1
crap 7
rs 8.4106
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\BinLog;
5
6
class BinLogServerInfo
7
{
8
    private const MYSQL_VERSION_MARIADB = 'MariaDB';
9
    private const MYSQL_VERSION_PERCONA = 'Percona';
10
    private const MYSQL_VERSION_GENERIC = 'MySQL';
11
    private static $serverInfo = [];
12
13 58
    public static function parsePackage(string $data, string $version): void
14
    {
15 58
        $i = 0;
16 58
        $length = strlen($data);
17 58
        self::$serverInfo['protocol_version'] = ord($data[$i]);
18 58
        ++$i;
19
20
        //version
21 58
        self::$serverInfo['server_version'] = '';
22 58
        $start = $i;
23 58
        for ($i = $start; $i < $length; ++$i) {
24 58
            if ($data[$i] === chr(0)) {
25 58
                ++$i;
26 58
                break;
27
            }
28 58
            self::$serverInfo['server_version'] .= $data[$i];
29
        }
30
31
        //connection_id 4 bytes
32 58
        self::$serverInfo['connection_id'] = unpack('I', $data[$i] . $data[++$i] . $data[++$i] . $data[++$i])[1];
33 58
        ++$i;
34
35
        //auth_plugin_data_part_1
36
        //[len=8] first 8 bytes of the auth-plugin data
37 58
        self::$serverInfo['salt'] = '';
38 58
        for ($j = $i; $j < $i + 8; ++$j) {
39 58
            self::$serverInfo['salt'] .= $data[$j];
40
        }
41 58
        $i += 8;
42
43
        //filler_1 (1) -- 0x00
44 58
        ++$i;
45
46
        //capability_flag_1 (2) -- lower 2 bytes of the Protocol::CapabilityFlags (optional)
47 58
        $i += 2;
48
49
        //character_set (1) -- default server character-set, only the lower 8-bits Protocol::CharacterSet (optional)
50 58
        self::$serverInfo['character_set'] = $data[$i];
51 58
        ++$i;
52
53
        //status_flags (2) -- Protocol::StatusFlags (optional)
54 58
        $i += 2;
55
56
        //capability_flags_2 (2) -- upper 2 bytes of the Protocol::CapabilityFlags
57 58
        $i += 2;
58
59
        //auth_plugin_data_len (1) -- length of the combined auth_plugin_data, if auth_plugin_data_len is > 0
60 58
        $salt_len = ord($data[$i]);
61 58
        ++$i;
62
63 58
        $salt_len = max(12, $salt_len - 9);
64
65 58
        $i += 10;
66
67
        //next salt
68 58
        if ($length >= $i + $salt_len) {
69 58
            for ($j = $i; $j < $i + $salt_len; ++$j) {
70 58
                self::$serverInfo['salt'] .= $data[$j];
71
            }
72
73
        }
74 58
        self::$serverInfo['auth_plugin_name'] = '';
75 58
        $i += $salt_len + 1;
76 58
        for ($j = $i; $j < $length - 1; ++$j) {
77 58
            self::$serverInfo['auth_plugin_name'] .= $data[$j];
78
        }
79
80 58
        self::$serverInfo['version_name'] = self::parseVersion($version);
81 58
        self::$serverInfo['version_revision'] = self::parseRevision($version);
82 58
    }
83
84 58
    public static function getSalt(): string
85
    {
86 58
        return self::$serverInfo['salt'];
87
    }
88
89
    /**
90
     * @see http://stackoverflow.com/questions/37317869/determine-if-mysql-or-percona-or-mariadb
91
     */
92 58
    private static function parseVersion(string $version): string
93
    {
94 58
        if ('' !== $version) {
95 58
            if (false !== strpos($version, self::MYSQL_VERSION_MARIADB)) {
96
                return self::MYSQL_VERSION_MARIADB;
97
            }
98 58
            if (false !== strpos($version, self::MYSQL_VERSION_PERCONA)) {
99
                return self::MYSQL_VERSION_PERCONA;
100
            }
101
        }
102
103 58
        return self::MYSQL_VERSION_GENERIC;
104
    }
105
106
    public static function getRevision(): float
107
    {
108
        return self::$serverInfo['version_revision'];
109
    }
110
111 58
    public static function getVersion(): string
112
    {
113 58
        return self::$serverInfo['version_name'];
114
    }
115
116 58
    public static function isMariaDb(): bool
117
    {
118 58
        return self::MYSQL_VERSION_MARIADB === self::getVersion();
119
    }
120
121
    public static function isPercona(): bool
122
    {
123
        return self::MYSQL_VERSION_PERCONA === self::getVersion();
124
    }
125
126
    public static function isGeneric(): bool
127
    {
128
        return self::MYSQL_VERSION_GENERIC === self::getVersion();
129
    }
130
131 58
    private static function parseRevision(string $version): float
132
    {
133 58
        return (float)$version;
134
    }
135
}