diff --git a/composer.lock b/composer.lock index 64c24da..8e682e4 100644 --- a/composer.lock +++ b/composer.lock @@ -884,16 +884,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.1", + "version": "9.5.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e7bdf4085de85a825f4424eae52c99a1cec2f360" + "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e7bdf4085de85a825f4424eae52c99a1cec2f360", - "reference": "e7bdf4085de85a825f4424eae52c99a1cec2f360", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f661659747f2f87f9e72095bb207bceb0f151cb4", + "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4", "shasum": "" }, "require": { @@ -979,7 +979,7 @@ "type": "github" } ], - "time": "2021-01-17T07:42:25+00:00" + "time": "2021-02-02T14:45:58+00:00" }, { "name": "sebastian/cli-parser", diff --git a/src/Application.php b/src/Application.php index b5895c8..2e32fd6 100644 --- a/src/Application.php +++ b/src/Application.php @@ -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, @@ -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); } @@ -137,4 +135,4 @@ protected function findCommandByName(string $name):Command { throw new InvalidCommandException($name); } -} \ No newline at end of file +} diff --git a/src/Argument/Argument.php b/src/Argument/Argument.php index 6ec88b7..62feda4 100644 --- a/src/Argument/Argument.php +++ b/src/Argument/Argument.php @@ -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); @@ -29,4 +29,4 @@ public function getKey():string { public function getValue():?string { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Argument/ArgumentList.php b/src/Argument/ArgumentList.php index 0001834..8722475 100644 --- a/src/Argument/ArgumentList.php +++ b/src/Argument/ArgumentList.php @@ -4,13 +4,14 @@ use Gt\Cli\Parameter\Parameter; use Iterator; +/** @implements Iterator */ 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; @@ -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; @@ -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) + ); } } } @@ -177,4 +188,4 @@ public function getValueForParameter(Parameter $parameter):?string { return null; } -} \ No newline at end of file +} diff --git a/src/Argument/ArgumentValue.php b/src/Argument/ArgumentValue.php index 1b4973a..8641722 100644 --- a/src/Argument/ArgumentValue.php +++ b/src/Argument/ArgumentValue.php @@ -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; @@ -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 { @@ -30,7 +31,8 @@ public function get():?string { return $value; } + /** @return string[] */ public function getAll():array { return $this->valueList; } -} \ No newline at end of file +} diff --git a/src/Argument/ArgumentValueList.php b/src/Argument/ArgumentValueList.php index 2636572..520864b 100644 --- a/src/Argument/ArgumentValueList.php +++ b/src/Argument/ArgumentValueList.php @@ -3,13 +3,15 @@ use Iterator; +/** @implements Iterator */ class ArgumentValueList implements Iterator { const DEFAULT_ARGUMENT_VALUE = "//\\DEFAULT ARGUMENT VALUE\\//"; /** @var ArgumentValue[] */ - protected $valueList = []; - protected $argumentValueMap = []; - protected $iteratorIndex; + protected array $valueList = []; + /** @var array */ + protected array $argumentValueMap = []; + protected int $iteratorIndex; public function set(string $key, string $value = null):void { if($this->contains($key)) { @@ -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. @@ -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; } @@ -65,7 +67,7 @@ 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(); } @@ -73,11 +75,11 @@ public function current() { } /** @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; } -} \ No newline at end of file +} diff --git a/src/Argument/NamedArgument.php b/src/Argument/NamedArgument.php index 6e9d9ce..c97ba54 100644 --- a/src/Argument/NamedArgument.php +++ b/src/Argument/NamedArgument.php @@ -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 ""; } -} \ No newline at end of file +} diff --git a/src/Command/Command.php b/src/Command/Command.php index 9aef5e4..10c3a59 100644 --- a/src/Command/Command.php +++ b/src/Command/Command.php @@ -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; } @@ -231,7 +230,7 @@ public function getArgumentValueList( continue; } elseif($argument instanceof NamedArgument) { - /** @var NamedParameter $parameter */ + /** @var NamedParameter|null $parameter */ $parameter = $namedParameterList[ $namedParameterIndex ] ?? null; @@ -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; } @@ -298,4 +297,4 @@ protected function writeLine( ):void { $this->write($message . PHP_EOL, $streamName); } -} \ No newline at end of file +} diff --git a/src/Command/HelpCommand.php b/src/Command/HelpCommand.php index 366cadd..940da96 100644 --- a/src/Command/HelpCommand.php +++ b/src/Command/HelpCommand.php @@ -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 @@ -140,4 +140,4 @@ protected function getHelpForCommand(string $commandName = null):string { return $output; } -} \ No newline at end of file +} diff --git a/src/Command/VersionCommand.php b/src/Command/VersionCommand.php index b649ba5..d8c4785 100644 --- a/src/Command/VersionCommand.php +++ b/src/Command/VersionCommand.php @@ -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 { @@ -88,4 +89,4 @@ protected function getVersion(string $command = null):string { return $version; } -} \ No newline at end of file +} diff --git a/src/ErrorCode.php b/src/ErrorCode.php index 292fc26..339818f 100644 --- a/src/ErrorCode.php +++ b/src/ErrorCode.php @@ -11,7 +11,8 @@ class ErrorCode { const DEFAULT_CODE = 1000; - protected static $classList = [ + /** @var string[] */ + protected static array $classList = [ NotEnoughArgumentsException::class, CommandException::class, InvalidCommandException::class, @@ -19,6 +20,7 @@ class ErrorCode { MissingRequiredParameterException::class, ]; + /** @param string|Exception $exception */ public static function get($exception):int { if($exception instanceof Exception) { $exception = get_class($exception); @@ -29,4 +31,4 @@ public static function get($exception):int { self::$classList ) ?: self::DEFAULT_CODE; } -} \ No newline at end of file +} diff --git a/src/Parameter/Parameter.php b/src/Parameter/Parameter.php index ada36e8..9b23465 100644 --- a/src/Parameter/Parameter.php +++ b/src/Parameter/Parameter.php @@ -2,11 +2,11 @@ namespace Gt\Cli\Parameter; class Parameter { - protected $takesValue; - protected $longOption; - protected $shortOption; - protected $documentation; - protected $exampleValue; + protected bool $takesValue; + protected string $longOption; + protected ?string $shortOption; + protected ?string $documentation; + protected ?string $exampleValue; public function __construct( bool $takesValue, @@ -54,4 +54,4 @@ public function getExampleValue():string { return $this->exampleValue ?? strtoupper($this->longOption); } -} \ No newline at end of file +} diff --git a/src/Stream.php b/src/Stream.php index f0a7754..f3dedda 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -35,7 +35,7 @@ public function __construct( $this->setStream($in, $out, $error); } - public function setStream(string $in, string $out, string $error) { + public function setStream(string $in, string $out, string $error):void { $this->in = new SplFileObject( $in, "r" @@ -93,4 +93,4 @@ protected function getNamedStream(string $streamName):SplFileObject { throw new InvalidStreamNameException($streamName); } -} \ No newline at end of file +}