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 628 in issue
id
Primary key.
INTEGER NOT NULL
repo_id
INTEGER
index
INTEGER
poster_id
INTEGER
original_author
TEXT
original_author_id
INTEGER
name
🔍 代码审查报告:pc-260616 - 需求 盘点单增加时间选择
TEXT
content
## 自动代码审查报告 **分支**: pc-260616 **提交**: `0f7d1fb5b09edc69aab36123175330ca59426459` **提交人**: chenjunfeng (developer.jeff.c@gmail.com) **时间**: 2026-06-09 13:49:27 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 - **总体评价**:业务逻辑链路完整,具备基础的事务控制、权限过滤与批量查询优化意识。但代码存在明显的内存溢出风险(无限制导出)、事务管理不规范、全局实例化滥用及隐式类型转换隐患。整体风格偏向传统 CodeIgniter 3 写法,与现代 PHP (7.4+/8.x) 及 PSR 规范存在一定差距。 - **风险等级**:🟠 中/高(导出功能存在 OOM 风险,事务与并发安全需重点关注) > 📌 **框架说明**:代码中大量使用 `get_instance()`、`$this->load->model()`、`$this->db->trans_*` 等特性,符合 **CodeIgniter 3** 架构规范。若 `phpci` 为内部定制框架,请确认其是否完全兼容 CI3 生命周期;以下审查基于 CI3 + 现代 PHP 最佳实践进行。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `stocktaking_report_export` 约 L240 | **无分页全量导出导致内存溢出 (OOM)**:调用 `get_stocktaking_report($merchantId, $params, 0, 0)` 会一次性加载所有匹配数据至内存,数据量大时直接触发 `Allowed memory size exhausted`。 | 采用分块查询(Chunk)或游标导出。通过循环 `limit/offset` 分批获取数据并追加写入文件,或使用生成器(Generator)流式处理。 | ```php\n// 伪代码示例\n$offset = 0;\n$limit = 1000;\ndo {\n $chunk = $this->get_stocktaking_report($merchantId, $params, 0, $limit, '', '', $offset);\n if (empty($chunk['rows'])) break;\n $this->Export_model->appendRows($chunk['rows']);\n $offset += $limit;\n} while (count($chunk['rows']) === $limit);\n``` | | 🔴 严重 | 文件顶部 L4-L5 | **全局 `$CI` 实例化违反框架生命周期**:在类外部执行 `$CI = & get_instance();` 会在文件被 `include` 时立即执行,若未初始化完成将报错,且破坏 MVC 请求隔离。 | 移除顶部全局实例。在类内部需要时通过 `$this->ci = &get_instance();`(构造函数)或直接使用 `$this->load->model()` 按需加载。 | ```php\nclass Ahead_merchant_goods_stocktaking_model extends Simple_model {\n protected $ci;\n public function __construct() {\n parent::__construct();\n $this->ci =& get_instance();\n }\n}\n``` | | 🟠 警告 | `add_merchant_stocktaking` 约 L68-L85 | **事务管理混用且异常捕获不严谨**:CI3 推荐 `trans_start()` + `trans_complete()` 自动管理。手动 `trans_commit/rollback` 配合 `try-catch` 可能导致事务状态未正确重置,且 `insert` 失败通常返回 `false` 而非抛异常。 | 改用 CI3 标准事务流,或确保 `try-catch` 内捕获所有数据库异常并正确回滚。 | ```php\n$this->db->trans_start();\n$result = $this->insert($addData);\nif (!$result) {\n $this->db->trans_rollback();\n return ['success'=>false, 'msg'=>'主表插入失败'];\n}\n// ... 其他逻辑\n$this->db->trans_complete();\nif ($this->db->trans_status() === FALSE) {\n return ['success'=>false, 'msg'=>'事务执行失败'];\n}\n``` | | 🟠 警告 | `search_stocktaking_list` 约 L118 | **`priv_shop_ids` 空值处理不当引发 SQL 隐患**:`trim($CI->priv_shop_ids, ",")` 为空时 `explode` 返回 `['']`,生成 `WHERE _shop_id IN ('')`,可能导致全表扫描或类型转换错误。 | 增加空值校验,避免无效 `IN` 查询。 | ```php\n$privIds = trim($CI->priv_shop_ids ?? '', ',');\nif ($privIds !== '' && $privIds !== 'all') {\n $where['where_in'] = ['_shop_id', array_filter(explode(',', $privIds), 'strlen')];\n}\n``` | | 🟠 警告 | `get_stocktaking_detail` 约 L168-L172 | **动态修改 `table_name` 非线程安全**:`set_table_name()` 修改的是模型实例属性,高并发下多个请求可能互相覆盖表名,导致查询串数据。 | 使用 Query Builder 的别名(Alias)或临时表名参数,避免修改实例状态。 | ```php\n// 建议在底层 Simple_model 支持传入表名参数,或使用原生别名\n$this->db->from($orginalTableName . ' stocktaking');\n$this->db->join('ahead_merchant_goods goods', 'stocktaking._merchant_goods_id = goods._id', 'LEFT');\n// 避免直接修改 $this->table_name\n``` | | 🟡 建议 | `add_merchant_stocktaking` 约 L48 | **时间比较存在隐式类型转换风险**:`$lastTimeBusiness['end_business_day_str']` 疑似字符串,直接 `+ 86400*15` 会触发 PHP 类型强转警告,且字符串比较 `>` 可能不符合预期。 | 统一转为时间戳或 `DateTime` 对象后再进行数学运算与比较。 | ```php\n$lastTs = strtotime($lastTimeBusiness['end_business_day_str']);\n$nowTs = strtotime($now_business_date['end_business_day_str']);\nif ($nowTs > $lastTs + 86400 * 15) {\n throwError("盘点库存截止时间只能是15天内");\n}\n``` | | 🟡 建议 | 全局多处 | **魔法数字/字符串未提取为常量**:如 `86400*15`、`1`、`2`、`6`(`get_clean_date` 参数)、`'cover'` 等硬编码,降低可维护性。 | 在类顶部定义 `const` 常量,如 `const MAX_STOCKTAKING_DAYS = 15; const EXPORT_TYPE_EXCEL = 1;`。 | `const MAX_STOCKTAKING_DAYS = 15;`<br>`const EXPORT_TYPE_EXCEL = 1;` | | 🟡 建议 | `stocktaking_report_export` 约 L258 | **使用 `exit()` 中断流程不符合 MVC 规范**:直接 `exit` 会跳过框架的响应渲染、日志记录与资源清理,且可能暴露内部错误信息。 | 抛出标准异常或返回统一错误结构,由控制器层统一处理响应。 | `throw new \InvalidArgumentException('导出字段不能为空');` | ## 3. 总结与行动建议 ### 🚨 优先修复的关键问题 1. **导出内存溢出**:立即改造 `stocktaking_report_export`,采用分块查询(`LIMIT/OFFSET`)或流式写入,避免一次性加载全量数据。 2. **事务状态管理**:将 `trans_start/commit/rollback` 替换为 CI3 推荐的 `trans_start()` + `trans_complete()` 组合,或确保 `try-catch` 内严格捕获 `Throwable` 并正确回滚。 3. **全局实例化清理**:移除文件顶部的 `$CI = & get_instance();`,改为在构造函数或方法内按需加载,确保符合框架生命周期。 4. **类型安全加固**:修复时间字符串与整数相加的隐式转换问题,对 `priv_shop_ids` 等外部输入增加严格过滤。 ### 🛠 后续重构与优化方向 - **规范命名与类型声明**:将类名改为 `PascalCase`(如 `AheadMerchantGoodsStocktakingModel`),为方法参数添加 PHP 7+ 类型提示(`int`, `string`, `array`),提升静态分析能力。 - **查询构建器优化**:避免使用 `set_table_name()` 动态切换表名,改用底层 Query Builder 的 `from('table AS alias')` 或封装带表名参数的查询方法,彻底解决并发串数据风险。 - **常量与配置外置**:将业务规则(如最大盘点天数、导出类型枚举、清理数据标识)提取为类常量或配置文件,便于后续维护与多环境部署。 - **统一异常处理**:逐步替换 `throwError()` 和 `exit()`,引入标准 `try-catch` 配合全局异常处理器,确保错误日志可追溯、响应格式统一。 - **框架适配确认**:若项目实际运行于 `phpci` 框架,请核对 `Simple_model` 的底层实现是否完全兼容 CI3 的 `trans_*` 与 `where_in` 语法。如有差异,建议查阅 `phpci` 官方文档替换为框架原生事务与查询 API。 > 💡 **审查结论**:当前代码具备可运行的业务基础,但生产环境部署前务必解决导出 OOM 与事务管理隐患。建议结合 CI3/PHP 8 特性进行渐进式重构,可显著提升系统稳定性与可维护性。 --- *此 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