From 5c08e8f5d8066678be76325b6c4cc2c30c65486d Mon Sep 17 00:00:00 2001 From: Oliver Klee <typo3-coding@oliverklee.de> Date: Thu, 27 Jul 2023 10:21:55 +0200 Subject: [PATCH] [!!!][TASK] Add native type declarations to GU HTML/XML methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves: #102116 Releases: main Change-Id: If78eabc7e7a729d04e6501bb3d9ce1fae0c373c0 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80183 Tested-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: core-ci <typo3@b13.com> Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Reviewed-by: Stefan Bürk <stefan@buerk.tech> --- Build/phpstan/phpstan-baseline.neon | 5 ---- .../core/Classes/Utility/GeneralUtility.php | 24 +++++++++---------- ...ypeDeclarationsInGeneralUtilityMethods.rst | 9 +++++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon index a78d7a972105..d959bdcda24b 100644 --- a/Build/phpstan/phpstan-baseline.neon +++ b/Build/phpstan/phpstan-baseline.neon @@ -320,11 +320,6 @@ parameters: count: 1 path: ../../typo3/sysext/core/Classes/DataHandling/DataHandler.php - - - message: "#^If condition is always true\\.$#" - count: 1 - path: ../../typo3/sysext/core/Classes/DataHandling/DataHandler.php - - message: "#^Left side of \\|\\| is always false\\.$#" count: 1 diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php index 375ad8ecddef..77897d25f86f 100644 --- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php +++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php @@ -917,7 +917,7 @@ class GeneralUtility * @param bool $decodeEntities Whether to decode HTML entities * @return array<string, string> Array with the attribute values. */ - public static function get_tag_attributes($tag, bool $decodeEntities = false) + public static function get_tag_attributes(string $tag, bool $decodeEntities = false): array { $components = self::split_tag_attributes($tag); // Attribute name is stored here @@ -953,7 +953,7 @@ class GeneralUtility * @param string $tag HTML-tag string (or attributes only) * @return string[] Array with the attribute values. */ - public static function split_tag_attributes($tag) + public static function split_tag_attributes(string $tag): array { $tag_tmp = trim(preg_replace('/^<[^[:space:]]*/', '', trim($tag)) ?? ''); // Removes any > in the end of the string @@ -989,7 +989,7 @@ class GeneralUtility * @param bool $keepBlankAttributes 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"' */ - public static function implodeAttributes(array $arr, $xhtmlSafe = false, $keepBlankAttributes = false) + public static function implodeAttributes(array $arr, bool $xhtmlSafe = false, bool $keepBlankAttributes = false): string { if ($xhtmlSafe) { $newArr = []; @@ -1019,7 +1019,7 @@ class GeneralUtility * @param array<string, string> $attributes (optional) script tag HTML attributes * @return string The wrapped JS code, ready to put into a XHTML page */ - public static function wrapJS(string $string, array $attributes = []) + public static function wrapJS(string $string, array $attributes = []): string { if (trim($string)) { // remove nl from the beginning @@ -1044,9 +1044,9 @@ class GeneralUtility * @param string $string XML data input * @param int $depth Number of element levels to resolve the XML into an array. Any further structure will be set as XML. * @param array $parserOptions Options that will be passed to PHP's xml_parser_set_option() - * @return mixed The array with the parsed structure unless the XML parser returns with an error in which case the error message string is returned. + * @return array|string The array with the parsed structure unless the XML parser returns with an error in which case the error message string is returned. */ - public static function xml2tree($string, $depth = 999, $parserOptions = []) + public static function xml2tree(string $string, int $depth = 999, array $parserOptions = []): array|string { $parser = xml_parser_create(); $vals = []; @@ -1126,7 +1126,7 @@ class GeneralUtility * @return string An XML string made from the input content in the array. * @see xml2array() */ - public static function array2xml(array $array, $NSprefix = '', $level = 0, $docTag = 'phparray', $spaceInd = 0, array $options = [], array $stackData = []) + public static function array2xml(array $array, string $NSprefix = '', int $level = 0, string $docTag = 'phparray', int $spaceInd = 0, array $options = [], array $stackData = []): string { // The list of byte values which will trigger binary-safe storage. If any value has one of these char values in it, it will be encoded in base64 $binaryChars = "\0" . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(11) . chr(12) . chr(14) . chr(15) . chr(16) . chr(17) . chr(18) . chr(19) . chr(20) . chr(21) . chr(22) . chr(23) . chr(24) . chr(25) . chr(26) . chr(27) . chr(28) . chr(29) . chr(30) . chr(31); @@ -1237,11 +1237,11 @@ class GeneralUtility * @param string $string XML content to convert into an array * @param string $NSprefix The tag-prefix resolve, eg. a namespace like "T3:" * @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array - * @return mixed If the parsing had errors, a string with the error message is returned. Otherwise an array with the content. + * @return array|string If the parsing had errors, a string with the error message is returned. Otherwise an array with the content. * @see array2xml() * @see xml2arrayProcess() */ - public static function xml2array($string, $NSprefix = '', $reportDocTag = false) + public static function xml2array(string $string, string $NSprefix = '', bool $reportDocTag = false): array|string { $runtimeCache = static::makeInstance(CacheManager::class)->getCache('runtime'); $firstLevelCache = $runtimeCache->get('generalUtilityXml2Array') ?: []; @@ -1261,10 +1261,10 @@ class GeneralUtility * @param string $string XML content to convert into an array * @param string $NSprefix The tag-prefix resolve, eg. a namespace like "T3:" * @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array - * @return mixed If the parsing had errors, a string with the error message is returned. Otherwise an array with the content. + * @return array|string If the parsing had errors, a string with the error message is returned. Otherwise an array with the content. * @see array2xml() */ - public static function xml2arrayProcess($string, $NSprefix = '', $reportDocTag = false) + public static function xml2arrayProcess(string $string, string $NSprefix = '', bool $reportDocTag = false): array|string { $string = trim((string)$string); // Create parser: @@ -1370,7 +1370,7 @@ class GeneralUtility * @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) + public static function xmlRecompileFromStructValArray(array $vals): string { $XMLcontent = ''; foreach ($vals as $val) { diff --git a/typo3/sysext/core/Documentation/Changelog/13.0/Breaking-101305-IntroduceTypeDeclarationsInGeneralUtilityMethods.rst b/typo3/sysext/core/Documentation/Changelog/13.0/Breaking-101305-IntroduceTypeDeclarationsInGeneralUtilityMethods.rst index d4f08dd44c64..dba9068441f9 100644 --- a/typo3/sysext/core/Documentation/Changelog/13.0/Breaking-101305-IntroduceTypeDeclarationsInGeneralUtilityMethods.rst +++ b/typo3/sysext/core/Documentation/Changelog/13.0/Breaking-101305-IntroduceTypeDeclarationsInGeneralUtilityMethods.rst @@ -14,13 +14,16 @@ Description Native return and param type declarations have been introduced for the following methods of :php:`\TYPO3\CMS\Core\Utility\GeneralUtility`: +- :php:`array2xml` - :php:`cmpFQDN` - :php:`cmpIP` - :php:`cmpIPv4` - :php:`cmpIPv6` - :php:`explodeUrl2Array` - :php:`getUrl` +- :php:`get_tag_attributes` - :php:`implodeArrayForUrl` +- :php:`implodeAttributes` - :php:`intExplode` - :php:`isOnCurrentHost` - :php:`isValidUrl` @@ -28,12 +31,18 @@ methods of :php:`\TYPO3\CMS\Core\Utility\GeneralUtility`: - :php:`normalizeIPv6` - :php:`revExplode` - :php:`sanitizeLocalUrl` +- :php:`split_tag_attributes` - :php:`trimExplode` - :php:`validEmail` - :php:`validIP` - :php:`validIPv4` - :php:`validIPv6` - :php:`validPathStr` +- :php:`wrapJS` +- :php:`xml2arrayProcess` +- :php:`xml2array` +- :php:`xml2tree` +- :php:`xmlRecompileFromStructValArray` Impact ====== -- GitLab