Skip to content

Commit 00f602b

Browse files
committed
Make use of the nullsafe operator
1 parent a0e1686 commit 00f602b

13 files changed

+46
-128
lines changed

Authentication/AuthenticatorManager.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,13 @@ public function supports(Request $request): ?bool
101101
$skippedAuthenticators = [];
102102
$lazy = true;
103103
foreach ($this->authenticators as $authenticator) {
104-
if (null !== $this->logger) {
105-
$this->logger->debug('Checking support on authenticator.', ['firewall_name' => $this->firewallName, 'authenticator' => \get_class($authenticator)]);
106-
}
104+
$this->logger?->debug('Checking support on authenticator.', ['firewall_name' => $this->firewallName, 'authenticator' => \get_class($authenticator)]);
107105

108106
if (false !== $supports = $authenticator->supports($request)) {
109107
$authenticators[] = $authenticator;
110108
$lazy = $lazy && null === $supports;
111109
} else {
112-
if (null !== $this->logger) {
113-
$this->logger->debug('Authenticator does not support the request.', ['firewall_name' => $this->firewallName, 'authenticator' => \get_class($authenticator)]);
114-
}
110+
$this->logger?->debug('Authenticator does not support the request.', ['firewall_name' => $this->firewallName, 'authenticator' => \get_class($authenticator)]);
115111
$skippedAuthenticators[] = $authenticator;
116112
}
117113
}
@@ -149,18 +145,14 @@ private function executeAuthenticators(array $authenticators, Request $request):
149145
// eagerly (before token storage is initialized), whereas authenticate() is called
150146
// lazily (after initialization).
151147
if (false === $authenticator->supports($request)) {
152-
if (null !== $this->logger) {
153-
$this->logger->debug('Skipping the "{authenticator}" authenticator as it did not support the request.', ['authenticator' => \get_class($authenticator)]);
154-
}
148+
$this->logger?->debug('Skipping the "{authenticator}" authenticator as it did not support the request.', ['authenticator' => \get_class($authenticator)]);
155149

156150
continue;
157151
}
158152

159153
$response = $this->executeAuthenticator($authenticator, $request);
160154
if (null !== $response) {
161-
if (null !== $this->logger) {
162-
$this->logger->debug('The "{authenticator}" authenticator set the response. Any later authenticator will not be called', ['authenticator' => \get_class($authenticator)]);
163-
}
155+
$this->logger?->debug('The "{authenticator}" authenticator set the response. Any later authenticator will not be called', ['authenticator' => \get_class($authenticator)]);
164156

165157
return $response;
166158
}
@@ -208,9 +200,7 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
208200

209201
$this->eventDispatcher->dispatch(new AuthenticationSuccessEvent($authenticatedToken), AuthenticationEvents::AUTHENTICATION_SUCCESS);
210202

211-
if (null !== $this->logger) {
212-
$this->logger->info('Authenticator successful!', ['token' => $authenticatedToken, 'authenticator' => \get_class($authenticator)]);
213-
}
203+
$this->logger?->info('Authenticator successful!', ['token' => $authenticatedToken, 'authenticator' => \get_class($authenticator)]);
214204
} catch (AuthenticationException $e) {
215205
// oh no! Authentication failed!
216206
$response = $this->handleAuthenticationFailure($e, $request, $authenticator, $passport);
@@ -227,9 +217,7 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
227217
return $response;
228218
}
229219

230-
if (null !== $this->logger) {
231-
$this->logger->debug('Authenticator set no success response: request continues.', ['authenticator' => \get_class($authenticator)]);
232-
}
220+
$this->logger?->debug('Authenticator set no success response: request continues.', ['authenticator' => \get_class($authenticator)]);
233221

234222
return null;
235223
}
@@ -254,9 +242,7 @@ private function handleAuthenticationSuccess(TokenInterface $authenticatedToken,
254242
*/
255243
private function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, AuthenticatorInterface $authenticator, ?Passport $passport): ?Response
256244
{
257-
if (null !== $this->logger) {
258-
$this->logger->info('Authenticator failed.', ['exception' => $authenticationException, 'authenticator' => \get_class($authenticator)]);
259-
}
245+
$this->logger?->info('Authenticator failed.', ['exception' => $authenticationException, 'authenticator' => \get_class($authenticator)]);
260246

261247
// Avoid leaking error details in case of invalid user (e.g. user not found or invalid account status)
262248
// to prevent user enumeration via response content comparison

Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,15 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
7878
}
7979

8080
if ($this->options['failure_forward']) {
81-
if (null !== $this->logger) {
82-
$this->logger->debug('Authentication failure, forward triggered.', ['failure_path' => $this->options['failure_path']]);
83-
}
81+
$this->logger?->debug('Authentication failure, forward triggered.', ['failure_path' => $this->options['failure_path']]);
8482

8583
$subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
8684
$subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
8785

8886
return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
8987
}
9088

91-
if (null !== $this->logger) {
92-
$this->logger->debug('Authentication failure, redirect triggered.', ['failure_path' => $this->options['failure_path']]);
93-
}
89+
$this->logger?->debug('Authentication failure, redirect triggered.', ['failure_path' => $this->options['failure_path']]);
9490

9591
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
9692

Authenticator/AbstractPreAuthenticatedAuthenticator.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,13 @@ public function supports(Request $request): ?bool
6464
} catch (BadCredentialsException $e) {
6565
$this->clearToken($e);
6666

67-
if (null !== $this->logger) {
68-
$this->logger->debug('Skipping pre-authenticated authenticator as a BadCredentialsException is thrown.', ['exception' => $e, 'authenticator' => static::class]);
69-
}
67+
$this->logger?->debug('Skipping pre-authenticated authenticator as a BadCredentialsException is thrown.', ['exception' => $e, 'authenticator' => static::class]);
7068

7169
return false;
7270
}
7371

7472
if (null === $username) {
75-
if (null !== $this->logger) {
76-
$this->logger->debug('Skipping pre-authenticated authenticator no username could be extracted.', ['authenticator' => static::class]);
77-
}
73+
$this->logger?->debug('Skipping pre-authenticated authenticator no username could be extracted.', ['authenticator' => static::class]);
7874

7975
return false;
8076
}
@@ -83,9 +79,7 @@ public function supports(Request $request): ?bool
8379
$token = $this->tokenStorage->getToken();
8480

8581
if ($token instanceof PreAuthenticatedToken && $this->firewallName === $token->getFirewallName() && $token->getUserIdentifier() === $username) {
86-
if (null !== $this->logger) {
87-
$this->logger->debug('Skipping pre-authenticated authenticator as the user already has an existing session.', ['authenticator' => static::class]);
88-
}
82+
$this->logger?->debug('Skipping pre-authenticated authenticator as the user already has an existing session.', ['authenticator' => static::class]);
8983

9084
return false;
9185
}
@@ -131,9 +125,7 @@ private function clearToken(AuthenticationException $exception): void
131125
if ($token instanceof PreAuthenticatedToken && $this->firewallName === $token->getFirewallName()) {
132126
$this->tokenStorage->setToken(null);
133127

134-
if (null !== $this->logger) {
135-
$this->logger->info('Cleared pre-authenticated token due to an exception.', ['exception' => $exception]);
136-
}
128+
$this->logger?->info('Cleared pre-authenticated token due to an exception.', ['exception' => $exception]);
137129
}
138130
}
139131
}

Authenticator/HttpBasicAuthenticator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
8686

8787
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
8888
{
89-
if (null !== $this->logger) {
90-
$this->logger->info('Basic authentication failed for user.', ['username' => $request->headers->get('PHP_AUTH_USER'), 'exception' => $exception]);
91-
}
89+
$this->logger?->info('Basic authentication failed for user.', ['username' => $request->headers->get('PHP_AUTH_USER'), 'exception' => $exception]);
9290

9391
return $this->start($request, $exception);
9492
}

Authenticator/RememberMeAuthenticator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ public function supports(Request $request): ?bool
7373
return false;
7474
}
7575

76-
if (null !== $this->logger) {
77-
$this->logger->debug('Remember-me cookie detected.');
78-
}
76+
$this->logger?->debug('Remember-me cookie detected.');
7977

8078
// the `null` return value indicates that this authenticator supports lazy firewalls
8179
return null;

EventListener/CheckRememberMeConditionsListener.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public function onSuccessfulLogin(LoginSuccessEvent $event): void
5656
if (!$this->options['always_remember_me']) {
5757
$parameter = ParameterBagUtils::getRequestParameterValue($event->getRequest(), $this->options['remember_me_parameter']);
5858
if (!('true' === $parameter || 'on' === $parameter || '1' === $parameter || 'yes' === $parameter || true === $parameter)) {
59-
if (null !== $this->logger) {
60-
$this->logger->debug('Remember me disabled; request does not contain remember me parameter ("{parameter}").', ['parameter' => $this->options['remember_me_parameter']]);
61-
}
59+
$this->logger?->debug('Remember me disabled; request does not contain remember me parameter ("{parameter}").', ['parameter' => $this->options['remember_me_parameter']]);
6260

6361
return;
6462
}

EventListener/RememberMeListener.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function onSuccessfulLogin(LoginSuccessEvent $event): void
4747
{
4848
$passport = $event->getPassport();
4949
if (!$passport->hasBadge(RememberMeBadge::class)) {
50-
if (null !== $this->logger) {
51-
$this->logger->debug('Remember me skipped: your authenticator does not support it.', ['authenticator' => \get_class($event->getAuthenticator())]);
52-
}
50+
$this->logger?->debug('Remember me skipped: your authenticator does not support it.', ['authenticator' => \get_class($event->getAuthenticator())]);
5351

5452
return;
5553
}
@@ -60,16 +58,12 @@ public function onSuccessfulLogin(LoginSuccessEvent $event): void
6058
/** @var RememberMeBadge $badge */
6159
$badge = $passport->getBadge(RememberMeBadge::class);
6260
if (!$badge->isEnabled()) {
63-
if (null !== $this->logger) {
64-
$this->logger->debug('Remember me skipped: the RememberMeBadge is not enabled.');
65-
}
61+
$this->logger?->debug('Remember me skipped: the RememberMeBadge is not enabled.');
6662

6763
return;
6864
}
6965

70-
if (null !== $this->logger) {
71-
$this->logger->debug('Remember-me was requested; setting cookie.');
72-
}
66+
$this->logger?->debug('Remember-me was requested; setting cookie.');
7367

7468
$this->rememberMeHandler->createRememberMeCookie($event->getUser());
7569
}

Firewall/ChannelListener.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public function supports(Request $request): ?bool
6262
}
6363

6464
if ('http' === $channel && $request->isSecure()) {
65-
if (null !== $this->logger) {
66-
$this->logger->info('Redirecting to HTTP.');
67-
}
65+
$this->logger?->info('Redirecting to HTTP.');
6866

6967
return true;
7068
}

Firewall/ContextListener.php

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,30 +120,22 @@ public function authenticate(RequestEvent $event)
120120

121121
$token = $this->safelyUnserialize($token);
122122

123-
if (null !== $this->logger) {
124-
$this->logger->debug('Read existing security token from the session.', [
125-
'key' => $this->sessionKey,
126-
'token_class' => \is_object($token) ? \get_class($token) : null,
127-
]);
128-
}
123+
$this->logger?->debug('Read existing security token from the session.', [
124+
'key' => $this->sessionKey,
125+
'token_class' => \is_object($token) ? \get_class($token) : null,
126+
]);
129127

