FormattedCommandAlias: reduce complexity of buildCommand()

This commit is contained in:
Dylan K. Taylor 2022-05-01 21:04:38 +01:00
parent 5d39d7a1c8
commit f4d71d0b48
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -117,18 +117,7 @@ class FormattedCommandAlias extends Command{
throw new \InvalidArgumentException("Missing required argument " . ($position + 1));
}
$replacement = "";
if($rest && $position < count($args)){
for($i = $position, $c = count($args); $i < $c; ++$i){
if($i !== $position){
$replacement .= " ";
}
$replacement .= $args[$i];
}
}elseif($position < count($args)){
$replacement .= $args[$position];
}
$replacement = self::buildReplacement($args, $position, $rest);
$formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end);
@ -143,4 +132,25 @@ class FormattedCommandAlias extends Command{
private static function inRange(int $i, int $j, int $k) : bool{
return $i >= $j && $i <= $k;
}
/**
* @param string[] $args
* @phpstan-param list<string> $args
*/
private static function buildReplacement(array $args, int $position, bool $rest) : string{
$replacement = "";
if($rest && $position < count($args)){
for($i = $position, $c = count($args); $i < $c; ++$i){
if($i !== $position){
$replacement .= " ";
}
$replacement .= $args[$i];
}
}elseif($position < count($args)){
$replacement .= $args[$position];
}
return $replacement;
}
}