Skip to content

Commit

Permalink
Improve argument validation
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jun 9, 2023
1 parent de59dd7 commit c42868e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/MTProto.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ final class MTProto implements TLCallback, LoggerGetter
*
* @var string
*/
const RELEASE = '8.0.0-beta90';
const RELEASE = '8.0.0-beta91';
/**
* We're not logged in.
*
Expand Down
24 changes: 17 additions & 7 deletions src/Namespace/AbstractAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,30 @@ public function setWrapper(APIWrapper $wrapper): void
*/
public function __call(string $name, array $arguments)
{
if ($arguments && !isset($arguments[0])) {
$arguments = [$arguments];
if (!isset($arguments[0])) {
// Named arguments
$args = $arguments;
$aargs = [];
} else {
// Legacy arguments
$args = $arguments[0] ?? [];
$aargs = $arguments[1] ?? [];
if (!\array_is_list($arguments)
|| \count($arguments) > 2
|| !\is_array($args)
|| !\is_array($aargs)
|| isset($args['_'])
|| (isset($args[0]) && !isset($aargs['multiple']))
) {
throw new InvalidArgumentException('Parameter names must be provided!');
}
}

$name = $this->namespace.'.'.$name;
if (isset(Blacklist::BLACKLIST[$name])) {
throw new Exception(Blacklist::BLACKLIST[$name]);
}

$aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : [];
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
if (isset($args[0]) && !isset($args['multiple'])) {
throw new InvalidArgumentException('Parameter names must be provided!');
}
return $this->wrapper->getAPI()->methodCallAsyncRead($name, $args, $aargs);
}
}

0 comments on commit c42868e

Please sign in to comment.