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 {
$argv = Input\InputHandlerFactory::build('Argv', $collection);
} 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;
}
@ -73,7 +73,7 @@ echo Cli\manpage(
Colour::FG_GREEN,
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;
@ -92,7 +92,6 @@ echo Cli\manpage(
// Examples:
// php -f example/example.php -- -vvvs -d example/example.json import
var_dump($argv->find('action'));
// string(6) "import"

View file

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

View file

@ -3,12 +3,13 @@
declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input\Exceptions;
use pointybeard\Helpers\Cli\Input\AbstractInputType;
class InputValidationFailedException extends \Exception
{
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',
$input->getDisplayName()
), $code, $previous);
}
public function getInput(): Input\AbstractInputType

View file

@ -148,7 +148,7 @@ class Argv extends Input\AbstractInputHandler
? $a->name()
: $argumentCount
] = $token;
$argumentCount++;
++$argumentCount;
break;
}
$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
{
foreach ($this->items as $type => $items) {
// Check if we're restricting to or excluding specific types
if (null !== $restrictToType && !in_array($type, $restrictToType)) {
continue;
} elseif (null !== $excludeType && in_array($type, $excludeType)) {
continue;
}
@ -50,22 +48,27 @@ class InputCollection
}
$type = null;
$index = null;
return null;
}
public function getTypes(): array {
public function getTypes(): array
{
return array_keys($this->items);
}
public function getItems(): array {
public function getItems(): array
{
return $this->items;
}
public function getItemsByType(string $type): array {
public function getItemsByType(string $type): array
{
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;
}

View file

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

View file

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

View file

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

View file

@ -5,16 +5,13 @@ declare(strict_types=1);
namespace pointybeard\Helpers\Cli\Input\Types;
use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Functions\Strings;
use pointybeard\Helpers\Functions\Cli;
use pointybeard\Helpers\Cli\Input;
class Flag extends Option
{
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)) {
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);
}

View file

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

View file

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

View file

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