From ad696f2ab25f8a81960fa76fa2f5318b40957436 Mon Sep 17 00:00:00 2001
From: Oliver Hader <oliver@typo3.org>
Date: Wed, 30 Dec 2015 09:16:41 +0100
Subject: [PATCH] [TASK] Clean up object access via global variables

Clean up object access via global variables to TYPO3_DB,
BE_USER and TT in the error handling service layer.

Resolves: #72471
Releases: master, 7.6
Change-Id: I8d4532924fad2eee676e4e87c8e61bb164bfa93d
Reviewed-on: https://review.typo3.org/45499
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
---
 .../Error/AbstractExceptionHandler.php        | 22 ++++++---
 .../core/Classes/Error/ErrorHandler.php       | 45 +++++++++++++++----
 2 files changed, 51 insertions(+), 16 deletions(-)

diff --git a/typo3/sysext/core/Classes/Error/AbstractExceptionHandler.php b/typo3/sysext/core/Classes/Error/AbstractExceptionHandler.php
index 47665ab196c9..1e02d2c3cd58 100644
--- a/typo3/sysext/core/Classes/Error/AbstractExceptionHandler.php
+++ b/typo3/sysext/core/Classes/Error/AbstractExceptionHandler.php
@@ -14,7 +14,6 @@ namespace TYPO3\CMS\Core\Error;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -108,12 +107,13 @@ abstract class AbstractExceptionHandler implements ExceptionHandlerInterface, \T
         }
         $userId = 0;
         $workspace = 0;
-        if (is_object($GLOBALS['BE_USER'])) {
-            if (isset($GLOBALS['BE_USER']->user['uid'])) {
-                $userId = $GLOBALS['BE_USER']->user['uid'];
+        $backendUser = $this->getBackendUser();
+        if (is_object($backendUser)) {
+            if (isset($backendUser->user['uid'])) {
+                $userId = $backendUser->user['uid'];
             }
-            if (isset($GLOBALS['BE_USER']->workspace)) {
-                $workspace = $GLOBALS['BE_USER']->workspace;
+            if (isset($backendUser->workspace)) {
+                $workspace = $backendUser->workspace;
             }
         }
         $fields_values = array(
@@ -152,9 +152,17 @@ abstract class AbstractExceptionHandler implements ExceptionHandlerInterface, \T
         }
     }
 
+    /**
+     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
+     */
+    protected function getBackendUser()
+    {
+        return $GLOBALS['BE_USER'];
+    }
+
     /**
      * Gets the Database Object
-     * @return DatabaseConnection
+     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
      */
     protected function getDatabaseConnection()
     {
diff --git a/typo3/sysext/core/Classes/Error/ErrorHandler.php b/typo3/sysext/core/Classes/Error/ErrorHandler.php
index 584f114fccea..0c499167a345 100644
--- a/typo3/sysext/core/Classes/Error/ErrorHandler.php
+++ b/typo3/sysext/core/Classes/Error/ErrorHandler.php
@@ -135,8 +135,9 @@ class ErrorHandler implements ErrorHandlerInterface
                 GeneralUtility::devLog($message, 'core', $severity + 1);
             }
             // Write error message to TSlog (admin panel)
-            if (is_object($GLOBALS['TT'])) {
-                $GLOBALS['TT']->setTSlogMessage($message, $severity + 1);
+            $timeTracker = $this->getTimeTracker();
+            if (is_object($timeTracker)) {
+                $timeTracker->setTSlogMessage($message, $severity + 1);
             }
             // Write error message to sys_log table (ext: belog, Tools->Log)
             if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['belogErrorReporting']) {
@@ -180,15 +181,17 @@ class ErrorHandler implements ErrorHandlerInterface
      */
     protected function writeLog($logMessage, $severity)
     {
-        if (is_object($GLOBALS['TYPO3_DB']) && $GLOBALS['TYPO3_DB']->isConnected()) {
+        $databaseConnection = $this->getDatabaseConnection();
+        if (is_object($databaseConnection) && $databaseConnection->isConnected()) {
             $userId = 0;
             $workspace = 0;
-            if (is_object($GLOBALS['BE_USER'])) {
-                if (isset($GLOBALS['BE_USER']->user['uid'])) {
-                    $userId = $GLOBALS['BE_USER']->user['uid'];
+            $backendUser = $this->getBackendUser();
+            if (is_object($backendUser)) {
+                if (isset($backendUser->user['uid'])) {
+                    $userId = $backendUser->user['uid'];
                 }
-                if (isset($GLOBALS['BE_USER']->workspace)) {
-                    $workspace = $GLOBALS['BE_USER']->workspace;
+                if (isset($backendUser->workspace)) {
+                    $workspace = $backendUser->workspace;
                 }
             }
             $fields_values = array(
@@ -202,7 +205,31 @@ class ErrorHandler implements ErrorHandlerInterface
                 'tstamp' => $GLOBALS['EXEC_TIME'],
                 'workspace' => $workspace
             );
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
+            $databaseConnection->exec_INSERTquery('sys_log', $fields_values);
         }
     }
+
+    /**
+     * @return \TYPO3\CMS\Core\TimeTracker\TimeTracker
+     */
+    protected function getTimeTracker()
+    {
+        return $GLOBALS['TT'];
+    }
+
+    /**
+     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
+     */
+    protected function getBackendUser()
+    {
+        return $GLOBALS['BE_USER'];
+    }
+
+    /**
+     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
+     */
+    protected function getDatabaseConnection()
+    {
+        return $GLOBALS['TYPO3_DB'];
+    }
 }
-- 
GitLab