11
0
Fork 0
mirror of https://github.com/n3w/helpers-cli-input.git synced 2025-12-19 12:43:23 +00:00

Code tidy

This commit is contained in:
Alannah Kearney 2019-05-27 00:22:25 +10:00
commit 419a4a9f79
14 changed files with 53 additions and 55 deletions

View file

@ -60,7 +60,7 @@ $collection = (new Input\InputCollection())
try { try {
$argv = Input\InputHandlerFactory::build('Argv', $collection); $argv = Input\InputHandlerFactory::build('Argv', $collection);
} catch (\Exception $ex) { } catch (\Exception $ex) {
echo "Error when attempting to bind values to collection. Returned: " . $ex->getMessage() . PHP_EOL; echo 'Error when attempting to bind values to collection. Returned: '.$ex->getMessage().PHP_EOL;
exit; exit;
} }
@ -73,7 +73,7 @@ echo Cli\manpage(
Colour::FG_GREEN, Colour::FG_GREEN,
Colour::FG_WHITE, Colour::FG_WHITE,
[ [
'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import' 'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import',
] ]
).PHP_EOL.PHP_EOL; ).PHP_EOL.PHP_EOL;
@ -92,7 +92,6 @@ echo Cli\manpage(
// Examples: // Examples:
// php -f example/example.php -- -vvvs -d example/example.json import // php -f example/example.php -- -vvvs -d example/example.json import
var_dump($argv->find('action')); var_dump($argv->find('action'));
// string(6) "import" // string(6) "import"

View file

@ -43,7 +43,7 @@ abstract class AbstractInputType implements Interfaces\InputTypeInterface
public function respondsTo(string $name): bool public function respondsTo(string $name): bool
{ {
return ($name == $this->name); return $name == $this->name;
} }
public function getType(): string public function getType(): string

View file

@ -3,12 +3,13 @@
declare(strict_types=1); declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input\Exceptions; namespace pointybeard\Helpers\Cli\Input\Exceptions;
use pointybeard\Helpers\Cli\Input\AbstractInputType; use pointybeard\Helpers\Cli\Input\AbstractInputType;
class InputValidationFailedException extends \Exception class InputValidationFailedException extends \Exception
{ {
public function __construct(AbstractInputType $input, $code = 0, \Exception $previous = null) public function __construct(AbstractInputType $input, $code = 0, \Exception $previous = null)
{ {
return parent::__construct(sprintf("Validation failed for %s. Returned: %s", $input->getDisplayName(), $previous->getMessage()), $code, $previous); return parent::__construct(sprintf('Validation failed for %s. Returned: %s', $input->getDisplayName(), $previous->getMessage()), $code, $previous);
} }
} }

View file

@ -18,7 +18,6 @@ class RequiredInputMissingValueException extends \Exception
'a value is required for %s', 'a value is required for %s',
$input->getDisplayName() $input->getDisplayName()
), $code, $previous); ), $code, $previous);
} }
public function getInput(): Input\AbstractInputType public function getInput(): Input\AbstractInputType

View file

@ -148,7 +148,7 @@ class Argv extends Input\AbstractInputHandler
? $a->name() ? $a->name()
: $argumentCount : $argumentCount
] = $token; ] = $token;
$argumentCount++; ++$argumentCount;
break; break;
} }
$it->next(); $it->next();

View file

@ -33,11 +33,9 @@ class InputCollection
public function find(string $name, array $restrictToType = null, array $excludeType = null, &$type = null, &$index = null): ?AbstractInputType public function find(string $name, array $restrictToType = null, array $excludeType = null, &$type = null, &$index = null): ?AbstractInputType
{ {
foreach ($this->items as $type => $items) { foreach ($this->items as $type => $items) {
// Check if we're restricting to or excluding specific types // Check if we're restricting to or excluding specific types
if (null !== $restrictToType && !in_array($type, $restrictToType)) { if (null !== $restrictToType && !in_array($type, $restrictToType)) {
continue; continue;
} elseif (null !== $excludeType && in_array($type, $excludeType)) { } elseif (null !== $excludeType && in_array($type, $excludeType)) {
continue; continue;
} }
@ -50,22 +48,27 @@ class InputCollection
} }
$type = null; $type = null;
$index = null; $index = null;
return null; return null;
} }
public function getTypes(): array { public function getTypes(): array
{
return array_keys($this->items); return array_keys($this->items);
} }
public function getItems(): array { public function getItems(): array
{
return $this->items; return $this->items;
} }
public function getItemsByType(string $type): array { public function getItemsByType(string $type): array
{
return $this->items[$type] ?? []; return $this->items[$type] ?? [];
} }
public function getItemByIndex(string $type, int $index): ?AbstractInputType { public function getItemByIndex(string $type, int $index): ?AbstractInputType
{
return $this->items[$type][$index] ?? null; return $this->items[$type][$index] ?? null;
} }

