Skip to content

Commit

Permalink
Improve UnionTypeHelper::sortTypes() stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard van Velzen authored and ondrejmirtes committed Sep 23, 2022
1 parent 2531ca3 commit e2384d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/Type/UnionTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function sortTypes(array $types): array

if ($a instanceof AccessoryType) {
if ($b instanceof AccessoryType) {
return strcasecmp($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
}

return 1;
Expand Down Expand Up @@ -108,7 +108,7 @@ public static function sortTypes(array $types): array
}

if ($a instanceof ConstantStringType && $b instanceof ConstantStringType) {
return strcasecmp($a->getValue(), $b->getValue());
return self::compareStrings($a->getValue(), $b->getValue());
}

if ($a instanceof ConstantArrayType && $b instanceof ConstantArrayType) {
Expand All @@ -122,12 +122,22 @@ public static function sortTypes(array $types): array
return 1;
}

return strcasecmp($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
}

return strcasecmp($a->describe(VerbosityLevel::typeOnly()), $b->describe(VerbosityLevel::typeOnly()));
return self::compareStrings($a->describe(VerbosityLevel::typeOnly()), $b->describe(VerbosityLevel::typeOnly()));
});
return $types;
}

private static function compareStrings(string $a, string $b): int
{
$cmp = strcasecmp($a, $b);
if ($cmp !== 0) {
return $cmp;
}

return $a <=> $b;
}

}

0 comments on commit e2384d6

Please sign in to comment.