[TASK] Use a reference variable to pass $this into hooks 36/63036/4
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Sat, 25 Jan 2020 16:52:28 +0000 (17:52 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Fri, 14 Feb 2020 19:48:41 +0000 (20:48 +0100)
Unfortunately phpstan looses the correct type of $this
as soon as $this is passed into a hooked as a hook method
parameter without a specific type. This leads to phpstan
losing the information about available methods and properties
of $this.

Releases: master
Resolves: #90204
Change-Id: I6fdb4f07dd970a1602a7afbbf662b29f2a927a15
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63036
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/felogin/Classes/Controller/FrontendLoginController.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/frontend/Classes/Imaging/GifBuilder.php
typo3/sysext/indexed_search/Classes/Hook/CrawlerHook.php
typo3/sysext/linkvalidator/Classes/Task/ValidatorTask.php

index b57048a..5a2f78f 100644 (file)
@@ -197,7 +197,8 @@ class FrontendLoginController extends AbstractPlugin
                 ];
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['beforeRedirect'] ?? [] as $_funcRef) {
                     if ($_funcRef) {
-                        GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+                        $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+                        GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
                     }
                 }
                 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirectUrl);
@@ -208,7 +209,8 @@ class FrontendLoginController extends AbstractPlugin
             $_params = [
                 'content' => $content
             ];
-            $content = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+            $content = GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
         }
         return $this->conf['wrapContentInBaseClass'] ? $this->pi_wrapInBaseClass($content) : $content;
     }
@@ -830,7 +832,8 @@ class FrontendLoginController extends AbstractPlugin
                     foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['logout_confirmed'] ?? [] as $_funcRef) {
                         $_params = [];
                         if ($_funcRef) {
-                            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+                            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+                            GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
                         }
                     }
                     switch ($redirMethod) {
index 587b49f..81f52ad 100644 (file)
@@ -2898,7 +2898,8 @@ class ContentObjectRenderer implements LoggerAwareInterface
                 'lifetime' => $lifetime,
                 'tags' => $tags
             ];
-            GeneralUtility::callUserFunction($_funcRef, $params, $this);
+            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+            GeneralUtility::callUserFunction($_funcRef, $params, $ref);
         }
         $cacheFrontend->set($key, $content, $tags, $lifetime);
         return $content;
@@ -4716,7 +4717,8 @@ class ContentObjectRenderer implements LoggerAwareInterface
                 if (!$hookObject instanceof ContentObjectGetDataHookInterface) {
                     throw new \UnexpectedValueException('$hookObject must implement interface ' . ContentObjectGetDataHookInterface::class, 1195044480);
                 }
-                $retVal = $hookObject->getDataExtension($string, $fieldArray, $secVal, $retVal, $this);
+                $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+                $retVal = $hookObject->getDataExtension($string, $fieldArray, $secVal, $retVal, $ref);
             }
         }
         return $retVal;
@@ -5125,7 +5127,8 @@ class ContentObjectRenderer implements LoggerAwareInterface
             'tagAttributes' => &$tagAttributes
         ];
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['typoLink_PostProc'] ?? [] as $_funcRef) {
-            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+            GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
         }
 
         // If flag "returnLastTypoLinkUrl" set, then just return the latest URL made:
index e6794af..aa31659 100644 (file)
@@ -2155,7 +2155,8 @@ class TypoScriptFrontendController implements LoggerAwareInterface
     {
         $_params = [];
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['settingLanguage_preProcess'] ?? [] as $_funcRef) {
-            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+            GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
         }
 
         // Rendering charset of HTML page.
index 06d37ef..9040b8a 100644 (file)
@@ -125,7 +125,8 @@ class GifBuilder extends GraphicalFunctions
             // line as TEXT obj, see extension julle_gifbconf
             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'] ?? [] as $_funcRef) {
                 $_params = $this->setup;
-                $this->setup = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+                $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+                $this->setup = GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
             }
             // Initializing global Char Range Map
             $this->charRangeMap = [];
index 1a5d866..3c377b1 100644 (file)
@@ -234,7 +234,8 @@ class CrawlerHook
                             $hookObj = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['crawler'][$cfgRec['type']]);
                             $this->pObj = $pObj;
                             // For addQueueEntryForHook()
-                            $hookObj->indexOperation($cfgRec, $session_data, $params, $this);
+                            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+                            $hookObj->indexOperation($cfgRec, $session_data, $params, $ref);
                         }
                 }
                 // Save process data which might be modified:
index 58630ee..ef246de 100644 (file)
@@ -443,7 +443,8 @@ class ValidatorTask extends AbstractTask
                 'pObj' => &$this,
                 'markerArray' => $markerArray
             ];
-            $newMarkers = GeneralUtility::callUserFunction($userFunc, $params, $this);
+            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
+            $newMarkers = GeneralUtility::callUserFunction($userFunc, $params, $ref);
             if (is_array($newMarkers)) {
                 $markerArray = $newMarkers + $markerArray;
             }