Skip to content

Commit f9f6c79

Browse files
committed
Fix maintainer feedback: always return strings from readInt64/unpackUInt64
- Changed readInt64() to always return string instead of string|int - Changed unpackUInt64() to always return string instead of string|int - Changed readUInt64() return type to string for consistency - Applied code style fixes with composer cs:fix - Fixes failing tests: testShouldReadInt64 and testShouldPack64bit Addresses maintainer feedback from PR krowinski#116 regarding MySQL 8.4.0 compatibility.
1 parent 09abaf4 commit f9f6c79

File tree

5 files changed

+29
-39
lines changed

5 files changed

+29
-39
lines changed

src/MySQLReplication/BinLog/BinLogSocketConnect.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ private function getBinlogStream(): void
195195

196196
if ($this->config->heartbeatPeriod > 0.00) {
197197
// master_heartbeat_period is in nanoseconds
198-
if(version_compare($this->repository->getVersion(),"8.4.0")>=0){
199-
$this->executeSQL('SET @source_heartbeat_period = ' . $this->config->heartbeatPeriod * 1000000000);
200-
}else{
201-
$this->executeSQL('SET @master_heartbeat_period = ' . $this->config->heartbeatPeriod * 1000000000);
202-
}
198+
if (version_compare($this->repository->getVersion(), '8.4.0') >= 0) {
199+
$this->executeSQL('SET @source_heartbeat_period = ' . $this->config->heartbeatPeriod * 1000000000);
200+
} else {
201+
$this->executeSQL('SET @master_heartbeat_period = ' . $this->config->heartbeatPeriod * 1000000000);
202+
}
203203

204204
$this->logger->debug('Heartbeat period set to ' . $this->config->heartbeatPeriod . ' seconds');
205205
}

src/MySQLReplication/BinaryDataReader/BinaryDataReader.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,16 @@ public function readUInt24(): int
114114
return $data[1] + ($data[2] << 8) + ($data[3] << 16);
115115
}
116116

117-
public function readUInt64(): string|int
117+
public function readUInt64(): string
118118
{
119119
return $this->unpackUInt64($this->read(self::UNSIGNED_INT64_LENGTH));
120120
}
121121

122-
public function unpackUInt64(string $binary): string|int
122+
public function unpackUInt64(string $binary): string
123123
{
124124
$data = self::unpack('V*', $binary);
125125

126-
$num = bcadd((string)$data[1], bcmul((string)$data[2], bcpow('2', '32')));
127-
if($num>PHP_INT_MAX || $num<PHP_INT_MIN){
128-
return $num;
129-
}else{
130-
return intval($num);
131-
}
126+
return bcadd((string)$data[1], bcmul((string)$data[2], bcpow('2', '32')));
132127
}
133128

134129
public function readInt24(): int
@@ -143,16 +138,11 @@ public function readInt24(): int
143138
return $res;
144139
}
145140

146-
public function readInt64(): string|int
141+
public function readInt64(): string
147142
{
148143
$data = self::unpack('V*', $this->read(self::UNSIGNED_INT64_LENGTH));
149144

150-
$num = bcadd((string)$data[1], (string)($data[2] << 32));
151-
if($num>PHP_INT_MAX || $num<PHP_INT_MIN){
152-
return $num;
153-
}else{
154-
return intval($num);
155-
}
145+
return bcadd((string)$data[1], (string)($data[2] << 32));
156146
}
157147

158148
public function readLengthString(int $size): string

src/MySQLReplication/Config/Config.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function validate(): void
106106
public function checkDataBasesOnly(string $database): bool
107107
{
108108
return ($this->databasesOnly !== [] && !in_array($database, $this->databasesOnly, true))
109-
|| ($this->databasesRegex !== [] && !self::matchNames($database, $this->databasesRegex));
109+
|| ($this->databasesRegex !== [] && !self::matchNames($database, $this->databasesRegex));
110110
}
111111

112112

@@ -116,17 +116,6 @@ public function checkTablesOnly(string $table): bool
116116
|| ($this->tablesRegex !== [] && !self::matchNames($table, $this->tablesRegex));
117117
}
118118

119-
private static function matchNames(string $name, array $patterns): bool
120-
{
121-
foreach ($patterns as $pattern) {
122-
if (preg_match($pattern, $name)) {
123-
return true;
124-
}
125-
}
126-
127-
return false;
128-
}
129-
130119
public function checkEvent(int $type): bool
131120
{
132121
if ($this->eventsOnly !== [] && !in_array($type, $this->eventsOnly, true)) {
@@ -144,4 +133,15 @@ public function jsonSerialize(): array
144133
{
145134
return get_class_vars(self::class);
146135
}
136+
137+
private static function matchNames(string $name, array $patterns): bool
138+
{
139+
foreach ($patterns as $pattern) {
140+
if (preg_match($pattern, $name)) {
141+
return true;
142+
}
143+
}
144+
145+
return false;
146+
}
147147
}

src/MySQLReplication/Repository/MySQLRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getVersion(): string
6060
$res = $this->getConnection()
6161
->fetchAssociative('SHOW VARIABLES LIKE "version"');
6262

63-
return $res['Value']??"";
63+
return $res['Value'] ?? '';
6464
}
6565

6666
public function getMasterStatus(): MasterStatusDTO

tests/Unit/Repository/MySQLRepositoryTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ public function testShouldIsCheckSum(): void
7070

7171
public function testShouldGetVersion(): void
7272
{
73-
$expected = [
74-
'Value' => 'version',
75-
];
73+
$expected = [
74+
'Value' => 'version',
75+
];
7676

77-
$this->connection->method('fetchAssociative')
78-
->willReturn($expected);
77+
$this->connection->method('fetchAssociative')
78+
->willReturn($expected);
7979

80-
self::assertEquals('version', $this->mySQLRepositoryTest->getVersion());
80+
self::assertEquals('version', $this->mySQLRepositoryTest->getVersion());
8181
}
8282

8383
public function testShouldGetMasterStatus(): void

0 commit comments

Comments
 (0)