From 6a0c949ed1e3ecddc8d878142b4233868d18db79 Mon Sep 17 00:00:00 2001 From: rxu Date: Thu, 15 May 2025 20:34:16 +0700 Subject: [PATCH] [ticket/17512] Fix sorting types check logic PHPBB-17512 --- .../CodeLayout/UnionTypesCheckSniff.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/build/code_sniffer/phpbb/Sniffs/CodeLayout/UnionTypesCheckSniff.php b/build/code_sniffer/phpbb/Sniffs/CodeLayout/UnionTypesCheckSniff.php index 1c6b76b0fe..64b30a859e 100644 --- a/build/code_sniffer/phpbb/Sniffs/CodeLayout/UnionTypesCheckSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/CodeLayout/UnionTypesCheckSniff.php @@ -81,9 +81,9 @@ class UnionTypesCheckSniff implements Sniff $data = [$type, $type_hint]; $phpcsFile->addError($error, $stack_pointer, 'ShortNullableSyntax', $data); } - else if (($types_array = explode('|', $type_hint)) > 1) // Check union type layout + else if ((count($types_array = explode('|', $type_hint))) > 1) // Check union type layout { - $types_array_sorted = $types_array_null_less = $types_array; + $types_array_null_less = $types_array; // Check 'null' to be the last element $null_position = array_search('null', $types_array); @@ -99,12 +99,17 @@ class UnionTypesCheckSniff implements Sniff { array_splice($types_array_null_less, $null_position, 1); } - sort($types_array_sorted); - if (!empty(array_diff_assoc($types_array_null_less, $types_array_sorted))) + + if (count($types_array_null_less) > 1) { - $error = 'Union type elements must be sorted alphabetically excepting the "null" type hint must be the last if any; found %s'; - $data = [implode('|', $types_array)]; - $phpcsFile->addError($error, $stack_pointer, 'AlphabeticalSort', $data); + $types_array_null_less_sorted = $types_array_null_less; + sort($types_array_null_less_sorted); + if (!empty(array_diff_assoc($types_array_null_less, $types_array_null_less_sorted))) + { + $error = 'Union type elements must be sorted alphabetically excepting the "null" type hint must be the last if any; found %s'; + $data = [implode('|', $types_array)]; + $phpcsFile->addError($error, $stack_pointer, 'AlphabeticalSort', $data); + } } } }