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
Delete row 632 from issue
id
632
repo_id
18
index
198
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pc-260616 - 需求 盘点单增加时间选择
content
## 自动代码审查报告 **分支**: pc-260616 **提交**: `c9caaf5c47
## 自动代码审查报告 **分支**: pc-260616 **提交**: `c9caaf5c47e86a80287dbba7b932ce23cc35c06a` **提交人**: chenjunfeng (developer.jeff.c@gmail.com) **时间**: 2026-06-09 14:19:45 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:业务逻辑链路完整,具备基础的事务控制与 N+1 查询优化意识。但存在多处严重架构违规(模型层直接 `exit`、顶层实例化 CI 对象)、类型混淆导致的逻辑缺陷,以及不符合现代 PHP 规范的命名与类型声明问题。整体可维护性与健壮性需重点提升。 - **风险等级**:🔴 高 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | 文件顶部 | 顶层直接执行 `$CI = & get_instance();`。在框架未完全初始化或模型被提前加载时会导致 Fatal Error,且违反 MVC 分层原则。 | 移除顶层代码。在方法内部按需使用 `$this->load->model()` 或框架提供的实例获取方式。 | `// 删除文件顶部的 $CI = & get_instance(); 及后续加载逻辑` | | 🔴 严重 | `stocktaking_report_export` 方法内 | 模型层使用 `exit()` 强制中断脚本执行。破坏框架生命周期,导致后续中间件、日志记录、事务回滚无法执行,且无法被上层捕获。 | 改为抛出标准异常或返回统一错误结构,由控制器层统一处理响应。 | `if (empty($exportFields)) { throw new \InvalidArgumentException('导出字段不能为空'); }` | | 🔴 严重 | `add_merchant_stocktaking` 约第 38 行 | 字符串与整数混合运算及比较:`$now_business_date['end_business_day_str'] > $lastTimeBusiness['end_business_day_str'] + 86400*15`。PHP 会将字符串转为 `0` 或触发 Warning,导致时间校验完全失效。 | 统一转换为时间戳后再进行数学运算与比较。 | `$nowTs = strtotime($now_business_date['end_business_day_str']);<br>$lastTs = strtotime($lastTimeBusiness['end_business_day_str']);<br>if ($nowTs > $lastTs + 15 * 86400) { throwError("..."); }` | | 🟠 警告 | `add_merchant_stocktaking` 事务块 | 混用 CI 自动事务 (`trans_start`) 与手动提交/回滚 (`trans_commit`/`trans_rollback`)。若 `insert` 失败未抛异常,事务状态可能不一致。 | 使用显式事务控制 `trans_begin()` + `try-catch` + `trans_commit/rollback`,或统一使用 `trans_start()` + `trans_complete()`。 | `$this->db->trans_begin();<br>try { /* 业务逻辑 */ $this->db->trans_commit(); } catch (\Exception $e) { $this->db->trans_rollback(); throw $e; }` | | 🟠 警告 | `get_stocktaking_detail` 约第 115 行 | 动态修改共享模型表名 `set_table_name()`。在并发请求或同一请求多次调用时,会污染模型全局状态,导致后续查询错乱。 | 使用查询构建器别名或创建临时查询实例,避免修改模型属性。 | `$this->db->select("SUM($amountField * stocktaking._profit_loss) AS amount_total")<br> ->from($orginalTableName . ' stocktaking')<br> ->join('ahead_merchant_goods goods', 'stocktaking._merchant_goods_id = goods._id', 'LEFT')<br> ->where('stocktaking._stocktaking_id', $takingDetail['id'])<br> ->get()->row_array();` | | 🟠 警告 | `search_stocktaking_list` / `get_stocktaking_report` | `strtotime($params['start_time'])` 未校验返回值。若传入非法时间字符串将返回 `false`,传入 Query Builder 可能引发 SQL 语法错误或注入风险。 | 增加时间格式校验,失败时跳过条件或赋予安全默认值。 | `if (!empty($params['start_time'])) { $ts = strtotime($params['start_time']); if ($ts !== false) $where['stocktaking._create_time >= '] = $ts; }` | | 🟡 建议 | 全局类/方法定义 | 类名 `Ahead_merchant_goods_stocktaking_model` 与方法名不符合 PSR-12 规范。缺乏 PHP 7+ 类型声明,降低 IDE 提示与静态分析能力。 | 类名改为 `PascalCase`,方法名改为 `camelCase`,补充参数类型与返回值声明。 | `class AheadMerchantGoodsStocktakingModel extends Simple_model {<br> public function addMerchantStocktaking(int $merchantId, int $adminId, string $adminName, array $data): array { ... }` | | 🟡 建议 | `stocktaking_report_export` | 全量查询 `get_stocktaking_report(..., 0, 0)` 后直接加载至内存。数据量大时极易触发 `Allowed memory size exhausted`。 | 采用游标查询、分块处理 (`chunk`) 或流式导出,避免一次性加载全部结果集。 | `// 建议 Export_model 内部实现分批查询,或在此处使用 while 循环配合 limit/offset 流式写入文件` | | 🟡 建议 | 多处 SQL 条件拼接 | `where_in` 与 `LIKE` 直接拼接数组/字符串。若 `Simple_model` 底层未使用预处理语句,存在 SQL 注入隐患。 | 确认 `Simple_model` 是否使用 PDO 预处理。若为原生拼接,请改用框架提供的 `like()`、`where_in()` 方法或手动绑定参数。 | `$this->db->like('info._goods_name', $params['goods_name'], 'both');` | ## 3. 总结与行动建议 ### 🚨 优先修复项(P0) 1. **移除顶层 `get_instance()`**:将其移至具体方法内部,或直接依赖 `$this->load->model()` 的自动加载机制。 2. **替换模型层 `exit()`**:所有业务中断逻辑必须改为 `throw new \Exception()` 或返回 `['success' => false, 'msg' => '...']`,确保框架能正常接管错误处理与事务回滚。 3. **修复时间比较逻辑**:将 `end_business_day_str` 统一通过 `strtotime()` 转为时间戳后再进行 `+ 86400*15` 运算与大小比较,杜绝隐式类型转换导致的逻辑漏洞。 ### 🛠 后续重构与优化方向 1. **事务规范化**:统一采用 `$this->db->trans_begin()` 配合 `try-catch` 显式控制事务,避免与框架自动事务机制冲突。建议在 `Simple_model` 基类中封装标准事务模板方法。 2. **类型安全与规范**:全面引入 PHP 7+ 类型声明(`int`, `string`, `array`, `bool`),类名与方法名严格遵循 PSR-12。可借助 `PHP_CodeSniffer` 或 `PHPStan` 进行静态扫描。 3. **大数据导出优化**:报表导出方法应改为流式处理。若 `Export_model` 不支持,可在此模型中实现 `yield` 生成器,按批次(如每次 500 条)查询并写入临时文件,彻底解决内存瓶颈。 4. **框架适配确认**:*注:代码特征高度符合 CodeIgniter 3 架构。若 `phpci` 为内部定制框架,请重点核对 `Simple_model` 的 `where_in`、`LIKE` 底层实现是否默认启用参数绑定,并查阅官方文档确认 `trans_*` 系列方法的生命周期行为。* > 💡 **审查局限性说明**:本次审查仅基于提供的单文件代码。`Simple_model`、`throwError`、`get_business_date` 等外部依赖的具体实现未提供,部分安全性与性能评估基于常规 CI 架构假设。建议结合完整项目上下文进行集成测试验证。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780985985
updated_unix
1780985985
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel