Skip to content

Commit 45a15ca

Browse files
committed
Command improvements
1 parent 5d389c8 commit 45a15ca

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/Commands/Command.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,11 @@ private function makeRegexPattern(): array
184184

185185
$patterns = collect($matches)
186186
->mapWithKeys(function ($match) {
187-
$name = $match['name'] ?? null;
188-
189-
if ($name === null) {
190-
return [];
191-
}
192-
193187
$pattern = $match['pattern'] ?? '[^ ]++';
194188

195-
return [$name => "(?<$name>$pattern)?"];
189+
return [
190+
$match['name'] => "(?<{$match['name']}>{$pattern})?",
191+
];
196192
})
197193
->filter();
198194

@@ -219,35 +215,37 @@ private function relevantMessageSubString(): string
219215
2
220216
);
221217

222-
return $splice->count() === 2 ? $this->cutTextBetween($splice) : $this->cutTextFrom($splice);
218+
return match ($splice->count()) {
219+
2 => $this->cutTextBetween($splice),
220+
default => $this->cutTextFrom($splice),
221+
};
223222
}
224223

225224
private function allCommandOffsets(): Collection
226225
{
227-
$message = $this->getUpdate()
228-
->getMessage();
226+
$message = $this->getUpdate()->getMessage();
229227

230-
return $message->hasCommand() ?
231-
$message
228+
return $message->hasCommand()
229+
? $message
232230
->get('entities', collect())
233231
->filter(static fn (MessageEntity $entity): bool => $entity->type === 'bot_command')
234-
->pluck('offset') :
235-
collect();
232+
->pluck('offset')
233+
: collect();
236234
}
237235

238236
private function cutTextBetween(Collection $splice): string
239237
{
240-
return substr(
241-
$this->getUpdate()->getMessage()->text,
238+
return mb_substr(
239+
$this->getUpdate()->getMessage()->text ?? '',
242240
$splice->first(),
243241
$splice->last() - $splice->first()
244242
);
245243
}
246244

247245
private function cutTextFrom(Collection $splice): string
248246
{
249-
return substr(
250-
$this->getUpdate()->getMessage()->text,
247+
return mb_substr(
248+
$this->getUpdate()->getMessage()->text ?? '',
251249
$splice->first()
252250
);
253251
}

0 commit comments

Comments
 (0)