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 */
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->handler = $handler;
$this->signature = $signature;
}
public function getName(): string
@@ -53,18 +56,23 @@ class Command
} else {
$type = $parameter->getType();
if ($type instanceof ReflectionNamedType && $type->isBuiltin()) {
$type = $type->getName();
$type = ($type->allowsNull() ? "?" : "") . $type->getName();
} else {
$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;
}
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) { });
$expect = [
'a' => 'string',
'b' => 'int',
'c' => '?bool'
'b' => '?int',
'c' => 'bool=false'
];
$this->assertEquals($expect, $command->parameters());
}