Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow numeric value in tags. #858

Merged
merged 2 commits into from
Jul 30, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix `TypeError` in `Sentry\Monolog\Handler` when the extra data array has numeric keys (#833).
- Changed type hint for both parameter and return value of `HubInterface::getCurrentHub` and `HubInterface::setCurrentHub()` methods (#849)
- Add the `setTags`, `setExtras` and `clearBreadcrumbs` methods to the `Scope` class (#852)
- Silently cast numeric values to strings when trying to set the tags instead of throwing (#858)

## 2.1.1 (2019-06-13)

Expand Down
5 changes: 3 additions & 2 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public function merge(array $data, bool $recursive = false): void
}

/**
* Sets the given data into this object.
* Sets each element of the array to the value of the corresponding key in
* the given input data.
*
* @param array $data
* @param array $data The data to set
*/
public function setData(array $data): void
{
Expand Down
53 changes: 32 additions & 21 deletions src/Context/TagsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,63 @@ public function merge(array $data, bool $recursive = false): void
throw new \InvalidArgumentException('The tags context does not allow recursive merging of its data.');
}

foreach ($data as $value) {
if (!\is_string($value)) {
throw new \InvalidArgumentException('The $data argument must contains a simple array of string values.');
}
}

parent::merge($data);
parent::merge(self::sanitizeData($data));
}

/**
* {@inheritdoc}
*/
public function setData(array $data): void
{
foreach ($data as $value) {
if (!\is_string($value)) {
throw new \InvalidArgumentException('The $data argument must contains a simple array of string values.');
}
}

parent::setData($data);
parent::setData(self::sanitizeData($data));
}

/**
* {@inheritdoc}
*/
public function replaceData(array $data): void
{
foreach ($data as $value) {
if (!\is_string($value)) {
throw new \InvalidArgumentException('The $data argument must contains a simple array of string values.');
}
}

parent::replaceData($data);
parent::replaceData(self::sanitizeData($data));
}

/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value): void
{
if (is_numeric($value)) {
$value = (string) $value;
}

if (!\is_string($value)) {
throw new \InvalidArgumentException('The $value argument must be a string.');
}

parent::offsetSet($offset, $value);
}

/**
* Sanitizes the given data by converting numeric values to strings.
*
* @param array $data The data to sanitize
*
* @return array
*
* @throws \InvalidArgumentException If any of the values of the input data
* is not a number or a string
*/
private static function sanitizeData(array $data): array
{
foreach ($data as &$value) {
if (is_numeric($value)) {
$value = (string) $value;
}

if (!\is_string($value)) {
throw new \InvalidArgumentException('The $data argument must contains a simple array of string values.');
}
}

return $data;
}
}
4 changes: 2 additions & 2 deletions tests/Context/TagsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function mergeDataProvider()
{
return [
[
['foo' => 'baz', 'baz' => 'foo'],
['foo' => 'baz', 'baz' => 'foo', 'int' => 1, 'float' => 1.1],
false,
['foo' => 'baz', 'bar' => 'foo', 'baz' => 'foo'],
['foo' => 'baz', 'bar' => 'foo', 'baz' => 'foo', 'int' => '1', 'float' => '1.1'],
null,
],
[
Expand Down