Passed
Pull Request — master (#27)
by kacper
02:45
created

BinLogServerInfo::isGeneric()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MySQLReplication\BinLog;
4
5
/**
6
 * Class BinLogServerInfo
7
 * @package MySQLReplication\BinLog
8
 */
9
class BinLogServerInfo
10
{
11
    const MYSQL_VERSION_MARIADB = 'MariaDB';
12
    const MYSQL_VERSION_PERCONA = 'Percona';
13
    const MYSQL_VERSION_GENERIC = 'MySQL';
14
    /**
15
     * @var array
16
     */
17
    private static $serverInfo = [];
18
19
    /**
20
     * @param string $data
21
     * @param string $version
22
     */
23 54
    public static function parsePackage($data, $version)
24
    {
25 54
        $i = 0;
26 54
        $length = strlen($data);
27 54
        self::$serverInfo['protocol_version'] = ord($data[$i]);
28 54
        $i++;
29
30
        //version
31 54
        self::$serverInfo['server_version'] = '';
32 54
        $start = $i;
33 54
        for ($i = $start; $i < $length; $i++) {
34 54
            if ($data[$i] === chr(0)) {
35 54
                $i++;
36 54
                break;
37
            }
38 54
            self::$serverInfo['server_version'] .= $data[$i];
39 54
        }
40
41
        //connection_id 4 bytes
42 54
        self::$serverInfo['connection_id'] = unpack('I', $data[$i] . $data[++$i] . $data[++$i] . $data[++$i])[1];
43 54
        $i++;
44
45
        //auth_plugin_data_part_1
46
        //[len=8] first 8 bytes of the auth-plugin data
47 54
        self::$serverInfo['salt'] = '';
48 54 View Code Duplication
        for ($j = $i; $j < $i + 8; $j++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49 54
            self::$serverInfo['salt'] .= $data[$j];
50 54
        }
51 54
        $i += 8;
52
53
        //filler_1 (1) -- 0x00
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54 54
        $i++;
55
56
        //capability_flag_1 (2) -- lower 2 bytes of the Protocol::CapabilityFlags (optional)
57 54
        $i += 2;
58
59
        //character_set (1) -- default server character-set, only the lower 8-bits Protocol::CharacterSet (optional)
60 54
        self::$serverInfo['character_set'] = $data[$i];
61 54
        $i++;
62
63
        //status_flags (2) -- Protocol::StatusFlags (optional)
1 ignored issue
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64 54
        $i += 2;
65
66
        //capability_flags_2 (2) -- upper 2 bytes of the Protocol::CapabilityFlags
67 54
        $i += 2;
68
69
        //auth_plugin_data_len (1) -- length of the combined auth_plugin_data, if auth_plugin_data_len is > 0
70 54
        $salt_len = ord($data[$i]);
71 54
        $i++;
72
73 54
        $salt_len = max(12, $salt_len - 9);
74
75 54
        $i += 10;
76
77
        //next salt
78 54
        if ($length >= $i + $salt_len) {
79 54 View Code Duplication
            for ($j = $i; $j < $i + $salt_len; $j++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80 54
                self::$serverInfo['salt'] .= $data[$j];
81 54
            }
82
83 54
        }
84 54
        self::$serverInfo['auth_plugin_name'] = '';
85 54
        $i += $salt_len + 1;
86 54
        for ($j = $i; $j < $length - 1; $j++) {
87 54
            self::$serverInfo['auth_plugin_name'] .= $data[$j];
88 54
        }
89
90 54
        self::$serverInfo['version_name'] = self::parseVersion($version);
91 54
    }
92
93
    /**
94
     * @return string
95
     */
96 54
    public static function getSalt()
97
    {
98 54
        return self::$serverInfo['salt'];
99
    }
100
101
    /**
102
     * @see http://stackoverflow.com/questions/37317869/determine-if-mysql-or-percona-or-mariadb
103
     * @param string $version
104
     * @return string
105
     */
106 54
    private static function parseVersion($version)
107
    {
108 54
        if ('' !== $version) {
109 54
            if (false !== strpos($version, self::MYSQL_VERSION_MARIADB)) {
110
                return self::MYSQL_VERSION_MARIADB;
111
            }
112 54
            if (false !== strpos($version, self::MYSQL_VERSION_PERCONA)) {
113
                return self::MYSQL_VERSION_PERCONA;
114
            }
115 54
        }
116
117 54
        return self::MYSQL_VERSION_GENERIC;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public static function getVersion()
124
    {
125
        return self::$serverInfo['version_name'];
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public static function isMariaDb()
132
    {
133
        return self::MYSQL_VERSION_MARIADB === self::getVersion();
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public static function isPercona()
140
    {
141
        return self::MYSQL_VERSION_PERCONA === self::getVersion();
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public static function isGeneric()
148
    {
149
        return self::MYSQL_VERSION_GENERIC === self::getVersion();
150
    }
151
}