Skip to content

Updated phpstan 2.1#527

Merged
zds-s merged 2 commits into
masterfrom
patch-12
Jan 10, 2025
Merged

Updated phpstan 2.1#527
zds-s merged 2 commits into
masterfrom
patch-12

Conversation

@zds-s

@zds-s zds-s commented Jan 10, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • 依赖更新

    • 将 PHPStan 静态分析工具从版本 1.10 升级到 2.1
  • 服务类型注解调整

    • 更新了多个服务类的文档注解,明确指定服务关联的实体类型
    • 包括 AttachmentServiceUserServiceMenuService
  • 服务层逻辑微调

    • MenuService 中修改了按钮权限创建和更新的条件判断逻辑
    • 简化了对模型有效性的检查
  • 容器初始化优化

    • 简化了依赖注入容器的初始化流程,移除了不必要的类型检查

zds-s added 2 commits January 11, 2025 03:18
…endencies

- Simplify container initialization in config/container.php
- Update phpstan dependency from ^1.10 to ^2.1 in composer.json
- Adjust AttachmentService to use Attachment instead of AttachmentRepository
- Remove unnecessary ContainerInterface check
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 10, 2025
@coderabbitai

coderabbitai Bot commented Jan 10, 2025

Copy link
Copy Markdown
📝 Walkthrough

概述

演练

这个拉取请求包含了多个文件的修改,主要涉及中间件、服务类和配置文件。更改包括更新类型注解、修改条件逻辑、升级开发依赖版本以及简化容器初始化过程。这些修改旨在提高代码的类型安全性、简化逻辑和更新开发工具。

变更

文件 变更摘要
app/Http/Common/Middleware/OperationMiddleware.php $operator 的条件检查从 Generator::UNDEFINED 改为 null
app/Service/AttachmentService.php 更新类型注解从 IService<AttachmentRepository>IService<Attachment>
app/Service/IService.php 更新类型模板为 T of Model,修改 updateById 方法返回类型,添加 Model 导入
app/Service/Permission/MenuService.php 简化 createupdateById 方法的条件逻辑,更新类签名
app/Service/Permission/UserService.php 更新 PHPDoc 注解,从 UserRepository 改为 User
composer.json phpstan/phpstan 开发依赖版本从 ^1.10 升级到 ^2.1
config/container.php 简化容器初始化过程,移除类型检查和异常处理

诗歌

🐰 代码如春风,变化轻盈飘
类型更精准,逻辑更清晰
依赖版本跃,容器更轻巧
兔子微笑看,技术在跳跃
重构的魔法,悄然绽芳华 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or PR title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dosubot dosubot Bot added the dependencies Pull requests that update a dependency file label Jan 10, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
app/Http/Common/Middleware/OperationMiddleware.php (1)

Line range hint 1-93: 代码结构良好,实现规范

整体代码实现非常专业,值得肯定的几点:

  • 使用了依赖注入
  • 类型提示完善
  • 职责单一,只负责操作日志
  • 代码组织结构清晰

建议考虑添加以下改进:

  • 为关键方法添加更详细的文档注释
  • 考虑将事件分发逻辑抽取到单独的方法中
composer.json (1)

77-77: 建议:考虑添加 phpstan 基线

为了更顺利地过渡到 PHPStan 2.1,建议:

  1. 生成基线文件以忽略现有错误
  2. 逐步解决新版本发现的问题

可以通过以下方式生成基线:

 "scripts": {
   "analyse": "phpstan analyse --memory-limit 500M -l 5 -c phpstan.neon.dist ./app ./config",
+  "analyse-baseline": "phpstan analyse --memory-limit 500M -l 5 -c phpstan.neon.dist --generate-baseline phpstan-baseline.neon ./app ./config"
 }
config/container.php (1)

20-20: 建议添加类型提示

为了提高代码的可维护性和类型安全性,建议为返回值添加类型声明。

建议修改如下:

-return ApplicationContext::setContainer(new Container((new DefinitionSourceFactory())()));
+/** @return \Psr\Container\ContainerInterface */
+return ApplicationContext::setContainer(new Container((new DefinitionSourceFactory())()));
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ccd7dbb and e148a35.

📒 Files selected for processing (7)
  • app/Http/Common/Middleware/OperationMiddleware.php (1 hunks)
  • app/Service/AttachmentService.php (1 hunks)
  • app/Service/IService.php (2 hunks)
  • app/Service/Permission/MenuService.php (2 hunks)
  • app/Service/Permission/UserService.php (1 hunks)
  • composer.json (1 hunks)
  • config/container.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (10)
app/Http/Common/Middleware/OperationMiddleware.php (1)

Line range hint 63-72: 条件检查的改进更加清晰和直观

将条件检查从 Generator::UNDEFINED 改为 null 检查是一个很好的改进,使代码更加清晰和符合 PHP 的惯用法。

这种改变:

  • 提高了代码的可读性
  • 简化了逻辑判断
  • 减少了对外部常量的依赖
app/Service/IService.php (2)

17-17: 类型约束的改进

通过将泛型类型 T 限制为 Model 的子类型,提高了类型安全性。这种改变确保了所有继承 IService 的服务类都必须使用 Model 的子类作为其泛型参数。

Also applies to: 21-22


60-63: 返回类型注解的完善

updateById 方法的返回类型注解从 @return null|T 更新为 @return null|mixed|T,更准确地反映了可能的返回值类型。

app/Service/AttachmentService.php (1)

22-22: 泛型类型参数的优化

@extends 注解从 IService<AttachmentRepository> 更改为 IService<Attachment> 更准确地表达了服务类与数据模型的关系,符合 IService 中的类型约束要求。

app/Service/Permission/UserService.php (1)

23-23: 类型注解的统一更新

@extends 注解从 IService<UserRepository> 更改为 IService<User>,保持了与其他服务类的一致性,并更好地反映了服务类操作的实际数据类型。

app/Service/Permission/MenuService.php (2)

15-15: 类型定义的完善

添加了 Menu 模型的导入并通过 @extends IService<Menu> 明确了服务类的泛型类型参数,提高了代码的类型安全性。

Also applies to: 19-21


36-36: 验证按钮权限创建逻辑

create 方法中移除了对 $model 的检查,这可能会在模型创建失败的情况下导致意外行为。建议验证此更改的影响。

✅ Verification successful

按钮权限创建逻辑验证结果

代码分析显示移除对 $model 的显式检查是安全的。原因如下:

  • 父类的 create 方法会处理数据验证
  • 创建失败时会抛出异常而不是返回空值
  • 代码遵循仓储模式的最佳实践
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索是否存在其他地方对 create 方法返回值的检查
rg -A 5 "create\(" app/Service/Permission/

Length of output: 1314

composer.json (1)

77-77: 验证 PHPStan 2.1 的兼容性

PHPStan 从 1.x 升级到 2.x 是一个主要版本升级,可能包含破坏性更改。请确保:

  1. 检查与当前 PHP 版本(8.1+)的兼容性
  2. 验证是否需要更新 phpstan.neon.dist 配置文件
  3. 确认 CI 管道中的静态分析命令是否需要调整

运行以下脚本来验证兼容性和必要的配置更改:

✅ Verification successful

PHPStan 2.1 升级验证完成

经过验证,您的项目环境完全满足 PHPStan 2.1 的要求:

  • PHP 8.1+ 环境要求已满足
  • 现有的 phpstan.neon.dist 配置文件兼容且有效
  • 所有忽略规则和框架特定配置均可继续使用

您可以安全地进行此升级。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查 PHPStan 2.x 的兼容性要求和重大变更

# 验证 phpstan.neon.dist 是否存在
if [ -f "phpstan.neon.dist" ]; then
    echo "Found phpstan.neon.dist configuration file"
    cat phpstan.neon.dist
fi

# 检查静态分析相关的 CI 配置
fd -t f "^\.github.*yml$" -X cat

Length of output: 1288


Script:

#!/bin/bash
# 使用正确的标志搜索 GitHub 工作流文件
fd -H -t f "^\.github.*yml$" -X cat

# 搜索任何包含 phpstan 的文件
fd -H -t f "phpstan" 

# 检查 composer.json 中的 PHP 版本要求
cat composer.json | grep -A 5 "require"

Length of output: 440

config/container.php (2)

2-2: 空行使用恰当

代码结构清晰,空行的使用有助于提高可读性。


20-20: 验证容器初始化的可靠性

容器初始化逻辑已简化,移除了之前的错误处理。虽然代码更简洁,但需要确保在容器初始化失败时能够正确处理异常。

建议验证以下几点:

  1. 容器初始化失败时的异常处理机制
  2. 确保应用其他部分能够正确处理容器初始化异常
✅ Verification successful

容器初始化的异常处理机制已得到验证

经过验证,系统具备完善的异常处理机制:

  • AppExceptionHandler 作为全局异常处理器可捕获所有异常,包括容器初始化异常
  • 异常处理系统支持详细的错误日志记录和格式化的错误响应
  • 在开发环境下提供详细的调试信息,生产环境确保安全的错误输出

当前的容器初始化实现是安全可靠的,无需添加额外的错误处理逻辑。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:检查是否存在其他地方的异常处理机制

# 搜索与容器初始化相关的异常处理代码
rg -t php "catch.*ContainerException" 

# 检查是否有全局异常处理器
ast-grep --pattern 'class $_ implements ExceptionHandlerInterface {
  $$$
}'

Length of output: 118


Script:

#!/bin/bash
# 搜索异常处理相关代码
# 查找所有包含 Exception 的处理类
rg -t php "class.*Exception"

# 查找包含try-catch的容器相关代码
rg -t php "try\s*{.*?Container.*?}\s*catch" -U

# 查找启动相关文件中的错误处理
fd -e php "bootstrap|index|start|app" --exec rg -l "catch|Exception|error"

# 查找容器相关的异常类定义
ast-grep --pattern 'class $_ extends Exception {
  $$$
}'

Length of output: 1250


Script:

#!/bin/bash
# 检查主要异常处理文件的内容
echo "=== AppExceptionHandler 内容 ==="
cat app/Exception/Handler/AppExceptionHandler.php

echo -e "\n=== Bootstrap 文件中的错误处理 ==="
cat tests/bootstrap.php

echo -e "\n=== AbstractHandler 基类实现 ==="
cat app/Exception/Handler/AbstractHandler.php

Length of output: 6036

@zds-s
zds-s merged commit 881ce11 into master Jan 10, 2025
@codecov

codecov Bot commented Jan 10, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (ccd7dbb) to head (e148a35).
Report is 1 commits behind head on master.

Additional details and impacted files
@@      Coverage Diff      @@
##   master   #527   +/-   ##
=============================
=============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zds-s
zds-s deleted the patch-12 branch January 11, 2025 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Composer dependencies Pull requests that update a dependency file size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant