Skip to content
Draft
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
85 changes: 10 additions & 75 deletions src/DataCollection/DataCollectionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@
* stack_frame_variables: KeyValueCollectionBehavior,
* frame_context_lines: int
* }
*
* @phpstan-implements \ArrayAccess<
* key-of<ResolvedDataCollectionOptions>,
* value-of<ResolvedDataCollectionOptions>
* >
*
* @mago-ignore analysis:missing-template-parameter
*/
final class DataCollectionOptions implements \ArrayAccess
final class DataCollectionOptions
{
private const COLLECTION_MODES = [
'off',
Expand All @@ -43,14 +36,16 @@ final class DataCollectionOptions implements \ArrayAccess
'terms' => [],
];

/**
* @internal
*/
public const HTTP_BODY_INCOMING_REQUEST = 'incomingRequest';
public const HTTP_BODY_OUTGOING_REQUEST = 'outgoingRequest';
public const HTTP_BODY_INCOMING_RESPONSE = 'incomingResponse';
public const HTTP_BODY_OUTGOING_RESPONSE = 'outgoingResponse';

public const HTTP_BODY_TYPES = [
'incomingRequest',
'outgoingRequest',
'incomingResponse',
'outgoingResponse',
self::HTTP_BODY_INCOMING_REQUEST,
self::HTTP_BODY_OUTGOING_REQUEST,
self::HTTP_BODY_INCOMING_RESPONSE,
self::HTTP_BODY_OUTGOING_RESPONSE,
];

private const DEFAULTS = [
Expand Down Expand Up @@ -253,66 +248,6 @@ public function setFrameContextLines(int $frameContextLines): self
return $this->updateOptions(['frame_context_lines' => $frameContextLines]);
}

/**
* @param mixed $offset
*/
public function offsetExists($offset): bool
{
return \is_string($offset) && \array_key_exists($offset, $this->options);
}

/**
* @phpstan-template TKey of key-of<ResolvedDataCollectionOptions>
*
* @param mixed $offset
*
* @phpstan-param TKey $offset
*
* @return mixed
*
* @phpstan-return ResolvedDataCollectionOptions[TKey]
*
* @mago-ignore analysis:incompatible-parameter-type
* @mago-ignore analysis:invalid-return-statement
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
/** @phpstan-ignore-next-line Runtime access to unknown offsets is intentionally non-throwing. */
return null;
}

return $this->options[$offset];
}

/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
if (!\is_string($offset)) {
return;
}

$this->updateOptions([$offset => $value]);
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset): void
{
if (!\is_string($offset) || !\array_key_exists($offset, self::DEFAULTS)) {
return;
}

/** @var mixed $default */
$default = self::DEFAULTS[$offset];
$this->updateOptions([$offset => $default]);
}

private function configureOptions(OptionsResolver $resolver): void
{
$resolver->setAllowedTypes('user_info', 'bool');
Expand Down
69 changes: 69 additions & 0 deletions src/DataCollection/DataCollectionPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Sentry\DataCollection;

use Sentry\Options;
use Sentry\State\HubInterface;

/**
* Provides a live view of the effective data collection configuration.
*
* A null data_collection option intentionally selects the legacy
* send_default_pii behavior until the next major-version migration.
*
* This is shared infrastructure for first-party SDK integrations. It is
* public in PHP terms so framework SDKs can use it without internal-API
* diagnostics, but it is not a primary end-user configuration surface.
*/
final class DataCollectionPolicy
{
/**
* @var Options|null
*/
private $options;

private function __construct(?Options $options)
{
$this->options = $options;
}

public static function fromHub(HubInterface $hub): self
{
$client = $hub->getClient();

return new self($client === null ? null : $client->getOptions());
}

public static function fromOptions(?Options $options): self
{
return new self($options);
}

public function getOptions(): ?Options
{
return $this->options;
}

public function getDataCollection(): ?DataCollectionOptions
{
return $this->options === null ? null : $this->options->getDataCollection();
}

public function isLegacyMode(): bool
{
return $this->getDataCollection() === null;
}

public function shouldCollectUserInfo(): bool
{
$dataCollection = $this->getDataCollection();

if ($dataCollection !== null) {
return $dataCollection->shouldCollectUserInfo();
}

return $this->options !== null && $this->options->shouldSendDefaultPii();
}
}
Loading