Browse Source

Merge pull request #16464 from cakephp/4.4-error-cleanup

Remove unnecessary variable assignment.
ADmad 3 years ago
parent
commit
36ac07da8b
2 changed files with 11 additions and 29 deletions
  1. 6 16
      src/Error/ErrorTrap.php
  2. 5 13
      src/Error/ExceptionTrap.php

+ 6 - 16
src/Error/ErrorTrap.php

@@ -147,20 +147,15 @@ class ErrorTrap
      */
     public function renderer(): ErrorRendererInterface
     {
-        $class = $this->_getConfig('errorRenderer');
-        if (!$class) {
-            $class = $this->chooseErrorRenderer();
-        }
+        /** @var class-string<\Cake\Error\ErrorRendererInterface> $class */
+        $class = $this->_getConfig('errorRenderer') ?: $this->chooseErrorRenderer();
         if (!in_array(ErrorRendererInterface::class, class_implements($class))) {
             throw new InvalidArgumentException(
                 "Cannot use {$class} as an error renderer. It must implement \Cake\Error\ErrorRendererInterface."
             );
         }
 
-        /** @var \Cake\Error\ErrorRendererInterface $instance */
-        $instance = new $class($this->_config);
-
-        return $instance;
+        return new $class($this->_config);
     }
 
     /**
@@ -170,19 +165,14 @@ class ErrorTrap
      */
     public function logger(): ErrorLoggerInterface
     {
-        $class = $this->_getConfig('logger');
-        if (!$class) {
-            $class = $this->_defaultConfig['logger'];
-        }
+        /** @var class-string<\Cake\Error\ErrorLoggerInterface> $class */
+        $class = $this->_getConfig('logger', $this->_defaultConfig['logger']);
         if (!in_array(ErrorLoggerInterface::class, class_implements($class))) {
             throw new InvalidArgumentException(
                 "Cannot use {$class} as an error logger. It must implement \Cake\Error\ErrorLoggerInterface."
             );
         }
 
-        /** @var \Cake\Error\ErrorLoggerInterface $instance */
-        $instance = new $class($this->_config);
-
-        return $instance;
+        return new $class($this->_config);
     }
 }

+ 5 - 13
src/Error/ExceptionTrap.php

@@ -115,7 +115,7 @@ class ExceptionTrap
         }
 
         if (is_string($class)) {
-            /** @var class-string $class */
+            /** @var class-string<\Cake\Error\ExceptionRendererInterface> $class */
             if (!(method_exists($class, 'render') && method_exists($class, 'write'))) {
                 throw new InvalidArgumentException(
                     "Cannot use {$class} as an `exceptionRenderer`. " .
@@ -123,10 +123,7 @@ class ExceptionTrap
                 );
             }
 
-            /** @var \Cake\Error\ExceptionRendererInterface $instance */
-            $instance = new $class($exception, $request, $this->_config);
-
-            return $instance;
+            return new $class($exception, $request, $this->_config);
         }
 
         return $class($exception, $request);
@@ -150,10 +147,8 @@ class ExceptionTrap
      */
     public function logger(): ErrorLoggerInterface
     {
-        $class = $this->_getConfig('logger');
-        if (!$class) {
-            $class = $this->_defaultConfig['logger'];
-        }
+        /** @var class-string<\Cake\Error\ErrorLoggerInterface> $class */
+        $class = $this->_getConfig('logger', $this->_defaultConfig['logger']);
         if (!in_array(ErrorLoggerInterface::class, class_implements($class))) {
             throw new InvalidArgumentException(
                 "Cannot use {$class} as an exception logger. " .
@@ -161,10 +156,7 @@ class ExceptionTrap
             );
         }
 
-        /** @var \Cake\Error\ErrorLoggerInterface $instance */
-        $instance = new $class($this->_config);
-
-        return $instance;
+        return new $class($this->_config);
     }
 
     /**