Skip to content

Commit

Permalink
Refactor findOption to use array_find for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Naneynonn committed Jul 30, 2024
1 parent 12126cb commit 013cd14
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/Interaction/CommandInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Ragnarok\Fenrir\Rest\Helpers\Webhook\EditWebhookBuilder;
use React\Promise\ExtendedPromiseInterface;

use function Freezemage\ArrayUtils\find as array_find;

class CommandInteraction
{
/** @var OptionStructure[] */
Expand Down Expand Up @@ -71,19 +73,13 @@ private function findOption(array $options, array $segments): ?OptionStructure
{
$currentSegment = array_shift($segments);

foreach ($options as $opt) {
if ($opt->name === $currentSegment) {
if (empty($segments)) {
return $opt;
}
$option = array_find($options, fn (OptionStructure $option) => $option->name === $currentSegment);

if (!empty($opt->options)) {
return $this->findOption($opt->options, $segments);
}
}
if (empty($segments)) {
return $option;
}

return null;
return empty($option->options) ? null : $this->findOption($option->options, $segments);
}

public function hasOption(string $path): bool
Expand Down

0 comments on commit 013cd14

Please sign in to comment.