sqlite-web 0.7.2
gitea.db
issue
Create
Query
access
access_token
action
action_artifact
action_run
action_run_index
action_run_job
action_runner
action_runner_token
action_schedule
action_schedule_spec
action_task
action_task_output
action_task_step
action_tasks_version
action_variable
app_state
attachment
auth_token
badge
branch
collaboration
comment
commit_status
commit_status_index
commit_status_summary
commit_sync_log
commit_sync_status
dbfs_data
dbfs_meta
deploy_key
email_address
email_hash
external_login_user
follow
gpg_key
gpg_key_import
hook_task
issue
issue_assignees
issue_content_history
issue_dependency
issue_index
issue_label
issue_pin
issue_user
issue_watch
label
language_stat
lfs_lock
lfs_meta_object
login_source
milestone
mirror
notice
notification
oauth2_application
oauth2_authorization_code
oauth2_grant
org_user
package
package_blob
package_blob_upload
package_cleanup_rule
package_file
package_property
package_version
project
project_board
project_issue
protected_branch
protected_tag
public_key
pull_auto_merge
pull_request
push_mirror
reaction
release
renamed_branch
repo_archiver
repo_hidden_file
repo_indexer_status
repo_license
repo_redirect
repo_topic
repo_transfer
repo_unit
repository
review
review_state
secret
session
sqlite_sequence
star
stopwatch
system_setting
task
team
team_invite
team_repo
team_unit
team_user
topic
tracked_time
two_factor
upload
user
user_badge
user_blocking
user_open_id
user_redirect
user_setting
version
watch
webauthn_credential
webhook
Toggle helper tables
Structure
Content
Query
Insert
Drop
Import
Export
Update row 244 in issue
id
Primary key.
INTEGER NOT NULL
repo_id
INTEGER
index
INTEGER
poster_id
INTEGER
original_author
TEXT
original_author_id
INTEGER
name
🔍 代码审查报告:pay-260616 - 需求 聚旺设备先软关机 16371
TEXT
content
## 自动代码审查报告 **分支**: pay-260616 **提交**: `ef8175b72ebadbc0b9f1d1cc707031c246034f0a` **提交人**: chenjunfeng (developer.jeff.c@gmail.com) **时间**: 2026-05-20 16:10:43 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:业务逻辑覆盖全面,能够处理多种智能设备(时序器、空开、门锁、新风机等)的联动控制。但存在**严重的代码重复**、**同步阻塞调用**、**不规范的全局变量使用**及**大量死代码**。整体架构偏向过程式堆砌,缺乏面向对象设计原则的约束,可维护性与高并发性能存在较大隐患。 - **风险等级**:🔴 高(主要源于同步阻塞导致的请求超时风险、代码重复引发的维护漏洞及潜在的死代码误导) > 📌 **框架适配说明**:代码语法高度符合 CodeIgniter (CI) 3/4 特征(如 `$this->load->model()`、`$this->db->insert_batch()`)。若 `phpci` 为贵司内部定制框架,请核对以下建议与官方文档的兼容性。部分全局函数(如 `throwError`、`curlWebsocketApi`)未在上下文中定义,审查基于通用 PHP/CI 最佳实践。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `action_data` & `action_data_power_fist` (约 100-250 行) | 两个方法代码重复率超 90%,仅 `power_operate` 与 `sequencer_operate` 的调用顺序不同。严重违反 DRY 原则,后续新增设备类型或修改查询逻辑时极易遗漏,导致逻辑分裂。 | 提取公共数据准备逻辑为独立方法,通过布尔参数 `$power_first` 控制执行顺序,彻底消除重复代码。 | 见下方 `重构示例` | | 🔴 严重 | `sequencer_operate` (约 340 行) | 循环内使用 `usleep(200000)` 同步阻塞当前 PHP 进程。在 Web 请求中会直接拖慢响应时间,设备线路较多时极易触发 PHP-FPM 超时或耗尽工作进程。 | 移除 `usleep`。将延迟控制交由设备端固件处理,或改为异步消息队列(如 Redis Delay Queue / RabbitMQ)下发指令。 | 移除 `usleep(200000);`,改为记录指令序列交由底层驱动异步调度。 | | 🟠 警告 | 文件顶部 (1-3 行) | `$CI = &get_instance(); $CI->load->model('Simple_model');` 位于类外部。在 CI 架构中,模型加载应在类内部完成,外部调用易引发未定义变量或加载时机错误。 | 删除顶部代码。类已继承 `Simple_model`,框架会自动处理父类加载。如需加载其他模型,移至 `__construct()`。 | `// 删除顶部 1-3 行代码`<br>`class Ahead_intelligent_control_log_model extends Simple_model { ... }` | | 🟠 警告 | `do_after_sequencer` (约 280 行) | 方法首行直接 `return true;`,下方约 80 行代码成为死代码。注释说明需求已废弃,但未清理,严重干扰后续开发者阅读。 | 彻底删除死代码,或保留空方法并添加 `@deprecated` 注解。 | `/** @deprecated 2026-01-05 需求废弃,保留空方法兼容旧调用 */`<br>`public function do_after_sequencer($params) { return true; }` | | 🟠 警告 | 多处方法内部 | 频繁使用 `throwError()` 全局函数。非标准 PHP 异常机制,若未配置全局捕获器将直接导致 500 错误且丢失完整堆栈,不利于线上排查。 | 替换为 PHP 标准异常或框架内置异常类,配合全局异常处理器统一返回格式。 | `if (empty($room_data)) { throw new \InvalidArgumentException("包厢不存在"); }` | | 🟡 建议 | 类属性/常量 | 命名不规范:`action_data_power_fist` (拼写错误)、`type_to_type_fun` (冗余)、`xinfeng_action` 与 `xinfeng_actions` 混用。缺乏 PHP 7+ 类型声明。 | 修正拼写,统一命名风格(蛇形/驼形),补充 `declare(strict_types=1);` 及参数/返回类型提示。 | `action_data_power_first`<br>`map_control_type(int $control, int $type): int` | | 🟡 建议 | 多处方法内部 | 每个方法开头重复 `$this->load->model('...')`。频繁调用增加框架 I/O 开销,且破坏单一职责原则。 | 将高频依赖模型统一移至 `__construct()` 中加载,或配置框架自动加载。 | `public function __construct() { parent::__construct(); $this->load->model(['Ahead_yc_shop_model', 'Ahead_family_servers_model']); }` | | 🟡 建议 | `power_operate` / `breaker_operate` | 数据库查询条件使用自定义数组结构(如 `'where_in' => [...]`)。若框架未做严格转义,存在潜在 SQL 注入风险。 | 确认框架底层是否自动绑定参数。建议统一使用框架提供的查询构造器链式调用,避免手动拼接。 | `$this->db->where_in('_control_type', $types)->get()->result_array();` | ### 🔧 核心重复代码重构示例 ```php // 提取公共逻辑 private function prepare_control_data(int $merchant_id, array $params, int $type): ?array { if ($type === 0) return null; $room_id = $params['room_id'] ?? 0; if (empty($room_id)) return null; // ... 此处保留原 action_data 中查询 room_data, shop_data, device_data, control_data 的逻辑 ... // 返回组装好的数据数组 return [ 'room_data' => $room_data, 'shop_data' => $shop_data, 'device_data' => $data, 'control_data' => $control_data, 'actions' => $this->categorize_actions($control_data) // 提取分类逻辑 ]; } // 统一入口 public function action_data(int $merchant_id, array $params, int $type = 1): bool { return $this->execute_control_flow($merchant_id, $params, $type, false); } public function action_data_power_first(int $merchant_id, array $params, int $type = 1): bool { return $this->execute_control_flow($merchant_id, $params, $type, true); } private function execute_control_flow(int $merchant_id, array $params, int $type, bool $power_first): bool { $data = $this->prepare_control_data($merchant_id, $params, $type); if (!$data) return true; if ($power_first) { $this->power_operate(...); $this->breaker_operate(...); $this->ttlock_operate(...); $this->xinfeng_operate(...); $this->sequencer_operate(...); } else { $this->sequencer_operate(...); $this->power_operate(...); $this->breaker_operate(...); $this->ttlock_operate(...); $this->xinfeng_operate(...); } return true; } ``` ## 3. 总结与行动建议 ### 🚨 优先修复的关键问题 1. **消除代码重复**:立即合并 `action_data` 与 `action_data_power_fist`,采用策略参数或模板方法模式重构,降低后续维护成本。 2. **移除同步阻塞**:彻底删除 `usleep()`,评估业务是否允许异步下发。若必须保证时序,建议引入消息队列(如 Redis `ZSET` 延迟队列)或交由硬件网关本地调度。 3. **清理死代码与规范异常**:删除 `do_after_sequencer` 中的废弃逻辑,将 `throwError()` 替换为标准 `Exception`,确保错误可追踪、可监控。 ### 🛠 后续重构与优化方向 - **架构解耦**:当前 Model 承担了过多职责(数据查询、协议组装、MQTT/WebSocket 通信、日志记录)。建议引入 **Service 层** 处理业务编排,**Repository 层** 封装数据访问,**Gateway/Driver 层** 统一设备通信协议。 - **异步化改造**:智能设备控制属于典型的 I/O 密集型操作。建议将控制指令写入任务表或推入队列,由独立 Worker 进程消费,彻底释放 Web 请求线程。 - **类型安全与静态分析**:全面补充 PHP 7.4+ 类型声明,启用 `declare(strict_types=1);`。配合 PHPStan 或 Psalm 进行静态扫描,提前拦截类型不匹配与未定义变量问题。 - **配置集中化**:将硬编码的常量(如 `SEQUENCER_FIRST_TYPE`、设备型号映射)抽离至配置文件或数据库字典表,避免每次发版需修改代码。 > 💡 **局限性说明**:由于未提供 `Simple_model` 基类实现、全局函数 `throwError`/`curlWebsocketApi` 的源码及 `phpci` 框架的查询构造器底层实现,部分安全性与性能评估基于通用 CI/PHP 规范推断。建议结合框架官方文档对 `where_in` 参数绑定机制进行二次验证。 --- *此 Issue 由代码审查服务自动创建*
TEXT
milestone_id
INTEGER
priority
INTEGER
is_closed
INTEGER
is_pull
INTEGER
num_comments
INTEGER
ref
TEXT
deadline_unix
INTEGER
created_unix
INTEGER
updated_unix
INTEGER
closed_unix
INTEGER
is_locked
INTEGER NOT NULL (default 0
content_version
INTEGER NOT NULL (default 0
time_estimate
INTEGER NOT NULL (default 0
Update
Cancel