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 379 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-260519 - 1
TEXT
content
## 自动代码审查报告 **分支**: pay-260519 **提交**: `1297c97575e9eb332e955aaa4f63a84310cf42eb` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-05-27 16:37:11 --- ## 1. 审查摘要 - **代码质量评分**:5.5 / 10 分 - **总体评价**:代码实现了复杂的包厢预订时段计算逻辑,涵盖跨天营业、团购券校验、最低时长限制、清扫缓冲等业务场景。但存在明显的架构设计缺陷:静态缓存滥用导致状态污染、方法职责过重、时间计算缺乏时区安全、强依赖全局超对象且缺乏输入校验。整体可维护性与健壮性较低,需进行结构性重构。 - **风险等级**:🔴 高(静态缓存脏数据、时间计算边界漏洞、方法过长导致逻辑难以追踪) > 📌 **注**:代码呈现典型的 **CodeIgniter 3** 架构特征(如 `get_instance()`、`$this->load->model()`)。若 `phpci` 为内部定制框架,请确认其生命周期与 CI 一致。以下审查基于 CI/PHP 通用最佳实践。此外,**文件末尾代码被截断**,部分逻辑(如下一天不可用时间的完整计算)无法全面评估。 --- ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | 约 145-150 行 | **静态缓存状态污染**:`self::$book_days_info` 等静态变量在首次赋值后直接返回,未区分 `$merchant_id`、`$shop_id`、`$add_day` 等参数。同一请求处理不同门店/日期时将返回错误数据。 | 改为实例级缓存或带复合键的静态缓存,或在方法入口清空/校验缓存键。 | `private static $book_days_cache = [];<br>public function get_book_days_info(...) {<br> $cache_key = md5(implode('_', func_get_args()));<br> if (isset(self::$book_days_cache[$cache_key])) return self::$book_days_cache[$cache_key];<br> // ... 计算逻辑 ...<br> return self::$book_days_cache[$cache_key] = $result;<br>}` | | 🔴 严重 | 约 200-210 行 | **静态门店数据缓存未区分门店**:`self::$shop_data` 仅缓存首次查询的门店配置。若同请求切换 `$shop_id`,将复用错误营业时间,导致时段计算全面错乱。 | 移除静态缓存,或改为 `self::$shop_data[$shop_id]` 键值对缓存。 | `if (!isset(self::$shop_data[$shop_id])) {<br> self::$shop_data[$shop_id] = $this->ahead_shop_model->get_one(...);<br>}<br>$shop_data = self::$shop_data[$shop_id];` | | 🟠 警告 | 全文多处 | **频繁调用 `get_instance()`**:每次调用都会产生函数开销,且破坏 OOP 封装。CI 框架推荐在构造函数中绑定一次。 | 在类属性中声明 `$ci`,构造函数中赋值,后续统一使用 `$this->ci`。 | `private $ci;<br>public function __construct() {<br> parent::__construct();<br> $this->ci =& get_instance();<br>}` | | 🟠 警告 | 约 250-400 行 | **时间计算未处理时区/夏令时**:大量使用 `strtotime()`、`date()` 及硬编码 `86400` 进行加减。在跨天、夏令时切换或服务器时区变更时极易产生 `±1小时` 偏差。 | 统一使用 `DateTimeImmutable` 或 `Carbon` 库,显式指定时区,避免直接操作时间戳。 | `$dt = new DateTimeImmutable($date, new DateTimeZone('Asia/Shanghai'));<br>$end = $dt->modify('+1 day')->setTime(0,0);` | | 🟠 警告 | 约 280-350 行 | **魔法数字与状态码硬编码**:`'1'`、`'-1'`、`86400`、`3600`、`'7'` 等散落各处,语义不明且极易在后续维护中引发逻辑错误。 | 提取为类常量,统一命名规范。 | `const STATUS_AVAILABLE = '1';<br>const STATUS_UNAVAILABLE = '-1';<br>const SECONDS_PER_DAY = 86400;` | | 🟠 警告 | 约 230-550 行 | **方法职责过重(SRP 违反)**:`get_book_day_time_info` 超过 300 行,混合了数据查询、时间区间合并、套餐校验、状态标记、前端展示字段组装。难以测试与维护。 | 拆分为独立方法:`fetchBookingData()`、`calculateUnavailableSlots()`、`applyPackageRules()`、`formatTimeSlots()`。 | 见下方重构建议 | | 🟡 建议 | 约 300 行 | **数组解包潜在风险**:`array_intersect(...array_values($all_room_book_time))` 在数组为空时会抛出 `ArgumentCountError`。虽有 `count > 1` 保护,但边界仍脆弱。 | 使用 `array_reduce` 或显式循环求交集,或确保数组非空。 | `if (count($all_room_book_time) > 1) {<br> $un_book_time = array_intersect(...array_values($all_room_book_time));<br>} else {<br> $un_book_time = reset($all_room_book_time) ?? [];<br>}` | | 🟡 建议 | 约 100-130 行 | **输入参数缺乏校验**:`$params` 直接用于数据库查询与逻辑分支,未校验类型、必填项或合法范围。 | 增加前置校验或使用 CI 的 `form_validation`/自定义验证器。 | `if (empty($params['merchant_id']) || empty($params['shop_id'])) {<br> throwError('缺少必要参数');<br>}` | | 🟡 建议 | 全文 | **PSR-12 规范与注释缺失**:属性声明混杂、部分方法无 PHPDoc、长行未换行、全局函数(如 `throwError`、`mergeTimeRanges`)依赖隐式加载。 | 遵循 PSR-12 格式化,补充 `@param`、`@return`、`@throws`,显式加载依赖或使用 DI。 | 使用 `php-cs-fixer` 自动格式化,补充类型声明:`public function get_book_days_info(int $merchant_id, int $shop_id, string $check_date = '', bool $add_day = false): array` | --- ## 3. 总结与行动建议 ### 🔑 优先修复的关键问题 1. **静态缓存污染**:立即修复 `self::$book_days_info` 和 `self::$shop_data` 的缓存策略。这是导致线上“时段显示错乱/不可预订”的高频根因。 2. **时间计算安全**:将所有 `strtotime` + 秒数加减的逻辑替换为 `DateTimeImmutable` 或 `Carbon`,并统一时区配置(建议在 `index.php` 或框架入口设置 `date_default_timezone_set('Asia/Shanghai')`)。 3. **魔法数字常量化**:将 `'1'/'-1'`、`86400`、`3600` 等提取为类常量,提升代码可读性与可维护性。 ### 🛠 后续重构与优化方向 1. **拆分巨型方法**:将 `get_book_day_time_info` 按职责拆分为 4~5 个私有方法。例如: ```php private function calculateBaseUnavailableSlots($params): array private function applyPackageConstraints(array &$slots): void private function applyBusinessHoursConstraints(array &$slots): void private function formatResponseSlots(array $slots): array ``` 2. **引入策略模式处理规则**:当前大量 `if ($this->tuangou->verify_token)`、`if ($this->ignore_tuangou_time_limit)` 嵌套。建议抽象为 `BookingRuleInterface`,通过工厂或配置动态加载规则链,避免 `if-else` 爆炸。 3. **依赖注入替代全局加载**:逐步将 `$this->load->model()` 和 `$this->load->library()` 移至构造函数或通过 Service Container 注入,便于单元测试与 Mock。 4. **性能优化**: - 对 `array_intersect`、`array_merge` 等高频数组操作,若数据量 > 1000,建议改用位运算或预计算索引。 - 考虑将 `prev_date` 和 `next_date` 的数据库查询合并为 `WHERE _date IN (...)` 批量查询,减少 DB 往返次数。 5. **补充单元测试**:针对跨天营业、套餐时长不足、清扫缓冲、不可用星期等边界场景编写 PHPUnit 用例,确保重构不破坏核心业务逻辑。 > 💡 **提示**:由于代码在 `$this->next_date_room_book_time = [...]` 处截断,后续关于“下一天不可用时间”的计算逻辑未完整展示。建议在完整提交后补充审查,重点关注跨天时间区间的合并算法与边界条件处理。 --- *此 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