Skip to content

Commit ac94969

Browse files
committed
类型转换
1 parent c809e48 commit ac94969

11 files changed

+45
-22
lines changed

src/Packets/AuthMoreDataRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ public static function pack(array $data): Binary
7272
$extraData = $data['extra_data'] ?? [];
7373
$binary->writeByte(self::PACKET_FLAG);
7474
if ($extraData) {
75+
if (!is_array($extraData)) {
76+
throw new PacketException('Invalid extra_data type, expected array', ExceptionCode::ERROR_TYPE);
77+
}
7578
$binary->writeBytes($extraData);
7679
}
77-
}, $packetId);
80+
}, (int)$packetId);
7881
}
7982
}

src/Packets/AuthMoreDataResponse.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Workbunny\MysqlProtocol\Packets;
66

7+
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
8+
use Workbunny\MysqlProtocol\Exceptions\PacketException;
79
use Workbunny\MysqlProtocol\Utils\Binary;
810
use Workbunny\MysqlProtocol\Utils\Packet;
911

@@ -47,7 +49,10 @@ public static function pack(array $data): Binary
4749
$packetId = $data['packet_id'] ?? 0;
4850
return Packet::binary(function (Binary $binary) use ($data) {
4951
$authResponse = $data['auth_response'] ?? '';
52+
if (!is_string($authResponse)) {
53+
throw new PacketException('Invalid auth_response type, expected string', ExceptionCode::ERROR_TYPE);
54+
}
5055
$binary->writeBytes(Binary::StringToBytes($authResponse));
51-
}, $packetId);
56+
}, (int)$packetId);
5257
}
5358
}

src/Packets/AuthSwitchRequest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ public static function pack(array $data): Binary
6060
// 写入标志字节 0xFE
6161
$binary->writeByte(self::PACKET_FLAG);
6262
// 写入认证插件名称(字符串转换成字节数组)及 NULL 终止符
63+
if (!is_string($pluginName)) {
64+
throw new PacketException('Invalid plugin_name type, expected string', ExceptionCode::ERROR_TYPE);
65+
}
6366
$binary->writeNullTerminated(Binary::StringToBytes($pluginName));
6467
// 如果附加认证数据存在,则写入
65-
if ($authPluginData and is_array($authPluginData)) {
68+
if ($authPluginData) {
69+
if (!is_array($authPluginData)) {
70+
throw new PacketException('Invalid auth_plugin_data type, expected bytes array', ExceptionCode::ERROR_TYPE);
71+
}
6672
$binary->writeBytes($authPluginData);
6773
}
6874
}, $data['packet_id'] ?? 0);

src/Packets/AuthSwitchResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Workbunny\MysqlProtocol\Packets;
66

7+
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
8+
use Workbunny\MysqlProtocol\Exceptions\PacketException;
79
use Workbunny\MysqlProtocol\Utils\Binary;
810
use Workbunny\MysqlProtocol\Utils\Packet;
911

@@ -42,6 +44,9 @@ public static function pack(array $data): Binary
4244
$packetId = $data['packet_id'] ?? 0;
4345
return Packet::binary(function (Binary $binary) use ($data) {
4446
$authResponse = $data['auth_response'] ?? '';
47+
if (!is_string($authResponse)) {
48+
throw new PacketException('Invalid auth_response value, expected string', ExceptionCode::ERROR_TYPE);
49+
}
4550
$binary->writeBytes(Binary::StringToBytes($authResponse));
4651
}, $packetId);
4752
}

src/Packets/Command.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Workbunny\MysqlProtocol\Packets;
66

7+
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
8+
use Workbunny\MysqlProtocol\Exceptions\PacketException;
79
use Workbunny\MysqlProtocol\Utils\Binary;
810
use Workbunny\MysqlProtocol\Exceptions\InvalidArgumentException;
911
use Workbunny\MysqlProtocol\Utils\Packet;
@@ -65,7 +67,6 @@ public static function unpack(Binary $binary): array
6567
*
6668
* @param array $data
6769
* @return Binary
68-
* @throws InvalidArgumentException 如果缺少必要的 'command' 字段
6970
*/
7071
public static function pack(array $data): Binary
7172
{
@@ -78,6 +79,9 @@ public static function pack(array $data): Binary
7879

7980
// 如果存在额外数据,则写入(例如对于 COM_QUERY,把 SQL 语句写进来)
8081
if ($data) {
82+
if (!is_string($data)) {
83+
throw new PacketException("Invalid data type, expected string", ExceptionCode::ERROR_TYPE);
84+
}
8185
$binary->writeBytes(Binary::StringToBytes($data));
8286
}
8387
}, $packetId);

src/Packets/Error.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public static function pack(array $data): Binary
9090
// 3. 写入 SQL state marker '#' 和 5 字节 SQL state
9191
$binary->writeByte(ord('#'));
9292
// 不足 5 字节则用空格补齐,多余取前 5 字节
93-
$sqlState = str_pad($sqlState, 5, ' ');
93+
$sqlState = str_pad((string)$sqlState, 5, ' ');
9494
$binary->writeBytes(Binary::StringToBytes(substr($sqlState, 0, 5)));
9595
// 4. 写入错误消息(剩余部分)
9696
if ($errorMessage) {
97-
$binary->writeBytes(Binary::StringToBytes($errorMessage));
97+
$binary->writeBytes(Binary::StringToBytes((string)$errorMessage));
9898
}
9999
}, $packetId);
100100
}

src/Packets/HandshakeInitialization.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public static function pack(array $data): Binary
118118
}
119119
return Packet::binary(function (Binary $binary) use ($data) {
120120
$protocolVersion = $data['protocol_version'] ?? 10;
121-
$serverVersion = $data['server_version'];
121+
$serverVersion = (string)$data['server_version'];
122122
$connectionId = (int)$data['connection_id'];
123123
$capabilityFlags = (int)$data['capability_flags'];
124124
$characterSetIndex = (int)$data['character_set_index'];
125125
$statusFlags = (int)$data['status_flags'];
126126
$authPluginData = (array)$data['auth_plugin_data'];
127-
$authPluginName = $data['auth_plugin_name'];
127+
$authPluginName = (string)$data['auth_plugin_name'];
128128

129129
// 认证数据长度
130130
if (($authPluginDataLength = count($authPluginData)) < 8) {
@@ -168,6 +168,6 @@ public static function pack(array $data): Binary
168168
$binary->writeBytes($authPluginPart2);
169169
// 13. 写入 Auth-plugin 名称(NULL 终止字符串)
170170
$binary->writeNullTerminated(Binary::StringToBytes($authPluginName));
171-
}, $data['packet_id'] ?? 0);
171+
}, (int)$data['packet_id'] ?? 0);
172172
}
173173
}

src/Packets/HandshakeResponse.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ public static function pack(array $data): Binary
115115
{
116116
$packetId = $data['packet_id'] ?? 0;
117117
return Packet::binary(function (Binary $binary) use ($data) {
118-
$capabilityFlags = $data['capability_flags'] ?? 0;
119-
$maxPacketSize = $data['max_packet_size'] ?? 0;
120-
$characterSet = $data['character_set'] ?? 0;
121-
$username = $data['username'] ?? '';
122-
$database = $data['database'] ?? '';
123-
$authPlugin = $data['auth_plugin'] ?? '';
124-
$attributes = $data['attributes'] ?? [];
125-
$authResponse = $data['auth_response'] ?? '';
118+
$capabilityFlags = (int)$data['capability_flags'] ?? 0;
119+
$maxPacketSize = (int)$data['max_packet_size'] ?? 0;
120+
$characterSet = (int)$data['character_set'] ?? 0;
121+
$username = (string)$data['username'] ?? '';
122+
$database = (string)$data['database'] ?? '';
123+
$authPlugin = (string)$data['auth_plugin'] ?? '';
124+
$attributes = (array)$data['attributes'] ?? [];
125+
$authResponse = (string)$data['auth_response'] ?? '';
126126
// 1. 写入能力标志(4 字节)
127127
$binary->writeUB($capabilityFlags, Binary::UB4);
128128
// 2. 写入最大数据包大小(4 字节)
@@ -162,7 +162,7 @@ public static function pack(array $data): Binary
162162
$binary->writeLenEncInt(strlen($attrStr));
163163
$binary->writeBytes(Binary::StringToBytes($attrStr));
164164
}
165-
}, $packetId);
165+
}, (int)$packetId);
166166
}
167167

168168
}

src/Packets/Ok.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public static function pack(array $data): Binary
9292
$binary->writeUB((int)$warnings, Binary::UB2);
9393
// 6. 写入 info 字符串(如果存在)
9494
if ($info) {
95-
$binary->writeBytes(Binary::StringToBytes($info));
95+
$binary->writeBytes(Binary::StringToBytes((string)$info));
9696
}
97-
}, $data['packet_id'] ?? 0);
97+
}, (int)$data['packet_id'] ?? 0);
9898
}
9999
}

src/Packets/ResultSetHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public static function pack(array $data): Binary
3838
return Packet::binary(function (Binary $binary) use ($data) {
3939
$fieldCount = (int)($data['field_count'] ?? 0);
4040
$binary->writeLenEncInt($fieldCount);
41-
}, $data['packet_id'] ?? 0);
41+
}, (int)$data['packet_id'] ?? 0);
4242
}
4343
}

0 commit comments

Comments
 (0)