Skip to content

Commit

Permalink
chore(console): throw on missing value
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Jan 5, 2022
1 parent 9ccfd0e commit 3c9455a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/.phpcs.xml
Expand Up @@ -8,7 +8,7 @@
<!-- // START: Enums are not supported -->
<exclude-pattern>*/src/Console/Output/(Type|Verbosity).php</exclude-pattern>
<exclude-pattern>*/src/Console/Output/Sequence/Erase.php</exclude-pattern>
<exclude-pattern>*/src/Console/Output/Sequence/Erase.php</exclude-pattern>
<exclude-pattern>*/src/Console/Input/Definition/Mode.php</exclude-pattern>
<exclude-pattern>*/src/Console/Internal/Utility.php</exclude-pattern>
<exclude-pattern>*/src/Console/Command/ExitCode.php</exclude-pattern>
<exclude-pattern>*/src/Console/Formatter/Style/(Effect|BackgroundColor|ForegroundColor).php</exclude-pattern>
Expand Down
15 changes: 8 additions & 7 deletions src/Console/Input/Definition/Definition.php
Expand Up @@ -5,6 +5,7 @@
namespace Neu\Console\Input\Definition;

use Psl\Str;
use Neu\Console\Exception;

/**
* A `Definition` is an object that designates the parameters accepted by
Expand Down Expand Up @@ -71,7 +72,7 @@ public function getAlias(): string
*/
public function alias(string $alias): self
{
if (Str\length($alias) > Str\length($this->name)) {
if (Str\Byte\length($alias) > Str\Byte\length($this->name)) {
$this->alias = $this->name;
$this->name = $alias;
} else {
Expand All @@ -94,7 +95,7 @@ public function getDescription(): string
*/
public function getFormattedName(string $name): string
{
if (Str\length($name) === 1) {
if (Str\Byte\length($name) === 1) {
return '-' . $name;
}

Expand All @@ -118,19 +119,19 @@ public function getName(): string
}

/**
* {@inheritDoc}
*
* @param T|null $default - The default value to return if no value has been assigned.
* Retrieve the value of the `Definition` as specified by the user.
*
* @return T
*
* @throws Exception\MissingValueException If the definition has not been assigned a value.
*/
public function getValue(mixed $default = null): mixed
public function getValue(): mixed
{
if ($this->exists) {
return $this->value;
}

return $default;
throw new Exception\MissingValueException(Str\format('The "%s" definition has not been assigned a value.', $this::class));
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Console/Input/Definition/DefinitionInterface.php
Expand Up @@ -4,6 +4,8 @@

namespace Neu\Console\Input\Definition;

use Neu\Console\Exception;

/**
* An `DefinitionInterface` defines the name and type of input that may be accepted
* by the user.
Expand Down Expand Up @@ -46,9 +48,9 @@ public function getName(): string;
/**
* Retrieve the value of the `DefinitionInterface` as specified by the user.
*
* @param T|null $default - The default value to return if no value has been assigned.
*
* @return T
*
* @throws Exception\MissingValueException If the definition has not been assigned a value.
*/
public function getValue(mixed $default = null): mixed;
public function getValue(): mixed;
}
9 changes: 5 additions & 4 deletions src/Console/Input/Definition/Mode.php
Expand Up @@ -4,7 +4,8 @@

namespace Neu\Console\Input\Definition;

enum Mode: int {
case Optional = 0;
case Required = 1;
}
enum Mode: int
{
case Optional = 0;
case Required = 1;
}

0 comments on commit 3c9455a

Please sign in to comment.