diff --git a/phpmd.xml b/phpmd.xml index 5424e30..e4bc82d 100644 --- a/phpmd.xml +++ b/phpmd.xml @@ -33,7 +33,7 @@ - + diff --git a/src/Application.php b/src/Application.php index bff0153..8b83e5a 100644 --- a/src/Application.php +++ b/src/Application.php @@ -151,11 +151,13 @@ protected function findCommandByName(string $name):Command { throw new InvalidCommandException($name); } + /** @SuppressWarnings(PHPMD.ExitExpression) */ private function exit():void { if(isset($this->exitHandler)) { call_user_func($this->exitHandler, $this->exitCode); } elseif($this->exitCode !== 0) { + // phpcs:ignore exit($this->exitCode); } } diff --git a/src/Argument/ArgumentList.php b/src/Argument/ArgumentList.php index e02dd30..b9e47a1 100644 --- a/src/Argument/ArgumentList.php +++ b/src/Argument/ArgumentList.php @@ -26,7 +26,10 @@ public function getCommandName():string { return $this->argumentList[0]->getValue() ?? ""; } - /** @param string[] $arguments */ + /** + * @param string[] $arguments + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ protected function buildArgumentList(array $arguments):void { if(isset($arguments[0]) && $arguments[0][0] !== "-") { diff --git a/src/Argument/ArgumentValueListNotSetException.php b/src/Argument/ArgumentValueListNotSetException.php index b5f078f..61f09af 100644 --- a/src/Argument/ArgumentValueListNotSetException.php +++ b/src/Argument/ArgumentValueListNotSetException.php @@ -3,4 +3,4 @@ use Gt\Cli\CliException; -class ArgumentValueListNotSetException extends CliException {} \ No newline at end of file +class ArgumentValueListNotSetException extends CliException {} diff --git a/src/Argument/CommandArgument.php b/src/Argument/CommandArgument.php index 2edb7c2..3e88941 100644 --- a/src/Argument/CommandArgument.php +++ b/src/Argument/CommandArgument.php @@ -6,7 +6,8 @@ public function __construct(string $commandName) { parent::__construct("", $commandName); } + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter protected function processRawKey(string $rawKey): string { return ""; } -} \ No newline at end of file +} diff --git a/src/Argument/CommandArgumentList.php b/src/Argument/CommandArgumentList.php index 54f9b90..3eb9c03 100644 --- a/src/Argument/CommandArgumentList.php +++ b/src/Argument/CommandArgumentList.php @@ -14,4 +14,4 @@ public function __construct( ); parent::__construct($script, ...$commandArguments); } -} \ No newline at end of file +} diff --git a/src/Argument/DefaultArgumentValue.php b/src/Argument/DefaultArgumentValue.php index 114cd7e..5bc5a94 100644 --- a/src/Argument/DefaultArgumentValue.php +++ b/src/Argument/DefaultArgumentValue.php @@ -6,4 +6,4 @@ public function __construct(?string $default) { parent::__construct("__DEFAULT__"); $this->push($default); } -} \ No newline at end of file +} diff --git a/src/Argument/LongOptionArgument.php b/src/Argument/LongOptionArgument.php index 7c34ef2..68dd29e 100644 --- a/src/Argument/LongOptionArgument.php +++ b/src/Argument/LongOptionArgument.php @@ -5,4 +5,4 @@ class LongOptionArgument extends Argument { protected function processRawKey(string $rawKey):string { return substr($rawKey, 2); } -} \ No newline at end of file +} diff --git a/src/Argument/NamedArgument.php b/src/Argument/NamedArgument.php index c97ba54..865c206 100644 --- a/src/Argument/NamedArgument.php +++ b/src/Argument/NamedArgument.php @@ -6,6 +6,7 @@ public function __construct(string $value) { parent::__construct("", $value); } + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter protected function processRawKey(string $rawKey): string { return ""; } diff --git a/src/Argument/NamedArgumentValue.php b/src/Argument/NamedArgumentValue.php index cfc78d2..a3eaba4 100644 --- a/src/Argument/NamedArgumentValue.php +++ b/src/Argument/NamedArgumentValue.php @@ -1,4 +1,4 @@ getName(); $documentation = []; - foreach($this->getRequiredNamedParameterList() as $parameter) { - $message .= " "; - $message .= $parameter->getOptionName(); - - $paramDocumentation = $parameter->getDocumentation(); - if(!empty($paramDocumentation)) { - $documentation[$parameter->getOptionName()] = - $paramDocumentation; - } - } - - foreach($this->getOptionalNamedParameterList() as $parameter) { - $message .= " ["; - $message .= $parameter->getOptionName(); - $message .= "]"; - - $paramDocumentation = $parameter->getDocumentation(); - if(!empty($paramDocumentation)) { - $documentation[$parameter->getOptionName()] = - "(Optional) " . $paramDocumentation; - } - } - - foreach($this->getRequiredParameterList() as $parameter) { - $message .= " --"; - $message .= $parameter->getLongOption(); - - if($short = $parameter->getShortOption()) { - $message .= "|-$short"; - } - - if($parameter->takesValue()) { - $message .= " "; - $message .= $parameter->getExampleValue(); - } - - $paramDocumentation = $parameter->getDocumentation(); - if(!empty($paramDocumentation)) { - $paramDocumentationKey = "--" . $parameter->getLongOption(); - if($short) { - $paramDocumentationKey .= "|-" . $short; - } - $documentation[$paramDocumentationKey] = - $paramDocumentation; - } - } - - foreach($this->getOptionalParameterList() as $parameter) { - $message .= " [--"; - $message .= $parameter->getLongOption(); - - if($short = $parameter->getShortOption()) { - $message .= "|-$short"; - } - - if($parameter->takesValue()) { - $message .= " "; - $message .= $parameter->getExampleValue(); - } - - $message .= "]"; - - $paramDocumentation = $parameter->getDocumentation(); - if(!empty($paramDocumentation)) { - $paramDocumentationKey = "--" . $parameter->getLongOption(); - if($short) { - $paramDocumentationKey .= "|-" . $short; - } - $documentation[$paramDocumentationKey] = - "(Optional) " . $paramDocumentation; - } - } - - if($includeDocumentation - && !empty($documentation)) { - $message .= PHP_EOL; - $message .= PHP_EOL; - - foreach($documentation as $key => $docString) { - $wrappedDocs = wordwrap($docString, 55); - $wrappedDocs = explode("\n", $wrappedDocs); - foreach($wrappedDocs as $i => $line) { - if($i === 0) { - continue; - } - - $wrappedDocs[$i] = "\t\t\t" . $line; - } - $wrappedDocs = implode("\n", $wrappedDocs); - - if(!strstr($key, "-")) { - $message .= str_repeat(" ", 6); - $message .= $key; - $message .= "\t\t"; - $message .= $wrappedDocs; - $message .= PHP_EOL; - } - else { - $keyParts = explode("|", $key); - $message .= str_repeat(" ", 2); - if(isset($keyParts[1])) { - $message .= $keyParts[1]; - $message .= ", "; - } - - $message .= $keyParts[0]; - if(!isset($keyParts[1])) { - $message .= str_repeat(" ", 3); - } - - $message .= "\t\t"; - $message .= $wrappedDocs; - - $message .= PHP_EOL; - } - } + $this->appendUsageRequiredNamedParameterList($message, $documentation); + $this->appendUsageOptionalNamedParameterList($message, $documentation); + $this->appendUsageRequiredParameterList($message, $documentation); + $this->appendUsageOptionalParameterList($message, $documentation); + if($includeDocumentation && !empty($documentation)) { + $this->appendDocumentation($message, $documentation); } return $message; @@ -176,7 +65,7 @@ public function checkArguments(ArgumentList $argumentList):void { $passedNamedArguments = 0; foreach($argumentList as $argument) { if($argument instanceof NamedArgument) { - $passedNamedArguments ++; + $passedNamedArguments++; } } @@ -207,6 +96,7 @@ public function checkArguments(ArgumentList $argumentList):void { } } + /** @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getArgumentValueList( ArgumentList $arguments ):ArgumentValueList { @@ -260,7 +150,7 @@ public function getArgumentValueList( || $argumentKey === $parameterToCheck->getShortOption()) { $parameter = $parameterToCheck; break; - }; + } } if(is_null($parameter)) { @@ -312,4 +202,149 @@ protected function readLine(string $default = null):string { } return trim($line) ?: $default ?? ""; } + + /** @param array $documentation */ + private function appendUsageRequiredNamedParameterList( + string &$message, + array &$documentation, + ):void { + foreach($this->getRequiredNamedParameterList() as $parameter) { + $message .= " "; + $message .= $parameter->getOptionName(); + + $paramDocumentation = $parameter->getDocumentation(); + if(!empty($paramDocumentation)) { + $documentation[$parameter->getOptionName()] = + $paramDocumentation; + } + } + } + + /** @param array $documentation */ + private function appendUsageOptionalNamedParameterList( + string &$message, + array &$documentation, + ):void { + foreach($this->getOptionalNamedParameterList() as $parameter) { + $message .= " ["; + $message .= $parameter->getOptionName(); + $message .= "]"; + + $paramDocumentation = $parameter->getDocumentation(); + if(!empty($paramDocumentation)) { + $documentation[$parameter->getOptionName()] = + "(Optional) " . $paramDocumentation; + } + } + } + + /** @param array $documentation */ + private function appendUsageRequiredParameterList( + string &$message, + array &$documentation, + ):void { + foreach($this->getRequiredParameterList() as $parameter) { + $message .= " --"; + $message .= $parameter->getLongOption(); + + if($short = $parameter->getShortOption()) { + $message .= "|-$short"; + } + + if($parameter->takesValue()) { + $message .= " "; + $message .= $parameter->getExampleValue(); + } + + $paramDocumentation = $parameter->getDocumentation(); + if(!empty($paramDocumentation)) { + $paramDocumentationKey = "--" . $parameter->getLongOption(); + if($short) { + $paramDocumentationKey .= "|-" . $short; + } + $documentation[$paramDocumentationKey] = + $paramDocumentation; + } + } + } + + /** @param array $documentation */ + private function appendUsageOptionalParameterList( + string &$message, + array &$documentation, + ):void { + foreach($this->getOptionalParameterList() as $parameter) { + $message .= " [--"; + $message .= $parameter->getLongOption(); + + if($short = $parameter->getShortOption()) { + $message .= "|-$short"; + } + + if($parameter->takesValue()) { + $message .= " "; + $message .= $parameter->getExampleValue(); + } + + $message .= "]"; + + $paramDocumentation = $parameter->getDocumentation(); + if(!empty($paramDocumentation)) { + $paramDocumentationKey = "--" . $parameter->getLongOption(); + if($short) { + $paramDocumentationKey .= "|-" . $short; + } + $documentation[$paramDocumentationKey] = + "(Optional) " . $paramDocumentation; + } + } + } + + /** @param array $documentation */ + private function appendDocumentation( + string &$message, + array &$documentation, + ):void { + $message .= PHP_EOL; + $message .= PHP_EOL; + + foreach($documentation as $key => $docString) { + $wrappedDocs = wordwrap($docString, 55); + $wrappedDocs = explode("\n", $wrappedDocs); + foreach($wrappedDocs as $i => $line) { + if($i === 0) { + continue; + } + + $wrappedDocs[$i] = "\t\t\t" . $line; + } + $wrappedDocs = implode("\n", $wrappedDocs); + + if(!strstr($key, "-")) { + $message .= str_repeat(" ", 6); + $message .= $key; + $message .= "\t\t"; + $message .= $wrappedDocs; + $message .= PHP_EOL; + } + else { + $keyParts = explode("|", $key); + $message .= str_repeat(" ", 2); + if(isset($keyParts[1])) { + $message .= $keyParts[1]; + $message .= ", "; + } + + $message .= $keyParts[0]; + if(!isset($keyParts[1])) { + $message .= str_repeat(" ", 3); + } + + $message .= "\t\t"; + $message .= $wrappedDocs; + + $message .= PHP_EOL; + } + } + } } diff --git a/src/Command/CommandException.php b/src/Command/CommandException.php index 493ae75..106454c 100644 --- a/src/Command/CommandException.php +++ b/src/Command/CommandException.php @@ -3,4 +3,4 @@ use Gt\Cli\CliException; -class CommandException extends CliException {} \ No newline at end of file +class CommandException extends CliException {} diff --git a/src/Command/HelpCommand.php b/src/Command/HelpCommand.php index c7314e9..dc15c5a 100644 --- a/src/Command/HelpCommand.php +++ b/src/Command/HelpCommand.php @@ -117,7 +117,8 @@ protected function getHelpForAllCommands():string { } $output .= PHP_EOL; - $output .= "Type `{$this->scriptName} help COMMAND` to get help for that command."; + $output .= "Type `{$this->scriptName} help COMMAND` " + . "to get help for that command."; return $output; } diff --git a/src/Command/InvalidCommandException.php b/src/Command/InvalidCommandException.php index ad1cfa1..551e65f 100644 --- a/src/Command/InvalidCommandException.php +++ b/src/Command/InvalidCommandException.php @@ -7,4 +7,4 @@ class InvalidCommandException extends CliException { public function __construct(string $message) { parent::__construct("Invalid command: \"$message\""); } -} \ No newline at end of file +} diff --git a/src/Command/VersionCommand.php b/src/Command/VersionCommand.php index e7419ae..0d77f87 100644 --- a/src/Command/VersionCommand.php +++ b/src/Command/VersionCommand.php @@ -7,12 +7,9 @@ use Gt\Cli\Parameter\Parameter; class VersionCommand extends Command { + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter public function run(ArgumentValueList $arguments = null):void { - $this->writeLine( - $this->getVersion($arguments?->get( - "command" - ) - )); + $this->writeLine($this->getVersion()); } public function getName():string { @@ -43,7 +40,8 @@ public function getOptionalParameterList():array { return []; } - protected function getVersion(string $command = null):string { + /** @SuppressWarnings(PHPMD.StaticAccess) */ + protected function getVersion():string { $package = InstalledVersions::getRootPackage()["name"]; return InstalledVersions::getVersion($package) ?? ""; } diff --git a/src/InvalidStreamNameException.php b/src/InvalidStreamNameException.php index 954bab3..b2b18da 100644 --- a/src/InvalidStreamNameException.php +++ b/src/InvalidStreamNameException.php @@ -3,4 +3,4 @@ use RuntimeException; -class InvalidStreamNameException extends RuntimeException {} \ No newline at end of file +class InvalidStreamNameException extends RuntimeException {} diff --git a/src/Parameter/MissingRequiredParameterException.php b/src/Parameter/MissingRequiredParameterException.php index 9f33c92..77e4059 100644 --- a/src/Parameter/MissingRequiredParameterException.php +++ b/src/Parameter/MissingRequiredParameterException.php @@ -3,4 +3,4 @@ use Gt\Cli\CliException; -class MissingRequiredParameterException extends CliException {} \ No newline at end of file +class MissingRequiredParameterException extends CliException {} diff --git a/src/Parameter/MissingRequiredParameterValueException.php b/src/Parameter/MissingRequiredParameterValueException.php index a5b27c9..b212f1d 100644 --- a/src/Parameter/MissingRequiredParameterValueException.php +++ b/src/Parameter/MissingRequiredParameterValueException.php @@ -3,4 +3,4 @@ use Gt\Cli\CliException; -class MissingRequiredParameterValueException extends CliException {} \ No newline at end of file +class MissingRequiredParameterValueException extends CliException {} diff --git a/src/Parameter/NamedParameter.php b/src/Parameter/NamedParameter.php index d2b5e2f..1524e53 100644 --- a/src/Parameter/NamedParameter.php +++ b/src/Parameter/NamedParameter.php @@ -10,4 +10,4 @@ public function __construct(string $optionName) { public function getOptionName():string { return $this->longOption; } -} \ No newline at end of file +} diff --git a/src/Parameter/UserParameter.php b/src/Parameter/UserParameter.php index 7f8c97e..3d56096 100644 --- a/src/Parameter/UserParameter.php +++ b/src/Parameter/UserParameter.php @@ -1,14 +1,4 @@ contains($param4)); } - public function data_randomNamedArgs():array { + public static function data_randomNamedArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -352,7 +352,7 @@ public function data_randomNamedArgs():array { return $dataSet; } - public function data_randomLongArgs():array { + public static function data_randomLongArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -381,7 +381,7 @@ public function data_randomLongArgs():array { return $dataSet; } - public function data_randomShortArgs():array { + public static function data_randomShortArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -410,7 +410,7 @@ public function data_randomShortArgs():array { return $dataSet; } - public function data_randomLongEqualsArgs():array { + public static function data_randomLongEqualsArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -430,7 +430,7 @@ public function data_randomLongEqualsArgs():array { return $dataSet; } - public function data_randomShortEqualsArgs():array { + public static function data_randomShortEqualsArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -455,4 +455,4 @@ public function data_randomShortEqualsArgs():array { return $dataSet; } -} \ No newline at end of file +}