Skip to content

Commit 910a255

Browse files
Bruno Wowkste93cry
Bruno Wowk
authored andcommitted
Deprecate the replacement of all data of the scope's user context and allow merging instead (#931)
1 parent 3b09f81 commit 910a255

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Deprecate `Scope::setUser` behaviour of replacing user data. (#929)
6+
- Add the `$merge` parameter on `Scope::setUser` to allow merging user context. (#929)
57
- Fix remaining PHP 7.4 deprecations (#930)
68
- Make the `integrations` option accept a `callable` that will receive the list of default integrations and returns a customized list (#919)
79
- Add the `IgnoreErrorsIntegration` integration to deprecate and replace the `exclude_exceptions` option (#928)

src/State/Scope.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,21 @@ public function setExtras(array $extras): self
130130
/**
131131
* Sets the given data in the user context.
132132
*
133-
* @param array $data The data
133+
* @param array $data The data
134+
* @param bool $merge If true, $data will be merged into user context instead of replacing it
134135
*
135136
* @return $this
136137
*/
137-
public function setUser(array $data): self
138+
public function setUser(array $data, bool $merge = false): self
138139
{
140+
if ($merge) {
141+
$this->user->merge($data);
142+
143+
return $this;
144+
}
145+
146+
@trigger_error('Replacing the data is deprecated since version 2.3 and will stop working from version 3.0. Set the second argument to `true` to merge the data instead.', E_USER_DEPRECATED);
147+
139148
$this->user->replaceData($data);
140149

141150
return $this;

tests/State/ScopeTest.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testSetTag(): void
2929
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getTagsContext()->toArray());
3030
}
3131

32-
public function setTags(): void
32+
public function testSetTags(): void
3333
{
3434
$scope = new Scope();
3535
$scope->setTags(['foo' => 'bar']);
@@ -82,7 +82,12 @@ public function testSetExtras(): void
8282
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getExtraContext()->toArray());
8383
}
8484

85-
public function testSetUser(): void
85+
/**
86+
* @group legacy
87+
*
88+
* @expectedDeprecation Replacing the data is deprecated since version 2.3 and will stop working from version 3.0. Set the second argument to `true` to merge the data instead.
89+
*/
90+
public function testSetUserThrowsDeprecation(): void
8691
{
8792
$scope = new Scope();
8893

@@ -106,6 +111,30 @@ public function testSetUser(): void
106111
$this->assertSame(['bar' => 'baz'], $event->getUserContext()->toArray());
107112
}
108113

114+
public function testSetUser(): void
115+
{
116+
$scope = new Scope();
117+
118+
$event = $scope->applyToEvent(new Event(), []);
119+
120+
$this->assertNotNull($event);
121+
$this->assertSame([], $event->getUserContext()->toArray());
122+
123+
$scope->setUser(['foo' => 'bar'], true);
124+
125+
$event = $scope->applyToEvent(new Event(), []);
126+
127+
$this->assertNotNull($event);
128+
$this->assertSame(['foo' => 'bar'], $event->getUserContext()->toArray());
129+
130+
$scope->setUser(['bar' => 'baz'], true);
131+
132+
$event = $scope->applyToEvent(new Event(), []);
133+
134+
$this->assertNotNull($event);
135+
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getUserContext()->toArray());
136+
}
137+
109138
public function testSetFingerprint(): void
110139
{
111140
$scope = new Scope();
@@ -237,7 +266,7 @@ public function testClear(): void
237266
$scope->setFingerprint(['foo']);
238267
$scope->setExtras(['foo' => 'bar']);
239268
$scope->setTags(['bar' => 'foo']);
240-
$scope->setUser(['foobar' => 'barfoo']);
269+
$scope->setUser(['foobar' => 'barfoo'], true);
241270

242271
$event = $scope->applyToEvent(new Event(), []);
243272

@@ -273,7 +302,7 @@ public function testApplyToEvent(): void
273302
$scope->addBreadcrumb($breadcrumb);
274303
$scope->setTag('foo', 'bar');
275304
$scope->setExtra('bar', 'foo');
276-
$scope->setUser(['foo' => 'baz']);
305+
$scope->setUser(['foo' => 'baz'], true);
277306

278307
$event = $scope->applyToEvent($event, []);
279308

@@ -290,7 +319,7 @@ public function testApplyToEvent(): void
290319
$scope->setLevel(Severity::fatal());
291320
$scope->setTag('bar', 'foo');
292321
$scope->setExtra('foo', 'bar');
293-
$scope->setUser(['baz' => 'foo']);
322+
$scope->setUser(['baz' => 'foo'], true);
294323

295324
$event = $scope->applyToEvent($event, []);
296325

0 commit comments

Comments
 (0)