View file

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input; namespace pointybeard\Helpers\Cli\Input;
use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Foundation\Factory; use pointybeard\Helpers\Foundation\Factory;
final class InputTypeFactory extends Factory\AbstractFactory final class InputTypeFactory extends Factory\AbstractFactory

View file

@ -16,7 +16,10 @@ interface InputTypeInterface
const FLAG_TYPE_INCREMENTING = 0x0400; const FLAG_TYPE_INCREMENTING = 0x0400;
public function getType(): string; public function getType(): string;
public function respondsTo(string $name): bool; public function respondsTo(string $name): bool;
public function __toString(): string; public function __toString(): string;
public function getDisplayName(): string; public function getDisplayName(): string;
} }

View file

@ -10,7 +10,6 @@ use pointybeard\Helpers\Functions\Cli;
class Argument extends Input\AbstractInputType class Argument extends Input\AbstractInputType
{ {
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = null) public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = null)
{ {
if (null === $validator) { if (null === $validator) {
@ -25,7 +24,8 @@ class Argument extends Input\AbstractInputType
parent::__construct($name, $flags, $description, $validator, $default); parent::__construct($name, $flags, $description, $validator, $default);
} }
public function getDisplayName(): string { public function getDisplayName(): string
{
return strtoupper($this->name()); return strtoupper($this->name());
} }

View file

@ -5,16 +5,13 @@ declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input\Types; namespace pointybeard\Helpers\Cli\Input\Types;
use pointybeard\Helpers\Functions\Flags; use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Functions\Strings;
use pointybeard\Helpers\Functions\Cli;
use pointybeard\Helpers\Cli\Input;
class Flag extends Option class Flag extends Option
{ {
public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = false) public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = false)
{ {
if (Flags\is_flag_set($flags, self::FLAG_VALUE_REQUIRED) || Flags\is_flag_set($flags, self::FLAG_VALUE_OPTIONAL)) { if (Flags\is_flag_set($flags, self::FLAG_VALUE_REQUIRED) || Flags\is_flag_set($flags, self::FLAG_VALUE_OPTIONAL)) {
throw new \Exception("The flags FLAG_VALUE_REQUIRED and FLAG_VALUE_OPTIONAL cannot be used on an input of type Flag"); throw new \Exception('The flags FLAG_VALUE_REQUIRED and FLAG_VALUE_OPTIONAL cannot be used on an input of type Flag');
} }
parent::__construct($name, null, $flags, $description, $validator, $default); parent::__construct($name, null, $flags, $description, $validator, $default);
} }

View file

@ -5,9 +5,6 @@ declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input\Types; namespace pointybeard\Helpers\Cli\Input\Types;
use pointybeard\Helpers\Functions\Flags; use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Functions\Strings;
use pointybeard\Helpers\Functions\Cli;
use pointybeard\Helpers\Cli\Input;
class IncrementingFlag extends Flag class IncrementingFlag extends Flag
{ {

View file

@ -21,18 +21,18 @@ class LongOption extends Input\AbstractInputType
public function respondsTo(string $name): bool public function respondsTo(string $name): bool
{ {
return ($name == $this->name || $name == $this->short); return $name == $this->name || $name == $this->short;
} }
public function getDisplayName(): string { public function getDisplayName(): string
{
$short = $short =
null !== $this->short() null !== $this->short()
? '-'.$this->short().', ' ? '-'.$this->short().', '
: null : null
; ;
return sprintf("%s--%s", $short, $this->name()); return sprintf('%s--%s', $short, $this->name());
} }
public function __toString(): string public function __toString(): string

View file

@ -4,14 +4,14 @@ declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input\Types; namespace pointybeard\Helpers\Cli\Input\Types;
use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Functions\Strings; use pointybeard\Helpers\Functions\Strings;
use pointybeard\Helpers\Functions\Cli; use pointybeard\Helpers\Functions\Cli;
use pointybeard\Helpers\Cli\Input; use pointybeard\Helpers\Cli\Input;
class Option extends Input\AbstractInputType class Option extends Input\AbstractInputType
{ {
public function getDisplayName(): string { public function getDisplayName(): string
{
return '-'.$this->name(); return '-'.$this->name();
} }