vr-shopxo-plugin/reviews/DebugAgent-PRELIMINARY.md

122 lines
3.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# DebugAgent Round 1 静态分析报告
> Agentcouncil/DebugAgent | 日期2026-04-20
> 代码版本bbea35d83feat: 保存时自动填充 template_snapshot
---
## 分析方法
基于代码静态分析,识别所有访问 `'id'` 键的位置,并按 PHP 8+ 严格类型行为评估触发概率。
---
## 一、所有 "id" 访问位置分析
### 位置 1AdminGoodsSaveHandle.php 第 77 行Primary
```php
return in_array($r['id'], $config['selected_rooms'] ?? []);
```
- **触发条件**:当 `$r`rooms 数组元素)缺少 `'id'` key
- **PHP 8+ 行为**:直接抛出 `Undefined array key "id"`
- **对比**SeatSkuService::BatchGenerate 第 100 行有正确写法:`!empty($r['id']) ? $r['id'] : ('room_' . $rIdx)`
### 位置 2AdminGoodsSaveHandle.php 第 71 行
```php
$template = Db::name('vr_seat_templates')->find($templateId);
$seatMap = json_decode($template['seat_map'] ?? '{}', true);
```
- **注意**:报错是 `"seat_map"` 不是 `"id"`
- **PHP 8+ 行为**:若 `$template` 是 null`$template['seat_map']` 抛出 `Undefined array key "seat_map"`
- **二级风险**:若 `$template` 是空数组 `[]``$template['seat_map']` 也抛出同样错误
### 位置 3SeatSkuService::BatchGenerate 第 100 行
```php
$roomId = !empty($room['id']) ? $room['id'] : ('room_' . $rIdx);
```
- **已安全**:有 `!empty()` 防护
### 位置 4SeatSkuService::ensureAndFillVrSpecTypes 第 283 行
```php
$existingNames = array_column($existingItems, 'name');
```
- **低风险**:若 `$existingItems` 不是数组,`array_column()` 抛出 Warning
---
## 二、表前缀分析
| 方法 | 展开 | 实际表名 |
|------|------|---------|
| `BaseService::table('seat_templates')` | `'vr_' + 'seat_templates'` | `vr_seat_templates` |
| `Db::name('vr_seat_templates')` | ThinkPHP prefix + `vr_seat_templates` | `vrt_vr_seat_templates` |
**关键发现**BackendArchitect 的 debug 报告已验证 ShopXO 前缀为 `vrt_`,两者等价。
---
## 三、PHP 8+ `??` 操作符关键行为
```php
$template['seat_map'] ?? '{}'
```
PHP 8+ null 合并操作符行为:
-`$template === null` → 返回 `'{}'`
-`$template = []` → 访问 `$template['seat_map']` 时抛出 `Undefined array key "seat_map"`
-`$template['seat_map'] === null` → 返回 `'{}'`
**`??` 不防御"数组存在但键不存在"的情况**。正确的防御写法:
```php
isset($template['seat_map']) ? $template['seat_map'] : '{}'
// 或
($template['seat_map'] ?? null) ?? '{}' // 先解包键,再解包 null
```
---
## 四、vr_goods_config JSON 解码安全性
```php
$configs = json_decode($rawConfig, true);
if (is_array($configs) && !empty($configs)) {
foreach ($configs as $i => &$config) {
```
- `$configs` 类型检查正确(`is_array()`
- `$config['template_id']` 访问安全(在 `foreach` 中不会越界)
- `$config['selected_rooms']` 访问安全(`?? []` 提供默认值)
---
## 五、根因概率评估
| 位置 | 错误类型 | 概率 | 原因 |
|------|---------|------|------|
| 第 77 行 `$r['id']` | "id" | **高** | 如果 room 数据无 id 字段 |
| 第 71 行 `$template['seat_map']` | "seat_map" | **低** | 如果 template 记录不存在 |
| 类型不匹配 | 静默 | **高** | str vs int 类型不一致 |
---
## 六、结论
1. **Primary**:第 77 行 `$r['id']` 无空安全 → 与 BackendArchitect 结论一致
2. **Secondary**:第 71 行 `$template` 可能为 null/[] → 与 BackendArchitect 一致
3. **Table prefix**:两者等价,已排除
4. **PHP 8+ 行为**`??` 对空数组 `[]` 的键访问无效,需用 `isset()`
---
## 七、Round 2 待验证项
- [ ] 读取 `shopxo/config/database.php` 确认 ShopXO 前缀
- [ ] 读取 `admin/Admin.php` 第 66 行BackendArchitect 引用的前缀验证代码)
- [ ] 编写 `reports/DebugAgent-ROOT_CAUSE.md`