[CLEANUP] Ensure variables initalized and fix code smell 42/51742/3
authorSascha Egerer <sascha@sascha-egerer.de>
Sat, 18 Feb 2017 15:58:33 +0000 (16:58 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 23 Feb 2017 14:02:43 +0000 (15:02 +0100)
* Ensure that variables are correctly initalized.
* Fix some php docs.
* Initalizes for loop iterator variables before the loop if they are
  also used after the for loop is finished.
* Remove superfluous function arguments.

Resolves: #79892
Releases: master
Change-Id: I75109b2914f2b05806a303837b7aad79be8e3707
Reviewed-on: https://review.typo3.org/51742
Reviewed-by: Thomas Hohn <thomas@hohn.dk>
Tested-by: Thomas Hohn <thomas@hohn.dk>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christer V <cvi@systime.dk>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
23 files changed:
typo3/sysext/core/Classes/Charset/CharsetConverter.php
typo3/sysext/core/Classes/Configuration/FlexForm/FlexFormTools.php
typo3/sysext/core/Classes/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcher.php
typo3/sysext/core/Classes/Controller/CommandLineController.php
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Classes/Database/QueryGenerator.php
typo3/sysext/core/Classes/Database/QueryView.php
typo3/sysext/core/Classes/ExtDirect/ExtDirectRouter.php
typo3/sysext/core/Classes/FrontendEditing/FrontendEditingController.php
typo3/sysext/core/Classes/Html/RteHtmlParser.php
typo3/sysext/core/Classes/Http/Response.php
typo3/sysext/core/Classes/Localization/LanguageStore.php
typo3/sysext/core/Classes/Localization/Parser/LocallangXmlParser.php
typo3/sysext/core/Classes/Package/MetaData/PackageConstraint.php
typo3/sysext/core/Classes/Service/DependencyOrderingService.php
typo3/sysext/core/Classes/TimeTracker/TimeTracker.php
typo3/sysext/core/Classes/TypoScript/ConfigurationForm.php
typo3/sysext/core/Classes/Utility/CommandUtility.php
typo3/sysext/core/Classes/Utility/File/ExtendedFileUtility.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Http/UploadedFileTest.php
typo3/sysext/core/Tests/Unit/Resource/FolderTest.php
typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php

index bfababa..772bd2b 100644 (file)
@@ -688,8 +688,9 @@ class CharsetConverter implements SingletonInterface
         // This verifies that it IS a multi byte string
         if (($ord & 192) === 192) {
             $binBuf = '';
+            $b = 0;
             // For each byte in multibyte string...
-            for ($b = 0; $b < 8; $b++) {
+            for (; $b < 8; $b++) {
                 // Shift it left and ...
                 $ord = $ord << 1;
                 // ... and with 8th bit - if that is set, then there are still bytes in sequence.
@@ -1432,8 +1433,9 @@ class CharsetConverter implements SingletonInterface
             if ($i <= 0) {
                 return '';
             }
+            $bc = 0;
             // Sanity check
-            for ($bc = 0, $mbs = ord($str[$i]); $mbs & 128; $mbs = $mbs << 1) {
+            for ($mbs = ord($str[$i]); $mbs & 128; $mbs = $mbs << 1) {
                 // Calculate number of bytes
                 $bc++;
             }
@@ -1536,7 +1538,8 @@ class CharsetConverter implements SingletonInterface
         GeneralUtility::logDeprecatedFunction();
         // Number of characters
         $n = 0;
-        for ($i = $pos; $i > 0; $i--) {
+        $i = $pos;
+        for (; $i > 0; $i--) {
             $c = (int)ord($str[$i]);
             // single-byte (0xxxxxx)
             if (!($c & 128)) {
@@ -1585,8 +1588,9 @@ class CharsetConverter implements SingletonInterface
             if (!($c & 128)) {
                 $mbc = $str[$i];
             } elseif (($c & 192) === 192) {
+                $bc = 0;
                 // multi-byte starting byte (11xxxxxx)
-                for ($bc = 0; $c & 128; $c = $c << 1) {
+                for (; $c & 128; $c = $c << 1) {
                     $bc++;
                 }
                 // calculate number of bytes
@@ -1627,7 +1631,8 @@ class CharsetConverter implements SingletonInterface
     {
         GeneralUtility::logDeprecatedFunction();
         $shiftJis = $charset === 'shift_jis';
-        for ($i = 0; isset($str[$i]) && $i < $len; $i++) {
+        $i = 0;
+        for (; isset($str[$i]) && $i < $len; $i++) {
             $c = ord($str[$i]);
             if ($shiftJis) {
                 if ($c >= 128 && $c < 160 || $c >= 224) {
index 1399622..ff28f69 100644 (file)
@@ -110,6 +110,7 @@ class FlexFormTools
      */
     public function getDataStructureIdentifier(array $fieldTca, string $tableName, string $fieldName, array $row): string
     {
+        $dataStructureIdentifier = null;
         // Hook to inject an own logic to point to a data structure elsewhere.
         // A hook has to implement method getDataStructureIdentifierPreProcess() to be called here.
         // All hooks are called in a row, each MUST return an array, and the FIRST one that
index 5275c5f..cca7d88 100644 (file)
@@ -272,6 +272,9 @@ abstract class AbstractConditionMatcher
                     case 'dayofyear':
                         $theTestValue = date('z', $theEvalTime);
                         break;
+                    default:
+                        $theTestValue = 0;
+                        break;
                 }
                 $theTestValue = (int)$theTestValue;
                 // comp
index 7dee4a1..e5686b0 100644 (file)
@@ -157,6 +157,7 @@ class CommandLineController
             $allOptions[] = $cfg[0];
             $argSplit = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $cfg[0], true);
             if (isset($cli_args_copy[$argSplit[0]])) {
+                $ii = 0;
                 foreach ($argSplit as $i => $v) {
                     $ii = $i;
                     if ($i > 0) {
index 0e2a205..7e2f102 100644 (file)
@@ -5612,13 +5612,11 @@ class DataHandler
                 'label' => $label,
             ];
             return $this->copyRecord_raw($table, $id, -1, $overrideArray, $workspaceOptions);
+        }
         // Reuse the existing record and return its uid
         // (prior to TYPO3 CMS 6.2, an error was thrown here, which
         // did not make much sense since the information is available)
-        } else {
-            return $versionRecord['uid'];
-        }
-        return null;
+        return $versionRecord['uid'];
     }
 
     /**
@@ -5903,6 +5901,7 @@ class DataHandler
 
             // If record has been versioned/copied in this process, handle invalid relations of the live record
             $liveId = BackendUtility::getLiveVersionIdOfRecord($table, $MM_localUid);
+            $originalId = 0;
             if (!empty($this->copyMappingArray_merged[$table])) {
                 $originalId = array_search($MM_localUid, $this->copyMappingArray_merged[$table]);
             }
index 74b258f..3ab572c 100644 (file)
@@ -437,7 +437,8 @@ class QueryGenerator
             $ssArr = $this->getSubscript($POST['qG_del']);
             $workArr = &$this->queryConfig;
             $ssArrSize = count($ssArr) - 1;
-            for ($i = 0; $i < $ssArrSize; $i++) {
+            $i = 0;
+            for (; $i < $ssArrSize; $i++) {
                 $workArr = &$workArr[$ssArr[$i]];
             }
             // Delete the entry and move the other entries
@@ -454,7 +455,8 @@ class QueryGenerator
             $ssArr = $this->getSubscript($POST['qG_ins']);
             $workArr = &$this->queryConfig;
             $ssArrSize = count($ssArr) - 1;
-            for ($i = 0; $i < $ssArrSize; $i++) {
+            $i = 0;
+            for (; $i < $ssArrSize; $i++) {
                 $workArr = &$workArr[$ssArr[$i]];
             }
             // Move all entries above position where new entry is to be inserted
@@ -472,7 +474,8 @@ class QueryGenerator
             $ssArr = $this->getSubscript($POST['qG_up']);
             $workArr = &$this->queryConfig;
             $ssArrSize = count($ssArr) - 1;
-            for ($i = 0; $i < $ssArrSize; $i++) {
+            $i = 0;
+            for (; $i < $ssArrSize; $i++) {
                 $workArr = &$workArr[$ssArr[$i]];
             }
             // Swap entries
@@ -486,7 +489,8 @@ class QueryGenerator
             $ssArr = $this->getSubscript($POST['qG_nl']);
             $workArr = &$this->queryConfig;
             $ssArraySize = count($ssArr) - 1;
-            for ($i = 0; $i < $ssArraySize; $i++) {
+            $i = 0;
+            for (; $i < $ssArraySize; $i++) {
                 $workArr = &$workArr[$ssArr[$i]];
             }
             // Do stuff:
@@ -507,7 +511,8 @@ class QueryGenerator
             $ssArr = $this->getSubscript($POST['qG_remnl']);
             $workArr = &$this->queryConfig;
             $ssArrSize = count($ssArr) - 1;
-            for ($i = 0; $i < $ssArrSize; $i++) {
+            $i = 0;
+            for (; $i < $ssArrSize; $i++) {
                 $workArr = &$workArr[$ssArr[$i]];
             }
             // Do stuff:
@@ -879,7 +884,6 @@ class QueryGenerator
                 $from_table_Arr[0] = $fieldSetup['foreign_table'];
             }
             $counter = 0;
-            $webMountPageTree = '';
             $tablePrefix = '';
             $backendUserAuthentication = $this->getBackendUserAuthentication();
             $module = $this->getModule();
@@ -905,6 +909,7 @@ class QueryGenerator
                         }
                         $useSelectLabels = true;
                     }
+                    $altLabelFieldSelect = [];
                     if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
                         foreach ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'] as $altLabelArray) {
                             if (substr($altLabelArray[0], 0, 4) === 'LLL:') {
index c0dbcba..dac343d 100644 (file)
@@ -1058,6 +1058,7 @@ class QueryView
                         }
                         $useSelectLabels = 1;
                     }
+                    $altLabelFieldSelect = [];
                     if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
                         $items = $GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'];
                         foreach ($items as $altLabelArray) {
index 31d32b9..e981d89 100644 (file)
@@ -119,21 +119,22 @@ class ExtDirectRouter
     protected function processRpc($singleRequest, $namespace)
     {
         $endpointName = $namespace . '.' . $singleRequest->action;
-        if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
+        if (
+            !isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])
+            || !is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])
+        ) {
             throw new \UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName, 1294586450);
         }
-        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
-            if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName]['callbackClass'])) {
-                throw new \UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName, 1294586451);
-            }
-            $callbackClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName]['callbackClass'];
-            $configuration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName];
-            if (!is_null($configuration['moduleName']) && !is_null($configuration['accessLevel'])) {
-                $GLOBALS['BE_USER']->modAccess([
-                    'name' => $configuration['moduleName'],
-                    'access' => $configuration['accessLevel']
-                ], true);
-            }
+        if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName]['callbackClass'])) {
+            throw new \UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName, 1294586451);
+        }
+        $callbackClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName]['callbackClass'];
+        $configuration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName];
+        if (!is_null($configuration['moduleName']) && !is_null($configuration['accessLevel'])) {
+            $GLOBALS['BE_USER']->modAccess([
+                'name' => $configuration['moduleName'],
+                'access' => $configuration['accessLevel']
+            ], true);
         }
         $endpointObject = GeneralUtility::getUserObj($callbackClass);
         return call_user_func_array([$endpointObject, $singleRequest->method], is_array($singleRequest->data) ? $singleRequest->data : []);
index d684788..d034293 100644 (file)
@@ -80,6 +80,7 @@ class FrontendEditingController
         list($table, $uid) = explode(':', $currentRecord);
         // Page ID for new records, 0 if not specified
         $newRecordPid = (int)$conf['newRecordInPid'];
+        $newUid = null;
         if (!$conf['onlyCurrentPid'] || $dataArray['pid'] == $GLOBALS['TSFE']->id) {
             if ($table === 'pages') {
                 $newUid = $uid;
index 4d64c26..e04e9db 100644 (file)
@@ -1098,7 +1098,8 @@ class RteHtmlParser extends HtmlParser
         } else {
             $curURL = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
             $urlLength = strlen($url);
-            for ($a = 0; $a < $urlLength; $a++) {
+            $a = 0;
+            for (; $a < $urlLength; $a++) {
                 if ($url[$a] != $curURL[$a]) {
                     break;
                 }
index 893db55..92ced4b 100644 (file)
@@ -134,7 +134,7 @@ class Response extends Message implements ResponseInterface
         $this->statusCode = (int)$statusCode;
 
         $this->reasonPhrase = $this->availableStatusCodes[$this->statusCode];
-        list($this->headerNames, $headers) = $this->filterHeaders($headers);
+        $headers = $this->filterHeaders($headers)[1];
         $this->assertHeaders($headers);
         $this->headers = $headers;
     }
index 85f535e..478dfc2 100644 (file)
@@ -157,7 +157,7 @@ class LanguageStore implements \TYPO3\CMS\Core\SingletonInterface
         ];
         $fileWithoutExtension = GeneralUtility::getFileAbsFileName($this->getFileReferenceWithoutExtension($fileReference));
         foreach ($this->supportedExtensions as $extension) {
-            if (@is_file(($fileWithoutExtension . '.' . $extension))) {
+            if (@is_file($fileWithoutExtension . '.' . $extension)) {
                 $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
                 $this->configuration[$fileReference]['fileExtension'] = $extension;
                 break;
index b97cf87..248a8c9 100644 (file)
@@ -42,7 +42,7 @@ class LocallangXmlParser extends AbstractXmlParser
     {
         $this->sourcePath = $sourcePath;
         $this->languageKey = $languageKey;
-        $this->charset = $this->getCharset($languageKey, $charset);
+        $this->charset = $this->getCharset($charset);
         // Parse source
         $parsedSource = $this->parseXmlFile();
         // Parse target
index 49dfa96..9a5a34c 100644 (file)
@@ -33,6 +33,18 @@ class PackageConstraint
     protected $value;
 
     /**
+     * Minimum version for the constraint
+     * @var string
+     */
+    protected $minVersion;
+
+    /**
+     * Maximum version for the constraint
+     * @var string
+     */
+    protected $maxVersion;
+
+    /**
      * Meta data constraint constructor
      *
      * @param string $constraintType
index 418ebc2..62798e3 100644 (file)
@@ -83,7 +83,7 @@ class DependencyOrderingService
      * @param array $dependencies
      * @param string $beforeKey The key to use in a dependency which specifies the "before"-relation. eg. 'sortBefore', 'loadBefore'
      * @param string $afterKey The key to use in a dependency which specifies the "after"-relation. eg. 'sortAfter', 'loadAfter'
-     * @return \bool[][] The dependency graph
+     * @return bool[][] The dependency graph
      */
     public function buildDependencyGraph(array $dependencies, $beforeKey = 'before', $afterKey = 'after')
     {
index b016595..38a08c6 100644 (file)
@@ -216,6 +216,7 @@ class TimeTracker implements SingletonInterface
         }
         end($this->currentHashPointer);
         $k = current($this->currentHashPointer);
+        $placeholder = '';
         // Enlarge the "details" column by adding a span
         if (strlen($content) > 30) {
             $placeholder = '<br /><span style="width: 300px; height: 1px; display: inline-block;"></span>';
index 5c2f209..40244d6 100644 (file)
@@ -77,7 +77,7 @@ class ConfigurationForm extends ExtendedTemplateService
     {
         $temp = $this->flatSetup;
         $this->flatSetup = [];
-        $this->flattenSetup($valueArray, '', '');
+        $this->flattenSetup($valueArray, '');
         $this->objReg = $this->ext_realValues = $this->flatSetup;
         $this->flatSetup = $temp;
         foreach ($theConstants as $k => $p) {
index 1bf8a72..6a4e23b 100644 (file)
@@ -437,6 +437,7 @@ class CommandUtility
     public static function escapeShellArguments(array $input)
     {
         $isUTF8Filesystem = !empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']);
+        $currentLocale = false;
         if ($isUTF8Filesystem) {
             $currentLocale = setlocale(LC_CTYPE, 0);
             setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
@@ -444,7 +445,7 @@ class CommandUtility
 
         $output = array_map('escapeshellarg', $input);
 
-        if ($isUTF8Filesystem) {
+        if ($isUTF8Filesystem && $currentLocale !== false) {
             setlocale(LC_CTYPE, $currentLocale);
         }
 
index c44bd9a..aa839d3 100644 (file)
@@ -912,8 +912,8 @@ class ExtendedFileUtility extends BasicFileUtility
             return false;
         }
         $resultObject = null;
+        $folderName = $cmds['data'];
         try {
-            $folderName = $cmds['data'];
             $resultObject = $targetFolderObject->createFolder($folderName);
             $this->writeLog(6, 0, 1, 'Directory "%s" created in "%s"', [$folderName, $targetFolderObject->getIdentifier()]);
             $this->addMessageToFlashMessageQueue('FileUtility.DirectoryCreatedIn', [$folderName, $targetFolderObject->getIdentifier()], FlashMessage::OK);
index 9956f8e..5d8eb7c 100644 (file)
@@ -2413,6 +2413,7 @@ class GeneralUtility
      */
     public static function get_dirs($path)
     {
+        $dirs = null;
         if ($path) {
             if (is_dir($path)) {
                 $dir = scandir($path);
@@ -2946,7 +2947,6 @@ class GeneralUtility
             case 'REMOTE_HOST':
 
             case 'QUERY_STRING':
-                $retVal = '';
                 if (isset($_SERVER[$getEnvName])) {
                     $retVal = $_SERVER[$getEnvName];
                 }
@@ -2969,9 +2969,8 @@ class GeneralUtility
                 }
                 $commonEnd = strrev(implode('/', $acc));
                 if ((string)$commonEnd !== '') {
-                    $DR = substr($SFN, 0, -(strlen($commonEnd) + 1));
+                    $retVal = substr($SFN, 0, -(strlen($commonEnd) + 1));
                 }
-                $retVal = $DR;
                 break;
             case 'TYPO3_HOST_ONLY':
                 $httpHost = self::getIndpEnv('HTTP_HOST');
index 5643e6e..1fa0312 100644 (file)
@@ -29,7 +29,7 @@ class UploadedFileTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
 
     protected function setUp()
     {
-        $this->tmpfile = null;
+        $this->tmpFile = null;
     }
 
     protected function tearDown()
index 6b27197..aa87762 100644 (file)
@@ -45,7 +45,7 @@ class FolderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         if ($mockedStorage === null) {
             $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
         }
-        return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name, 0);
+        return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name);
     }
 
     /**
index 21f9665..1b28a30 100644 (file)
@@ -2509,7 +2509,7 @@ class ArrayUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
             'stan',
         ];
 
-        $this->assertNull(ArrayUtility::assertAllArrayKeysAreValid($arrayToTest, $allowedArrayKeys));
+        ArrayUtility::assertAllArrayKeysAreValid($arrayToTest, $allowedArrayKeys);
     }
 
     /**