From 7d9a206fe6a3050f3717e7c172cdbe4a08565c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qu=E1=BB=B3nh=20Nguy=E1=BB=85n?= Date: Fri, 5 Jul 2024 23:01:28 +0700 Subject: [PATCH] Use nullsafe operator for event dispatcher (#52024) --- src/Illuminate/Auth/Passwords/PasswordBroker.php | 4 +--- src/Illuminate/Auth/SessionGuard.php | 8 ++------ src/Illuminate/Log/Logger.php | 4 +--- src/Illuminate/Redis/Connections/Connection.php | 10 +++++----- 4 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/Illuminate/Auth/Passwords/PasswordBroker.php b/src/Illuminate/Auth/Passwords/PasswordBroker.php index ad39d0df20f0..f543884fe05e 100755 --- a/src/Illuminate/Auth/Passwords/PasswordBroker.php +++ b/src/Illuminate/Auth/Passwords/PasswordBroker.php @@ -82,9 +82,7 @@ public function sendResetLink(#[\SensitiveParameter] array $credentials, ?Closur // the current URI having nothing set in the session to indicate errors. $user->sendPasswordResetNotification($token); - if ($this->events) { - $this->events->dispatch(new PasswordResetLinkSent($user)); - } + $this->events?->dispatch(new PasswordResetLinkSent($user)); return static::RESET_LINK_SENT; } diff --git a/src/Illuminate/Auth/SessionGuard.php b/src/Illuminate/Auth/SessionGuard.php index 1740396d9597..e4a46c611eb0 100644 --- a/src/Illuminate/Auth/SessionGuard.php +++ b/src/Illuminate/Auth/SessionGuard.php @@ -609,9 +609,7 @@ public function logout() // If we have an event dispatcher instance, we can fire off the logout event // so any further processing can be done. This allows the developer to be // listening for anytime a user signs out of this application manually. - if (isset($this->events)) { - $this->events->dispatch(new Logout($this->name, $user)); - } + $this->events?->dispatch(new Logout($this->name, $user)); // Once we have fired the logout event we will clear the users out of memory // so they are no longer available as the user is no longer considered as @@ -637,9 +635,7 @@ public function logoutCurrentDevice() // If we have an event dispatcher instance, we can fire off the logout event // so any further processing can be done. This allows the developer to be // listening for anytime a user signs out of this application manually. - if (isset($this->events)) { - $this->events->dispatch(new CurrentDeviceLogout($this->name, $user)); - } + $this->events?->dispatch(new CurrentDeviceLogout($this->name, $user)); // Once we have fired the logout event we will clear the users out of memory // so they are no longer available as the user is no longer considered as diff --git a/src/Illuminate/Log/Logger.php b/src/Illuminate/Log/Logger.php index 799228a65045..c94bf5bae249 100755 --- a/src/Illuminate/Log/Logger.php +++ b/src/Illuminate/Log/Logger.php @@ -244,9 +244,7 @@ protected function fireLogEvent($level, $message, array $context = []) // If the event dispatcher is set, we will pass along the parameters to the // log listeners. These are useful for building profilers or other tools // that aggregate all of the log messages for a given "request" cycle. - if (isset($this->dispatcher)) { - $this->dispatcher->dispatch(new MessageLogged($level, $message, $context)); - } + $this->dispatcher?->dispatch(new MessageLogged($level, $message, $context)); } /** diff --git a/src/Illuminate/Redis/Connections/Connection.php b/src/Illuminate/Redis/Connections/Connection.php index 69ed5c1a313d..50bd1b096710 100644 --- a/src/Illuminate/Redis/Connections/Connection.php +++ b/src/Illuminate/Redis/Connections/Connection.php @@ -117,11 +117,9 @@ public function command($method, array $parameters = []) $time = round((microtime(true) - $start) * 1000, 2); - if (isset($this->events)) { - $this->event(new CommandExecuted( - $method, $this->parseParametersForEvent($parameters), $time, $this - )); - } + $this->events?->dispatch(new CommandExecuted( + $method, $this->parseParametersForEvent($parameters), $time, $this + )); return $result; } @@ -142,6 +140,8 @@ protected function parseParametersForEvent(array $parameters) * * @param mixed $event * @return void + * + * @deprecated since Laravel 11.x */ protected function event($event) {