Skip to content

Commit 20917ce

Browse files
derrabusnicolas-grekas
authored andcommitted
Remove the default values from setters with a nullable parameter.
1 parent 2be15a2 commit 20917ce

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

Authentication/Token/Storage/TokenStorage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function getToken(): ?TokenInterface
3939

4040
public function setToken(TokenInterface $token = null)
4141
{
42+
if (1 > \func_num_args()) {
43+
trigger_deprecation('symfony/security-core', '6.2', 'Calling "%s()" without any arguments is deprecated. Please explicitly pass null if you want to unset the token.', __METHOD__);
44+
}
45+
4246
if ($token) {
4347
// ensure any initializer is called
4448
$this->getToken();

Authentication/Token/Storage/TokenStorageInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ public function getToken(): ?TokenInterface;
3030
*
3131
* @param TokenInterface|null $token A TokenInterface token, or null if no further authentication information should be stored
3232
*/
33-
public function setToken(TokenInterface $token = null);
33+
public function setToken(?TokenInterface $token);
3434
}

Authentication/Token/Storage/UsageTrackingTokenStorage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function getToken(): ?TokenInterface
4646

4747
public function setToken(TokenInterface $token = null): void
4848
{
49+
if (1 > \func_num_args()) {
50+
trigger_deprecation('symfony/security-core', '6.2', 'Calling "%s()" without any arguments is deprecated. Please explicitly pass null if you want to unset the token.', __METHOD__);
51+
}
52+
4953
$this->storage->setToken($token);
5054

5155
if ($token && $this->shouldTrackUsage()) {

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ CHANGELOG
44
6.2
55
---
66

7-
* Deprecate the `Security` class, use `Symfony\Bundle\SecurityBundle\Security\Security` instead
7+
* Deprecate the `Security` class, use `Symfony\Bundle\SecurityBundle\Security\Security` instead
8+
* Change the signature of `TokenStorageInterface::setToken()` to `setToken(?TokenInterface $token)`
9+
* Deprecate calling `TokenStorage::setToken()` or `UsageTrackingTokenStorage::setToken()` without arguments
810

911
6.0
1012
---

Tests/Authentication/Token/Storage/TokenStorageTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,39 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authentication\Token\Storage;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1617
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1718
use Symfony\Component\Security\Core\User\InMemoryUser;
1819

1920
class TokenStorageTest extends TestCase
2021
{
22+
use ExpectDeprecationTrait;
23+
24+
/**
25+
* @group legacy
26+
*/
27+
public function testGetSetTokenLegacy()
28+
{
29+
$tokenStorage = new TokenStorage();
30+
$token = new UsernamePasswordToken('username', 'password', 'provider');
31+
$tokenStorage->setToken($token);
32+
$this->assertSame($token, $tokenStorage->getToken());
33+
34+
$this->expectDeprecation('Since symfony/security-core 6.2: Calling "Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::setToken()" without any arguments is deprecated. Please explicitly pass null if you want to unset the token.');
35+
36+
$tokenStorage->setToken();
37+
$this->assertNull($tokenStorage->getToken());
38+
}
39+
2140
public function testGetSetToken()
2241
{
2342
$tokenStorage = new TokenStorage();
2443
$this->assertNull($tokenStorage->getToken());
2544
$token = new UsernamePasswordToken(new InMemoryUser('username', 'password'), 'provider');
2645
$tokenStorage->setToken($token);
2746
$this->assertSame($token, $tokenStorage->getToken());
47+
$tokenStorage->setToken(null);
48+
$this->assertNull($tokenStorage->getToken());
2849
}
2950
}

0 commit comments

Comments
 (0)