Skip to content

Commit b046e99

Browse files
[HttpFoundation] Deprecate Request::get() in favor of using properties ->attributes, query or request directly
1 parent 6764b99 commit b046e99

File tree

8 files changed

+32
-55
lines changed

8 files changed

+32
-55
lines changed

AccessToken/FormEncodedBodyExtractor.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public function __construct(
3434

3535
public function extractAccessToken(Request $request): ?string
3636
{
37-
if (
38-
Request::METHOD_POST !== $request->getMethod()
39-
|| !str_starts_with($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
40-
) {
37+
if ('POST' !== $request->getMethod() || !str_starts_with($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')) {
4138
return null;
4239
}
4340
$parameter = $request->request->get($this->parameter);

Authenticator/LoginLinkAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function supports(Request $request): ?bool
5151

5252
public function authenticate(Request $request): Passport
5353
{
54-
if (!$username = $request->get('user')) {
54+
if (!$username = $request->query->get('user') ?? (!\in_array($request->getMethod(), ['GET', 'HEAD'], true) ? $request->request->get('user') : null)) {
5555
throw new InvalidLoginLinkAuthenticationException('Missing user from link.');
5656
}
5757

Firewall/SwitchUserListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(
6565
public function supports(Request $request): ?bool
6666
{
6767
// usernames can be falsy
68-
$username = $request->get($this->usernameParameter);
68+
$username = $request->query->get($this->usernameParameter) ?? (!\in_array($request->getMethod(), ['GET', 'HEAD'], true) ? $request->request->get($this->usernameParameter) : null);
6969

7070
if (null === $username || '' === $username) {
7171
$username = $request->headers->get($this->usernameParameter);

HttpUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public function createRequest(Request $request, string $path): Request
8888
$newRequest->attributes->set(SecurityRequestAttributes::LAST_USERNAME, $request->attributes->get(SecurityRequestAttributes::LAST_USERNAME));
8989
}
9090

91-
if ($request->get('_format')) {
92-
$newRequest->attributes->set('_format', $request->get('_format'));
91+
if ($request->attributes->has('_format')) {
92+
$newRequest->attributes->set('_format', $request->attributes->get('_format'));
9393
}
9494
if ($request->getDefaultLocale() !== $request->getLocale()) {
9595
$newRequest->setLocale($request->getLocale());

LoginLink/LoginLinkHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Security\Core\User\UserProviderInterface;
2323
use Symfony\Component\Security\Http\LoginLink\Exception\ExpiredLoginLinkException;
2424
use Symfony\Component\Security\Http\LoginLink\Exception\InvalidLoginLinkException;
25+
use Symfony\Component\Security\Http\ParameterBagUtils;
2526

2627
/**
2728
* @author Ryan Weaver <ryan@symfonycasts.com>
@@ -79,16 +80,16 @@ public function createLoginLink(UserInterface $user, ?Request $request = null, ?
7980

8081
public function consumeLoginLink(Request $request): UserInterface
8182
{
82-
$userIdentifier = $request->get('user');
83+
$userIdentifier = ParameterBagUtils::getRequestParameterValue($request, 'user');
8384

84-
if (!$hash = $request->get('hash')) {
85+
if (!$hash = ParameterBagUtils::getRequestParameterValue($request, 'hash')) {
8586
throw new InvalidLoginLinkException('Missing "hash" parameter.');
8687
}
8788
if (!\is_string($hash)) {
8889
throw new InvalidLoginLinkException('Invalid "hash" parameter.');
8990
}
9091

91-
if (!$expires = $request->get('expires')) {
92+
if (!$expires = ParameterBagUtils::getRequestParameterValue($request, 'expires')) {
9293
throw new InvalidLoginLinkException('Missing "expires" parameter.');
9394
}
9495
if (!preg_match('/^\d+$/', $expires)) {

ParameterBagUtils.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ public static function getParameterBagValue(ParameterBag $parameters, string $pa
6262
*/
6363
public static function getRequestParameterValue(Request $request, string $path, array $parameters = []): mixed
6464
{
65+
$get = static fn ($path) => $request->attributes->get($path) ?? $request->query->all()[$path] ?? (!\in_array($request->getMethod(), ['GET', 'HEAD'], true) ? $request->request->all()[$path] ?? null : null);
66+
6567
if (false === $pos = strpos($path, '[')) {
66-
return $parameters[$path] ?? $request->get($path);
68+
return $parameters[$path] ?? $get($path);
6769
}
6870

6971
$root = substr($path, 0, $pos);
7072

71-
if (null === $value = $parameters[$root] ?? $request->get($root)) {
73+
if (null === $value = $parameters[$root] ?? $get($root)) {
7274
return null;
7375
}
7476

@@ -77,7 +79,7 @@ public static function getRequestParameterValue(Request $request, string $path,
7779
try {
7880
$value = self::$propertyAccessor->getValue($value, substr($path, $pos));
7981

80-
if (null === $value && isset($parameters[$root]) && null !== $value = $request->get($root)) {
82+
if (null === $value && isset($parameters[$root]) && null !== $value = $get($root)) {
8183
$value = self::$propertyAccessor->getValue($value, substr($path, $pos));
8284
}
8385

Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,17 @@ protected function setUp(): void
4646
$this->logger = $this->createMock(LoggerInterface::class);
4747

4848
$this->session = $this->createMock(SessionInterface::class);
49-
$this->request = $this->createMock(Request::class);
50-
$this->request->attributes = new ParameterBag(['_stateless' => false]);
51-
$this->request->expects($this->any())->method('getSession')->willReturn($this->session);
49+
$this->request = Request::create('/');
50+
$this->request->attributes->set('_stateless', false);
51+
$this->request->setSession($this->session);
5252
$this->exception = $this->getMockBuilder(AuthenticationException::class)->onlyMethods(['getMessage'])->getMock();
5353
}
5454

5555
public function testForward()
5656
{
5757
$options = ['failure_forward' => true];
5858

59-
$subRequest = $this->getRequest();
60-
$subRequest->attributes->expects($this->once())
61-
->method('set')->with(SecurityRequestAttributes::AUTHENTICATION_ERROR, $this->exception);
59+
$subRequest = Request::create('/');
6260
$this->httpUtils->expects($this->once())
6361
->method('createRequest')->with($this->request, '/login')
6462
->willReturn($subRequest);
@@ -67,6 +65,7 @@ public function testForward()
6765
$result = $handler->onAuthenticationFailure($this->request, $this->exception);
6866

6967
$this->assertSame($this->response, $result);
68+
$this->assertSame($this->exception, $subRequest->attributes->get(SecurityRequestAttributes::AUTHENTICATION_ERROR));
7069
}
7170

7271
public function testRedirect()
@@ -106,9 +105,7 @@ public function testExceptionIsPassedInRequestOnForward()
106105
{
107106
$options = ['failure_forward' => true];
108107

109-
$subRequest = $this->getRequest();
110-
$subRequest->attributes->expects($this->once())
111-
->method('set')->with(SecurityRequestAttributes::AUTHENTICATION_ERROR, $this->exception);
108+
$subRequest = Request::create('/');
112109

113110
$this->httpUtils->expects($this->once())
114111
->method('createRequest')->with($this->request, '/login')
@@ -118,6 +115,8 @@ public function testExceptionIsPassedInRequestOnForward()
118115

119116
$handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
120117
$handler->onAuthenticationFailure($this->request, $this->exception);
118+
119+
$this->assertSame($this->exception, $subRequest->attributes->get(SecurityRequestAttributes::AUTHENTICATION_ERROR));
121120
}
122121

123122
public function testRedirectIsLogged()
@@ -137,7 +136,7 @@ public function testForwardIsLogged()
137136

138137
$this->httpUtils->expects($this->once())
139138
->method('createRequest')->with($this->request, '/login')
140-
->willReturn($this->getRequest());
139+
->willReturn(Request::create('/'));
141140

142141
$this->logger
143142
->expects($this->once())
@@ -161,9 +160,7 @@ public function testFailurePathCanBeOverwritten()
161160

162161
public function testFailurePathCanBeOverwrittenWithRequest()
163162
{
164-
$this->request->expects($this->once())
165-
->method('get')->with('_failure_path')
166-
->willReturn('/auth/login');
163+
$this->request->attributes->set('_failure_path', '/auth/login');
167164

168165
$this->httpUtils->expects($this->once())
169166
->method('createRedirectResponse')->with($this->request, '/auth/login');
@@ -174,9 +171,7 @@ public function testFailurePathCanBeOverwrittenWithRequest()
174171

175172
public function testFailurePathCanBeOverwrittenWithNestedAttributeInRequest()
176173
{
177-
$this->request->expects($this->once())
178-
->method('get')->with('_failure_path')
179-
->willReturn(['value' => '/auth/login']);
174+
$this->request->attributes->set('_failure_path', ['value' => '/auth/login']);
180175

181176
$this->httpUtils->expects($this->once())
182177
->method('createRedirectResponse')->with($this->request, '/auth/login');
@@ -189,9 +184,7 @@ public function testFailurePathParameterCanBeOverwritten()
189184
{
190185
$options = ['failure_path_parameter' => '_my_failure_path'];
191186

192-
$this->request->expects($this->once())
193-
->method('get')->with('_my_failure_path')
194-
->willReturn('/auth/login');
187+
$this->request->attributes->set('_my_failure_path', '/auth/login');
195188

196189
$this->httpUtils->expects($this->once())
197190
->method('createRedirectResponse')->with($this->request, '/auth/login');
@@ -204,9 +197,7 @@ public function testFailurePathFromRequestWithInvalidUrl()
204197
{
205198
$options = ['failure_path_parameter' => '_my_failure_path'];
206199

207-
$this->request->expects($this->once())
208-
->method('get')->with('_my_failure_path')
209-
->willReturn('some_route_name');
200+
$this->request->attributes->set('_my_failure_path', 'some_route_name');
210201

211202
$this->logger->expects($this->exactly(2))
212203
->method('debug')
@@ -229,22 +220,12 @@ public function testAbsoluteUrlRedirectionFromRequest()
229220
{
230221
$options = ['failure_path_parameter' => '_my_failure_path'];
231222

232-
$this->request->expects($this->once())
233-
->method('get')->with('_my_failure_path')
234-
->willReturn('https://localhost/some-path');
223+
$this->request->attributes->set('_my_failure_path', 'https://localhost/some-path');
235224

236225
$this->httpUtils->expects($this->once())
237226
->method('createRedirectResponse')->with($this->request, 'https://localhost/some-path');
238227

239228
$handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
240229
$handler->onAuthenticationFailure($this->request, $this->exception);
241230
}
242-
243-
private function getRequest()
244-
{
245-
$request = $this->createMock(Request::class);
246-
$request->attributes = $this->createMock(ParameterBag::class);
247-
248-
return $request;
249-
}
250231
}

Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,8 @@ public function testTargetPathFromRequestWithInvalidUrl()
146146
$options = ['target_path_parameter' => '_my_target_path'];
147147
$token = $this->createMock(TokenInterface::class);
148148

149-
$request = $this->createMock(Request::class);
150-
$request->expects($this->once())
151-
->method('get')->with('_my_target_path')
152-
->willReturn('some_route_name');
149+
$request = Request::create('/');
150+
$request->attributes->set('_my_target_path', 'some_route_name');
153151

154152
$logger = $this->createMock(LoggerInterface::class);
155153
$logger->expects($this->once())
@@ -165,10 +163,8 @@ public function testTargetPathWithAbsoluteUrlFromRequest()
165163
{
166164
$options = ['target_path_parameter' => '_my_target_path'];
167165

168-
$request = $this->createMock(Request::class);
169-
$request->expects($this->once())
170-
->method('get')->with('_my_target_path')
171-
->willReturn('https://localhost/some-path');
166+
$request = Request::create('/');
167+
$request->attributes->set('_my_target_path', 'https://localhost/some-path');
172168

173169
$httpUtils = $this->createMock(HttpUtils::class);
174170
$httpUtils->expects($this->once())

0 commit comments

Comments
 (0)