Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/sentry/src/Switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function __construct(protected ConfigInterface $config)

public function isEnable(string $key): bool
{
return (bool) $this->config->get('sentry.enable.' . $key, false);
return (bool) $this->config->get('sentry.enable.' . $key, true);
}

public function isBreadcrumbEnable(string $key): bool
{
return (bool) $this->config->get('sentry.breadcrumbs.' . $key, false);
return (bool) $this->config->get('sentry.breadcrumbs.' . $key, true);
}

public function isTracingEnable(string $key): bool
Expand All @@ -38,7 +38,7 @@ public function isTracingEnable(string $key): bool
return false;
}

return (bool) $this->config->get('sentry.tracing.enable.' . $key, false);
return (bool) $this->config->get('sentry.tracing.enable.' . $key, true);
}

public function isTracingSpanEnable(string $key): bool
Expand All @@ -47,12 +47,12 @@ public function isTracingSpanEnable(string $key): bool
return false;
}

return (bool) $this->config->get('sentry.tracing.spans.' . $key, false);
return (bool) $this->config->get('sentry.tracing.spans.' . $key, true);
}

public function isTracingExtraTagEnable(string $key): bool
{
return (bool) ($this->config->get('sentry.tracing.extra_tags', [])[$key] ?? false);
return (bool) ($this->config->get('sentry.tracing.extra_tags', [])[$key] ?? true);
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

潜在致命错误:对非数组配置做数组下标访问会触发 TypeError

如果用户将 sentry.tracing.extra_tags 误配为布尔/字符串(这一类误配在实际环境并不少见),$this->config->get(... )[$key] 在 PHP 8+ 会报 “Cannot use array offset on type string/bool”。建议先归一为数组并做布尔规范化:

-        return (bool) ($this->config->get('sentry.tracing.extra_tags', [])[$key] ?? true);
+        $extraTags = $this->config->get('sentry.tracing.extra_tags', []);
+        $extraTags = is_array($extraTags) ? $extraTags : [];
+        $val = $extraTags[$key] ?? true;
+        return $this->toBool($val, true);

请补一条用例覆盖:extra_tags 配置为 'false'(字符串)与 false(布尔)时的行为。


为验证潜在 TypeError 问题,请打印 src/sentry/src/Switcher.php 文件第 45–75 行内容:


🏁 Script executed:

#!/bin/bash
set -e
# 打印 Switcher.php 文件第 45-75 行以查看上下文
sed -n '45,75p' src/sentry/src/Switcher.php

Length of output: 900


为确认 toBool 方法和 config 属性类型,请打印 Switcher.php 中相关定义:


🏁 Script executed:

#!/bin/bash
set -e
# 查找 toBool 方法定义
rg -n "function toBool" -C3 src/sentry/src/Switcher.php || echo "未找到 toBool 方法"
# 查找 config 属性声明
rg -n "private .*config" -C3 src/sentry/src/Switcher.php || echo "未找到 config 属性"
# 查找构造函数,确认注入类型
rg -n "function __construct" -C3 src/sentry/src/Switcher.php || echo "未找到 __construct"

Length of output: 381


确保 isTracingExtraTagEnable 安全处理 extra_tags 并添加测试

  • 先将配置值归一为数组:
    $extraTags = $this->config->get('sentry.tracing.extra_tags', []);
    if (! is_array($extraTags)) {
        $extraTags = [];
    }
  • 根据键取值并做布尔规范化:可直接用 PHP 内置 filter_var
    $val = $extraTags[$key] ?? true;
    return filter_var($val, FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => true]]);
    如需支持更多字符串(如 "off"/"no"),可自行实现 toBool(mixed $val, bool $default): bool 辅助方法。
  • 补充单元测试,覆盖当 sentry.tracing.extra_tags 被误配置为布尔或字符串(如 false'false')时的行为。
🤖 Prompt for AI Agents
In src/sentry/src/Switcher.php around line 55, isTracingExtraTagEnable currently
assumes the config value is an array and casts the lookup result directly to
bool; change it to first normalize the config value to an array (if
get('sentry.tracing.extra_tags', []) returns non-array, set to []), then read
$val = $extraTags[$key] ?? true and normalize to boolean using PHP's filter_var
with FILTER_VALIDATE_BOOLEAN and default true (or call a small toBool helper
that supports values like "off"/"no"); also add unit tests covering cases where
sentry.tracing.extra_tags is a boolean, string ('false', 'off'), null, and a
proper array to assert correct boolean normalization.

}

public function isExceptionIgnored(string|Throwable $exception): bool
Expand Down
6 changes: 3 additions & 3 deletions tests/Sentry/SwitcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'extra_tags' => [
'foo.bar' => true,
'foo.baz' => true,
'foo.bar.baz' => true,
'foo.bar.baz' => false,
],
],
],
Expand All @@ -34,6 +34,6 @@
})->with([
['foo.bar', true],
['foo.baz', true],
['foo.bar.baz', true],
['foo.bay', false],
['foo.bar.baz', false],
['foo.bay', true],
]);