2 Commits
Author SHA1 Message Date
noccy 6671ec3eac Allow providing manual signature hint to commands 2025-01-18 21:58:51 +01:00
noccy 0bca797e8d Improve command signature logic 2025-01-18 14:23:46 +01:00
2 changed files with 13 additions and 5 deletions

No files matched your search

+11 -3
View File
@@ -19,10 +19,13 @@ class Command
/** @var callable $handler The handler */ /** @var callable $handler The handler */
private $handler; private $handler;
public function __construct(string $name, callable $handler) private ?array $signature;
public function __construct(string $name, callable $handler, ?array $signature = null)
{ {
$this->name = $name; $this->name = $name;
$this->handler = $handler; $this->handler = $handler;
$this->signature = $signature;
} }
public function getName(): string public function getName(): string
@@ -53,18 +56,23 @@ class Command
} else { } else {
$type = $parameter->getType(); $type = $parameter->getType();
if ($type instanceof ReflectionNamedType && $type->isBuiltin()) { if ($type instanceof ReflectionNamedType && $type->isBuiltin()) {
$type = $type->getName(); $type = ($type->allowsNull() ? "?" : "") . $type->getName();
} else { } else {
$type = null; $type = null;
} }
} }
if ($parameter->isOptional()) $type = "?{$type}"; if ($parameter->isDefaultValueAvailable()) $type = "{$type}=".\json_encode($parameter->getDefaultValue(),\JSON_UNESCAPED_SLASHES);
if ($type !== null) $args[$name] = $type; if ($type !== null) $args[$name] = $type;
} }
return $args; return $args;
} }
public function getSignature(): array
{
return $this->signature ?? $this->parameters();
}
} }
+2 -2
View File
@@ -31,8 +31,8 @@ class CommandTest extends \PHPUnit\Framework\TestCase
$command = new Command("test", function (string $a, ?int $b, bool $c = false) { }); $command = new Command("test", function (string $a, ?int $b, bool $c = false) { });
$expect = [ $expect = [
'a' => 'string', 'a' => 'string',
'b' => 'int', 'b' => '?int',
'c' => '?bool' 'c' => 'bool=false'
]; ];
$this->assertEquals($expect, $command->parameters()); $this->assertEquals($expect, $command->parameters());
} }