From 8fdd5af7243e7fbc62c2cd799dd7c833ac578f03 Mon Sep 17 00:00:00 2001 From: Oliver Klee <typo3-coding@oliverklee.de> Date: Thu, 16 Apr 2020 18:02:40 +0200 Subject: [PATCH] [TASK] Use more specific array type annotations in GeneralUtility Having PHPDoc parameter and return type annotations like "string[]" instead of the generic "array" will help both static code analysers as well as human readers. Resolves: #91065 Releases: master, 9.5 Change-Id: I7d39241d81ce607e9ca6050cc82126420e1e756e Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64203 Tested-by: Alexander Schnitzler <git@alexanderschnitzler.de> Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Jonas Eberle <flightvision@googlemail.com> Tested-by: Daniel Goerz <daniel.goerz@posteo.de> Reviewed-by: Alexander Schnitzler <git@alexanderschnitzler.de> Reviewed-by: Jonas Eberle <flightvision@googlemail.com> Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de> --- .../core/Classes/Utility/GeneralUtility.php | 50 +++++++++---------- .../Tests/Unit/Utility/GeneralUtilityTest.php | 8 +-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php index e7a8a376d77e..de4216ad7ad1 100644 --- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php +++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php @@ -64,21 +64,21 @@ class GeneralUtility * Singleton instances returned by makeInstance, using the class names as * array keys * - * @var array<\TYPO3\CMS\Core\SingletonInterface> + * @var array<string, SingletonInterface> */ protected static $singletonInstances = []; /** * Instances returned by makeInstance, using the class names as array keys * - * @var array<object> + * @var array<string, array<object>> */ protected static $nonSingletonInstances = []; /** * Cache for makeInstance with given class name and final class names to reduce number of self::getClassName() calls * - * @var array Given class name => final class name + * @var array<string, class-string> Given class name => final class name */ protected static $finalClassNameCache = []; @@ -91,7 +91,7 @@ class GeneralUtility protected static $applicationContext; /** - * @var array + * @var array<string, mixed> */ protected static $indpEnvCache = []; @@ -707,7 +707,7 @@ class GeneralUtility * Splits a reference to a file in 5 parts * * @param string $fileNameWithPath File name with path to be analyzed (must exist if open_basedir is set) - * @return array Contains keys [path], [file], [filebody], [fileext], [realFileext] + * @return array<string, string> Contains keys [path], [file], [filebody], [fileext], [realFileext] */ public static function split_fileref($fileNameWithPath) { @@ -815,7 +815,7 @@ class GeneralUtility * * @param string $string Input string, eg "123 + 456 / 789 - 4 * @param string $operators Operators to split by, typically "/+-* - * @return array Array with operators and operands separated. + * @return array<int, array<int, string>> Array with operators and operands separated. * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::calc() * @see \TYPO3\CMS\Frontend\Imaging\GifBuilder::calcOffset() */ @@ -983,7 +983,7 @@ class GeneralUtility * @param string $string The string to explode * @param bool $removeEmptyValues If set, all empty values (='') will NOT be set in output * @param int $limit If positive, the result will contain a maximum of limit elements, - * @return array Exploded values, all converted to integers + * @return int[] Exploded values, all converted to integers */ public static function intExplode($delimiter, $string, $removeEmptyValues = false, $limit = 0) { @@ -1020,7 +1020,7 @@ class GeneralUtility * @param string $delimiter Delimiter string to explode with * @param string $string The string to explode * @param int $count Number of array entries - * @return array Exploded values + * @return string[] Exploded values */ public static function revExplode($delimiter, $string, $count = 0) { @@ -1050,7 +1050,7 @@ class GeneralUtility * @param int $limit If limit is set and positive, the returned array will contain a maximum of limit elements with * the last element containing the rest of string. If the limit parameter is negative, all components * except the last -limit are returned. - * @return array Exploded values + * @return string[] Exploded values */ public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0) { @@ -1112,7 +1112,7 @@ class GeneralUtility * then this method is for you. * * @param string $string GETvars string - * @return array Array of values. All values AND keys are rawurldecoded() as they properly should be. But this means that any implosion of the array again must rawurlencode it! + * @return array<string, string> Array of values. All values AND keys are rawurldecoded() as they properly should be. But this means that any implosion of the array again must rawurlencode it! * @see implodeArrayForUrl() */ public static function explodeUrl2Array($string) @@ -1183,7 +1183,7 @@ class GeneralUtility * If an attribute is empty, then the value for the key is empty. You can check if it existed with isset() * * @param string $tag HTML-tag string (or attributes only) - * @return array Array with the attribute values. + * @return string[] Array with the attribute values. */ public static function get_tag_attributes($tag) { @@ -1219,7 +1219,7 @@ class GeneralUtility * Removes tag-name if found * * @param string $tag HTML-tag string (or attributes only) - * @return array Array with the attribute values. + * @return string[] Array with the attribute values. */ public static function split_tag_attributes($tag) { @@ -1252,7 +1252,7 @@ class GeneralUtility /** * Implodes attributes in the array $arr for an attribute list in eg. and HTML tag (with quotes) * - * @param array $arr Array with attribute key/value pairs, eg. "bgcolor"=>"red", "border"=>0 + * @param array<string, string> $arr Array with attribute key/value pairs, eg. "bgcolor" => "red", "border" => "0" * @param bool $xhtmlSafe If set the resulting attribute list will have a) all attributes in lowercase (and duplicates weeded out, first entry taking precedence) and b) all values htmlspecialchar()'ed. It is recommended to use this switch! * @param bool $dontOmitBlankAttribs If TRUE, don't check if values are blank. Default is to omit attributes with blank values. * @return string Imploded attributes, eg. 'bgcolor="red" border="0"' @@ -1641,7 +1641,7 @@ class GeneralUtility /** * This implodes an array of XML parts (made with xml_parse_into_struct()) into XML again. * - * @param array $vals An array of XML parts, see xml2tree + * @param array<int, array<string, mixed>> $vals An array of XML parts, see xml2tree * @return string Re-compiled XML data. */ public static function xmlRecompileFromStructValArray(array $vals) @@ -2146,7 +2146,7 @@ class GeneralUtility * Will return null if provided path is false. * * @param string $path Path to list directories from - * @return array|string|null Returns an array with the directory entries as values. If no path is provided, the return value will be null. + * @return string[]|string|null Returns an array with the directory entries as values. If no path is provided, the return value will be null. */ public static function get_dirs($path) { @@ -2177,7 +2177,7 @@ class GeneralUtility * @param bool $prependPath If TRUE, the full path to the file is returned. If FALSE only the file name is returned. * @param string $order The sorting order. The default sorting order is alphabetical. Setting $order to 'mtime' will sort the files by modification time. * @param string $excludePattern A regular expression pattern of file names to exclude. For example: 'clear.gif' or '(clear.gif|.htaccess)'. The pattern will be wrapped with: '/^' and '$/'. - * @return array|string Array of the files found, or an error message in case the path could not be opened. + * @return array<string, string>|string Array of the files found, or an error message in case the path could not be opened. */ public static function getFilesInDir($path, $extensionList = '', $prependPath = false, $order = '', $excludePattern = '') { @@ -2236,13 +2236,13 @@ class GeneralUtility /** * Recursively gather all files and folders of a path. * - * @param array $fileArr Empty input array (will have files added to it) + * @param string[] $fileArr Empty input array (will have files added to it) * @param string $path The path to read recursively from (absolute) (include trailing slash!) * @param string $extList Comma list of file extensions: Only files with extensions in this list (if applicable) will be selected. * @param bool $regDirs If set, directories are also included in output. * @param int $recursivityLevels The number of levels to dig down... * @param string $excludePattern regex pattern of files/directories to exclude - * @return array An array with the found files/directories. + * @return array<string, string> An array with the found files/directories. */ public static function getAllFilesAndFoldersInPath(array $fileArr, $path, $extList = '', $regDirs = false, $recursivityLevels = 99, $excludePattern = '') { @@ -2264,9 +2264,9 @@ class GeneralUtility /** * Removes the absolute part of all files/folders in fileArr * - * @param array $fileArr The file array to remove the prefix from + * @param string[] $fileArr The file array to remove the prefix from * @param string $prefixToRemove The prefix path to remove (if found as first part of string!) - * @return array|string The input $fileArr processed, or a string with an error message, when an error occurred. + * @return string[]|string The input $fileArr processed, or a string with an error message, when an error occurred. */ public static function removePrefixPathFromList(array $fileArr, $prefixToRemove) { @@ -3379,7 +3379,7 @@ class GeneralUtility * the instance of a specific class. * * @param string $className name of the class to instantiate, must not be empty and not start with a backslash - * @param array<int,mixed> $constructorArguments Arguments for the constructor + * @param array<int, mixed> $constructorArguments Arguments for the constructor * @return object the created instance * @throws \InvalidArgumentException if $className is empty or starts with a backslash */ @@ -3440,7 +3440,7 @@ class GeneralUtility * container. * * @param string $className name of the class to instantiate - * @param array<int,mixed> $constructorArguments Arguments for the constructor + * @param array<int, mixed> $constructorArguments Arguments for the constructor * @return object the created instance * @internal */ @@ -3558,7 +3558,7 @@ class GeneralUtility * manipulated in tests with setSingletonInstance() * * @internal - * @param array $newSingletonInstances $className => $object + * @param array<string, SingletonInterface> $newSingletonInstances */ public static function resetSingletonInstances(array $newSingletonInstances) { @@ -3578,7 +3578,7 @@ class GeneralUtility * setSingletonInstance() in tests. * * @internal - * @return array $className => $object + * @return array<string, SingletonInterface> */ public static function getSingletonInstances() { @@ -3594,7 +3594,7 @@ class GeneralUtility * have no left over instances that were previously added using addInstance(). * * @internal - * @return array $className => $objects[] + * @return array<string, array<object>> */ public static function getInstances() { diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php index 8d6fa5001f79..aceb8f3b1ec5 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php @@ -787,11 +787,11 @@ class GeneralUtilityTest extends UnitTestCase '' ], 'number without operator returns array with plus and number' => [ - [['+', 42]], + [['+', '42']], '42' ], 'two numbers with asterisk return first number with plus and second number with asterisk' => [ - [['+', 42], ['*', 31]], + [['+', '42'], ['*', '31']], '42 * 31' ] ]; @@ -801,9 +801,9 @@ class GeneralUtilityTest extends UnitTestCase * @test * @dataProvider splitCalcDataProvider */ - public function splitCalcCorrectlySplitsExpression($expected, $expression) + public function splitCalcCorrectlySplitsExpression(array $expected, string $expression): void { - self::assertEquals($expected, GeneralUtility::splitCalc($expression, '+-*/')); + self::assertSame($expected, GeneralUtility::splitCalc($expression, '+-*/')); } /////////////////////////////// -- GitLab