[ticket/17512] Fix sorting types check logic

PHPBB-17512
This commit is contained in:
rxu 2025-05-15 20:34:16 +07:00
parent 146f917d19
commit 6a0c949ed1
No known key found for this signature in database
GPG key ID: 955F0567380E586A

View file

@ -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,8 +99,12 @@ 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)
{
$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)];
@ -108,4 +112,5 @@ class UnionTypesCheckSniff implements Sniff
}
}
}
}
}