130128
if ($token instanceof TokenInterface) {
131129
$originalToken = $token;
132130
$token = $this->refreshUser($token);
133131

134132
if (!$token) {
135-
if ($this->logger) {
136-
$this->logger->debug('Token was deauthenticated after trying to refresh it.');
137-
}
133+
$this->logger?->debug('Token was deauthenticated after trying to refresh it.');
138134

139-
if ($this->dispatcher) {
140-
$this->dispatcher->dispatch(new TokenDeauthenticatedEvent($originalToken, $request));
141-
}
135+
$this->dispatcher?->dispatch(new TokenDeauthenticatedEvent($originalToken, $request));
142136
}
143137
} elseif (null !== $token) {
144-
if (null !== $this->logger) {
145-
$this->logger->warning('Expected a security token from the session, got something else.', ['key' => $this->sessionKey, 'received' => $token]);
146-
}
138+
$this->logger?->warning('Expected a security token from the session, got something else.', ['key' => $this->sessionKey, 'received' => $token]);
147139

148140
$token = null;
149141
}
@@ -170,9 +162,7 @@ public function onKernelResponse(ResponseEvent $event)
170162
return;
171163
}
172164

173-
if ($this->dispatcher) {
174-
$this->dispatcher->removeListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse']);
175-
}
165+
$this->dispatcher?->removeListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse']);
176166
$this->registered = false;
177167
$session = $request->getSession();
178168
$sessionId = $session->getId();
@@ -186,9 +176,7 @@ public function onKernelResponse(ResponseEvent $event)
186176
} else {
187177
$session->set($this->sessionKey, serialize($token));
188178

189-
if (null !== $this->logger) {
190-
$this->logger->debug('Stored the security token in the session.', ['key' => $this->sessionKey]);
191-
}
179+
$this->logger?->debug('Stored the security token in the session.', ['key' => $this->sessionKey]);
192180
}
193181

194182
if ($this->sessionTrackerEnabler && $session->getId() === $sessionId) {
@@ -227,9 +215,7 @@ protected function refreshUser(TokenInterface $token): ?TokenInterface
227215
if ($token instanceof AbstractToken && $this->hasUserChanged($user, $newToken)) {
228216
$userDeauthenticated = true;
229217

230-
if (null !== $this->logger) {
231-
$this->logger->debug('Cannot refresh token because user has changed.', ['username' => $refreshedUser->getUserIdentifier(), 'provider' => \get_class($provider)]);
232-
}
218+
$this->logger?->debug('Cannot refresh token because user has changed.', ['username' => $refreshedUser->getUserIdentifier(), 'provider' => \get_class($provider)]);
233219

234220
continue;
235221
}
@@ -251,9 +237,7 @@ protected function refreshUser(TokenInterface $token): ?TokenInterface
251237
} catch (UnsupportedUserException $e) {
252238
// let's try the next user provider
253239
} catch (UserNotFoundException $e) {
254-
if (null !== $this->logger) {
255-
$this->logger->warning('Username could not be found in the selected user provider.', ['username' => $e->getUserIdentifier(), 'provider' => \get_class($provider)]);
256-
}
240+
$this->logger?->warning('Username could not be found in the selected user provider.', ['username' => $e->getUserIdentifier(), 'provider' => \get_class($provider)]);
257241

258242
$userNotFoundByProvider = true;
259243
}
@@ -288,9 +272,7 @@ private function safelyUnserialize(string $serializedToken)
288272
if (0x37313BC !== $e->getCode()) {
289273
throw $e;
290274
}
291-
if ($this->logger) {
292-
$this->logger->warning('Failed to unserialize the security token from the session.', ['key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e]);
293-
}
275+
$this->logger?->warning('Failed to unserialize the security token from the session.', ['key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e]);
294276
} finally {
295277
restore_error_handler();
296278
ini_set('unserialize_callback_func', $prevUnserializeHandler);

0 commit comments

Comments
 (0)