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 442 from issue
id
442
repo_id
21
index
157
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pay-260616 - 1
content
## 自动代码审查报告 **分支**: pay-260616 **提交**: `49815ee06
## 自动代码审查报告 **分支**: pay-260616 **提交**: `49815ee063afc9e9ce372297e070928d101fe75d` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-06-01 19:24:58 --- ## 1. 审查摘要 - **代码质量评分**:5/10 分 - **总体评价**:代码承载了复杂的门店预订时间计算与配置读取逻辑,业务覆盖全面。但整体呈现典型的“上帝类/上帝方法”特征,大量业务规则、时间计算、数据查询耦合在单一方法中。存在**实例状态污染**、**静态缓存失效**、**文件级非法代码**等严重缺陷。注:代码实际使用的是 `CodeIgniter 3` 语法架构(如 `$CI = &get_instance()`、`$this->load->model()`),而非 `phpci`,以下审查将基于 CI3 最佳实践与通用 PHP 规范进行。 - **风险等级**:🔴 高 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `Ahead_shop_book_time_info_model.php` 顶部 | 文件顶部在类定义外直接执行 `$CI = &get_instance();` 和 `$CI->load->model()`。在 PHP/CI3 中,模型文件不应包含类外可执行代码,会导致 Fatal Error 或加载时序异常。 | 移除文件顶部代码,将实例获取与依赖加载移至 `__construct()` 方法内。 | `// 删除顶部代码<br>class Ahead_shop_book_time_info_model extends Simple_model {<br> public function __construct() {<br> parent::__construct();<br> $this->load->model('Simple_model');<br> // ...<br> }<br>}` | | 🔴 严重 | `get_book_days_info` 方法内 | `$this->book_days += 1;` 直接修改了类的公共属性。若模型实例被复用(CI3 默认单例复用),多次调用会导致天数无限累加,引发严重业务逻辑错误。 | 使用局部变量计算,禁止修改类属性。若需扩展天数,应通过参数传递或返回新数组。 | `$days = $this->book_days + ($add_day ? 1 : 0);<br>for ($i = 0; $i < $days; $i++) { ... }` | | 🔴 严重 | `get_book_days_info` 方法内 | `self::$book_days_info` 静态缓存未区分 `$add_day` 参数。首次以 `$add_day=true` 调用后,缓存被写入;后续 `$add_day=false` 调用将直接返回错误缓存,导致日期状态错乱。 | 缓存 Key 需包含参数特征,或仅在 `$add_day=false` 时缓存。 | `$cache_key = 'book_days_' . ($add_day ? '1' : '0');<br>if (!empty(self::$book_days_info[$cache_key])) return self::$book_days_info[$cache_key];<br>// ...<br>self::$book_days_info[$cache_key] = $result;` | | 🟠 警告 | `get_book_day_time_info` 方法 | 方法体超 300 行,混合了 DB 查询、时间区间计算、套餐规则校验、状态标记、数组过滤等。违反单一职责原则,极难单元测试与维护。 | 按职责拆分:1. `fetchBookingData()` 2. `calculateTimeRanges()` 3. `applyVoucherRules()` 4. `formatResponse()`。引入独立的时间计算服务类。 | *(架构级建议,见第3节)* | | 🟠 警告 | `_get_un_book_time` 方法 | 频繁调用 `get_one()` 查询前一天/后一天数据,且未使用批量查询。在高并发预订场景下易引发 N+1 查询瓶颈。 | 使用 `where_in` 批量查询前后日期数据,或在内存中一次性加载多日缓存。 | `$dates = [$prev_date, $date, $next_date];<br>$this->db->where_in('_date', $dates);<br>$all_data = $this->get();` | | 🟠 警告 | `Ahead_shop_config_second_model.php` `get_shop_setting` | `switch` 语句超 200 行,大量分支逻辑高度重复(如 KTV/台球/棋牌/酒馆的退款、变更、时长限制)。违反开闭原则,新增场景需修改核心方法。 | 提取配置映射表或使用策略模式。将重复逻辑抽象为 `get_scene_setting($scene_prefix, $field, $default)`。 | `private function get_scene_setting($prefix, $field, $default) {<br> $key = $prefix . $field;<br> return $this->data[$key] ?? $default;<br>}` | | 🟠 警告 | 全局辅助函数依赖 | 大量使用 `throwError()`, `timeToHour()`, `hourToTime()`, `minToStr()` 等未声明命名空间的全局函数。若环境未加载对应 helper,将直接崩溃。 | 明确引入 `helper('custom_time')`,或封装为 `TimeHelper` 静态类。对关键函数添加 `function_exists()` 防御或依赖注入。 | `if (!function_exists('throwError')) { throw new \RuntimeException('Helper not loaded'); }` | | 🟡 建议 | 两个模型文件 | 属性全部声明为 `public`,且缺乏类型声明(PHP 7.4+ 支持)。外部可直接篡改 `$book_time_limit`、`$tuangou` 等核心状态,破坏封装性。 | 改为 `protected`/`private`,提供 `get/set` 方法。为属性添加类型提示(如 `public int $book_time_limit = 3600;`)。 | `protected int $book_time_limit = 3600;<br>public function getBookTimeLimit(): int { return $this->book_time_limit; }` | | 🟡 建议 | `Ahead_shop_config_second_model.php` | `case 'book_trial_time': $result = 0;//$data['book_trial_time'] ?? 0;` 存在注释掉的旧逻辑。`deal_audio_content_params` 中 `$params_map` 末尾有多余分号 `;;`。 | 清理死代码与语法冗余。遵循 PSR-12 规范,移除无用注释。 | `case 'book_trial_time':<br> $result = 0; // 251021版本已废弃<br> break;` | ## 3. 总结与行动建议 ### 🔑 优先修复的关键问题 1. **立即修复状态污染**:移除 `get_book_days_info` 中的 `$this->book_days += 1;`,改用局部变量。修复静态缓存 Key 未区分参数的问题,防止跨请求数据串扰。 2. **清理非法文件头代码**:将 `Ahead_shop_book_time_info_model.php` 顶部的 `$CI = &get_instance();` 及 `load->model()` 移入构造函数,确保符合 PHP 类加载规范。 3. **防御性编程**:对 `$this->tuangou` 等动态加载的库增加 `if (property_exists($this, 'tuangou'))` 或 `instanceof` 校验,避免未加载时触发 `Call to a member function on null` 致命错误。 ### 🛠 后续重构与优化方向 1. **架构拆分(SRP 原则)**: - 将 `get_book_day_time_info` 拆分为 **数据层**(查询预订记录、门店配置)、**规则层**(时长限制、套餐校验、跨天逻辑)、**表现层**(状态标记、格式化输出)。 - 建议引入 `BookingTimeCalculator` 服务类,专门处理时间区间交集、差集、合并算法(当前大量使用 `array_intersect`/`array_merge` 处理时间字符串,性能与可读性均不佳)。 2. **配置读取优化**: - `Ahead_shop_config_second_model` 的 `switch` 可重构为配置映射数组。例如: ```php protected $config_map = [ 'book_refund_time_limit' => ['status_field' => 'book_refund_time_limit_status', 'default' => 2], // ... ]; ``` - 利用 CI3 的 `Cache` 驱动(如 Redis/Memcached)缓存门店配置,替代仅存在于请求生命周期的 `static $shop_config`,降低 DB 压力。 3. **安全与规范**: - 所有外部传入的 `$params` 数组应在入口处进行类型校验与过滤(可使用 CI3 的 `Form_validation` 或自定义 DTO)。 - 统一时间处理:建议全局使用 `DateTimeImmutable` 或统一转换为 Unix 时间戳进行计算,避免 `YmdHi` 字符串比较带来的边界错误(如 `23:59` 与 `00:00` 跨天问题)。 - 若项目确需迁移至 `phpci` 或现代 PHP 框架,建议逐步将 `$CI = &get_instance()` 替换为依赖注入(DI)容器,并启用严格类型模式 `declare(strict_types=1);`。 > 💡 **局限性说明**:本次审查基于提供的两个模型文件。由于时间计算强依赖未提供的全局 Helper(如 `timeToHour`, `mergeTimeRanges`)及 `Tuangou` 库的内部实现,部分边界条件(如跨天套餐时长计算、并发锁机制)的准确性需结合完整上下文进行集成测试验证。建议补充单元测试覆盖核心时间区间算法。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780313098
updated_unix
1780313098
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel