Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Sep 29, 2025

Summary

  • Simplified transaction null checks by replacing explicit null validation with PHP 8's nullsafe operator (?->)
  • Improved code readability while maintaining the same logical behavior
  • Updated 6 locations across CoroutineAspect and EventHandleListener classes

Changes Made

  • CoroutineAspect.php: Updated transaction check in process() method
  • EventHandleListener.php: Updated transaction checks in 5 methods:
    • onBeforeHandle()
    • onAfterHandle()
    • onBeforeWrite()
    • onAfterWrite()
    • onBeforeFlush()

Technical Details

Before:

if (! $transaction || ! $transaction->getSampled()) {
    return;
}

After:

if (! $transaction?->getSampled()) {
    return;
}

The nullsafe operator automatically handles the null check, making the code more concise and leveraging PHP 8's modern syntax while preserving identical runtime behavior.

Test Plan

  • Code maintains existing logic (null transaction or unsampled transaction both result in early return)
  • All existing tests should continue to pass
  • No functional changes to tracing behavior

Summary by CodeRabbit

  • 重构
    • 统一使用空安全检查处理缺失或未采样的事务,精简条件分支并保持原有早退与可观测行为一致。
    • 提升追踪逻辑的健壮性与容错性,减少在无活动事务时的潜在报错日志,不影响现有功能与默认追踪策略。

…rator

Replace explicit null checks with PHP 8's nullsafe operator (?->) for cleaner and more concise transaction validation. This change maintains the same logic while improving code readability by combining null and method call checks in a single expression.

Changes:
- CoroutineAspect: Simplified transaction check in process method
- EventHandleListener: Updated 5 methods to use nullsafe operator for transaction validation
@coderabbitai
Copy link

coderabbitai bot commented Sep 29, 2025

Walkthrough

在协程切面与事件监听器中,将对事务是否存在与是否采样的双重判断,统一替换为使用 PHP 空安全操作符的单一判断:由 if (!$transaction || !$transaction->getSampled()) 改为 if (!$transaction?->getSampled())。未变更公开接口声明。

Changes

Cohort / File(s) Summary of Changes
Nullsafe 采样判断统一化
src/sentry/src/Tracing/Aspect/CoroutineAspect.php, src/sentry/src/Tracing/Listener/EventHandleListener.php
将所有基于事务存在性与采样状态的判断改为空安全调用:!$transaction?->getSampled();维持原有早退逻辑,避免对空对象调用方法。涉及多个事件处理方法(命令、Crontab、AMQP、Kafka、异步队列等)。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant E as EventHandler
  participant C as CurrentHub
  participant T as Transaction

  Note over E: 处理事件时
  E->>C: getCurrentTransaction()
  C-->>E: transaction 或 null

  alt 未采样或无事务
    E->>E: if (!transaction?->getSampled()) return
    Note over E: 直接返回,跳过追踪
  else 已采样事务
    E->>E: 继续处理/记录span等
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • xuanyanwow

Poem

小兔执笔改一行,
空安全问采样香。
事务若无轻跃过,
若已采样继续忙。
代码如风无声响,
稳稳跳坑踏春光。 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 此 PR 标题简洁清晰地指出了对 Sentry 模块的重构,聚焦于通过 PHP 8 的 nullsafe 操作符来简化事务空检查。它准确总结了更改的核心意图,让团队成员在浏览提交历史时能够快速理解主要改动。
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/sentry-simplify-transaction-checks

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.1.28)

At least one path must be specified to analyse.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@huangdijia huangdijia merged commit 093e6a7 into main Sep 29, 2025
16 of 17 checks passed
@huangdijia huangdijia deleted the refactor/sentry-simplify-transaction-checks branch September 29, 2025 08:27
huangdijia added a commit that referenced this pull request Sep 29, 2025
…rator (#942)

Replace explicit null checks with PHP 8's nullsafe operator (?->) for cleaner and more concise transaction validation. This change maintains the same logic while improving code readability by combining null and method call checks in a single expression.

Changes:
- CoroutineAspect: Simplified transaction check in process method
- EventHandleListener: Updated 5 methods to use nullsafe operator for transaction validation

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (1)

64-66: 使用 nullsafe 运算符合并判空与采样检查,语义等价且更简洁。

  • if (! $transaction?->getSampled()) 在 $transaction 为 null 或 getSampled() 为 false/null 时均为真,等价于原先的双条件早退逻辑。
  • 建议将上一行注释从“没有活动事务则跳过”更新为“没有活动事务或未采样则跳过”,以与逻辑完全一致。
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e0daefc and 694a14c.

📒 Files selected for processing (2)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (1 hunks)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (5 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/*/src/**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use the namespace pattern FriendsOfHyperf{ComponentName} in all component PHP source files

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
{src,tests}/**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Adhere to PSR-12 coding standards across PHP code

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
src/*/**

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

New components must follow the standard component structure under src/{component-name}/ including .gitattributes, .github, LICENSE, README.md, composer.json, and a src/ subdirectory

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7

Comment on lines +407 to 409
if (! $transaction?->getSampled()) {
return;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

统一使用 $transaction?->getSampled() 判空+采样检查,保持原有早退行为。

  • 改动保持功能不变,避免在 $transaction 为空时调用方法的风险,符合代码库现有风格(PSR-12 间距也保持一致)。
  • 请确认组件(及 monorepo 根)composer 最低 PHP 版本约束≥8.0,以确保 nullsafe 语法在分发环境无兼容风险。

运行以下脚本快速检查仓库中各 composer.json 的 PHP 版本约束:

Also applies to: 520-522, 588-590, 656-658, 707-709


🏁 Script executed:

#!/bin/bash
set -euo pipefail
echo "Composer PHP 版本约束扫描:"
fd -a composer.json | while read -r f; do
  req=$(jq -r '.require.php // empty' "$f" 2>/dev/null || true)
  if [ -z "$req" ]; then
    req=$(rg -nP '"php"\s*:\s*"[^"]+"' "$f" -N -S -o | sed -E 's/.*"php"\s*:\s*"([^"]+)".*/\1/' || true)
  fi
  printf "%s => %s\n" "$f" "${req:-<未声明>}"
done

Length of output: 3161


在 src/sentry/composer.json 中添加 PHP 最低版本约束

  • 根据 monorepo 根 composer.json (>=8.1),请在 src/sentry/composer.jsonrequire 段中增加 "php": ">=8.1" 以确保 nullsafe 操作符(PHP 8.0+)在分发环境可用。
🤖 Prompt for AI Agents
In src/sentry/src/Tracing/Listener/EventHandleListener.php around lines 407 to
409, the code uses the nullsafe operator but src/sentry/composer.json lacks a
PHP version constraint; open src/sentry/composer.json and add or update the
"require" section to include "php": ">=8.1" (matching the monorepo root), then
run composer validate and composer update (or composer install) to regenerate
lock files so the package declares PHP >=8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants