Skip to content

Commit

Permalink
Tidy up types (#96)
Browse files Browse the repository at this point in the history
* Tidy up types

* Check output is set

* Update deps
  • Loading branch information
g105b authored Feb 12, 2021
1 parent 2442711 commit ff2640f
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 76 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
namespace Gt\Cli;

use Gt\Cli\Argument\ArgumentValueList;
use Gt\Cli\Argument\NotEnoughArgumentsException;
use Gt\Cli\Command\Command;
use Gt\Cli\Command\CommandException;
use Gt\Cli\Command\HelpCommand;
use Gt\Cli\Command\InvalidCommandException;
use Gt\Cli\Command\VersionCommand;
use Gt\Cli\Parameter\MissingRequiredParameterException;
use Gt\Cli\Parameter\MissingRequiredParameterValueException;
use Gt\Cli\Argument\ArgumentList;

class Application {
protected $description;
protected $arguments;
protected $commands;
protected $stream;
protected $helpCommand;
protected $versionCommand;
protected string $description;
protected ?ArgumentList $arguments;
/** @var Command[] */
protected array $commands;
protected Stream $stream;
protected HelpCommand $helpCommand;
protected VersionCommand $versionCommand;

public function __construct(
string $description,
Expand Down Expand Up @@ -53,7 +51,7 @@ public function __construct(
$this->versionCommand->setOutput($this->stream);
}

public function setStream($in, $out, $error) {
public function setStream(string $in, string $out, string $error):void {
$this->stream->setStream($in, $out, $error);
}

Expand Down Expand Up @@ -137,4 +135,4 @@ protected function findCommandByName(string $name):Command {

throw new InvalidCommandException($name);
}
}
}
6 changes: 3 additions & 3 deletions src/Argument/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

abstract class Argument {
const USER_DATA = "__user-data__";
protected $key;
protected $value;
protected string $key;
protected ?string $value;

public function __construct(string $rawKey, string $value = null) {
$this->key = $this->processRawKey($rawKey);
Expand All @@ -29,4 +29,4 @@ public function getKey():string {
public function getValue():?string {
return $this->value;
}
}
}
39 changes: 25 additions & 14 deletions src/Argument/ArgumentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
use Gt\Cli\Parameter\Parameter;
use Iterator;

/** @implements Iterator<int, Argument> */
class ArgumentList implements Iterator {
const DEFAULT_COMMAND = "help";

protected $script;
protected string $script;
/** @var Argument[] */
protected $argumentList = [];
protected $iteratorIndex;
protected array $argumentList = [];
protected int $iteratorIndex;

public function __construct(string $script, string...$arguments) {
$this->script = $script;
Expand All @@ -25,18 +26,20 @@ public function getCommandName():string {
return $this->argumentList[0]->getValue();
}

/** @param string[] $arguments */
protected function buildArgumentList(array $arguments):void {
$commandArgument = array_shift($arguments);
if($commandArgument) {
$this->argumentList []= new CommandArgument(
$commandArgument
array_push(
$this->argumentList,
new CommandArgument($commandArgument)
);
}
else {
$defaultCommandArgument = new CommandArgument(
self::DEFAULT_COMMAND
);
$this->argumentList []= $defaultCommandArgument;
array_push($this->argumentList, $defaultCommandArgument);
}

$skipNextArgument = false;
Expand Down Expand Up @@ -82,19 +85,27 @@ protected function buildArgumentList(array $arguments):void {
}

if ($arg[1] === "-") {
$this->argumentList []= new LongOptionArgument(
$name,
$value
array_push(
$this->argumentList,
new LongOptionArgument(
$name,
$value
)
);
}
else {
$this->argumentList []= new ShortOptionArgument(
$arg,
$value
array_push($this->argumentList,
new ShortOptionArgument(
$arg,
$value
)
);
}
} else {
$this->argumentList []= new NamedArgument($arg);
array_push(
$this->argumentList,
new NamedArgument($arg)
);
}
}
}
Expand Down Expand Up @@ -177,4 +188,4 @@ public function getValueForParameter(Parameter $parameter):?string {

return null;
}
}
}
12 changes: 7 additions & 5 deletions src/Argument/ArgumentValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
namespace Gt\Cli\Argument;

class ArgumentValue {
protected $key;
protected $valueList;
protected $queueIndex;
protected string $key;
/** @var string[] */
protected array $valueList;
protected int $queueIndex;

public function __construct(string $key) {
$this->key = $key;
Expand All @@ -17,7 +18,7 @@ public function __toString():string {
}

public function push(string $value = null):void {
$this->valueList []= $value;
array_push($this->valueList, $value);
}

public function getKey():string {
Expand All @@ -30,7 +31,8 @@ public function get():?string {
return $value;
}

/** @return string[] */
public function getAll():array {
return $this->valueList;
}
}
}
22 changes: 12 additions & 10 deletions src/Argument/ArgumentValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

use Iterator;

/** @implements Iterator<int, ArgumentValue> */
class ArgumentValueList implements Iterator {
const DEFAULT_ARGUMENT_VALUE = "//\\DEFAULT ARGUMENT VALUE\\//";

/** @var ArgumentValue[] */
protected $valueList = [];
protected $argumentValueMap = [];
protected $iteratorIndex;
protected array $valueList = [];
/** @var array<string, ArgumentValue> */
protected array $argumentValueMap = [];
protected int $iteratorIndex;

public function set(string $key, string $value = null):void {
if($this->contains($key)) {
Expand All @@ -28,7 +30,7 @@ public function set(string $key, string $value = null):void {

public function get(
string $key,
$default = self::DEFAULT_ARGUMENT_VALUE
string $default = self::DEFAULT_ARGUMENT_VALUE
):ArgumentValue {
// $default is handled in this manner because we want to allow null to be a
// valid value for the default.
Expand All @@ -48,12 +50,12 @@ public function contains(string $key):bool {
}

/** @link https://php.net/manual/en/iterator.rewind.php */
public function rewind() {
public function rewind():void {
$this->iteratorIndex = 0;
}

/** @link https://php.net/manual/en/iterator.key.php */
public function key() {
public function key():int {
return $this->iteratorIndex;
}

Expand All @@ -65,19 +67,19 @@ public function valid():bool {
}

/** @link https://php.net/manual/en/iterator.current.php */
public function current() {
public function current():?ArgumentValue {
if(!$this->iteratorIndex) {
$this->rewind();
}
return $this->valueList[$this->iteratorIndex] ?? null;
}

/** @link https://php.net/manual/en/iterator.next.php */
public function next() {
public function next():void {
$this->iteratorIndex++;
}

public function first() {
public function first():?ArgumentValue {
return $this->valueList[0] ?? null;
}
}
}
4 changes: 2 additions & 2 deletions src/Argument/NamedArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
namespace Gt\Cli\Argument;

class NamedArgument extends Argument {
public function __construct($value) {
public function __construct(string $value) {
parent::__construct("", $value);
}

protected function processRawKey(string $rawKey): string {
return "";
}
}
}
11 changes: 5 additions & 6 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
use Gt\Cli\Stream;

abstract class Command {
/** @var Stream */
protected $output;
protected ?Stream $output;

public function setOutput(Stream $output = null) {
public function setOutput(Stream $output = null):void {
$this->output = $output;
}

Expand Down Expand Up @@ -231,7 +230,7 @@ public function getArgumentValueList(
continue;
}
elseif($argument instanceof NamedArgument) {
/** @var NamedParameter $parameter */
/** @var NamedParameter|null $parameter */
$parameter = $namedParameterList[
$namedParameterIndex
] ?? null;
Expand Down Expand Up @@ -285,7 +284,7 @@ protected function write(
string $message,
string $streamName = Stream::OUT
):void {
if(is_null($this->output)) {
if(!isset($this->output) || is_null($this->output)) {
return;
}

Expand All @@ -298,4 +297,4 @@ protected function writeLine(
):void {
$this->write($message . PHP_EOL, $streamName);
}
}
}
8 changes: 4 additions & 4 deletions src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class HelpCommand extends Command {
const ALL_COMMANDS = "*";

protected $applicationDescription;
protected string $applicationDescription;
/** @var Command[] */
protected $applicationCommandList;
protected $scriptName;
protected array $applicationCommandList;
protected ?string $scriptName;

/**
* @param Command[] $applicationCommandList
Expand Down Expand Up @@ -140,4 +140,4 @@ protected function getHelpForCommand(string $commandName = null):string {

return $output;
}
}
}
13 changes: 7 additions & 6 deletions src/Command/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
use Gt\Cli\Parameter\Parameter;

class VersionCommand extends Command {
protected $script;
protected string $script;

public function __construct(string $script) {
$this->script = $script;
}

public function run(ArgumentValueList $arguments = null):void {
$this->writeLine($this->getVersion($arguments->get(
"command",
null
)));
$this->writeLine(
$this->getVersion($arguments->get(
"command"
)
));
}

public function getName():string {
Expand Down Expand Up @@ -88,4 +89,4 @@ protected function getVersion(string $command = null):string {

return $version;
}
}
}
Loading

0 comments on commit ff2640f

Please sign in to comment.