Change functions to properties

- Force PHP 8.4
This commit is contained in:
2024-09-30 22:59:46 -04:00
parent 6779b2c554
commit 483d7875d5
6 changed files with 126 additions and 159 deletions

View File

@@ -25,50 +25,38 @@ use InvalidArgumentException;
*
* @template T The type of value represented by this option
*/
readonly class Option
class Option
{
/** @var T|null $value The value for this option */
private mixed $value;
private mixed $val;
/**
* @param T|null $value The possibly null value for this option
*/
private function __construct(mixed $value = null)
{
$this->value = $value;
$this->val = $value;
}
/**
* Get the value of this option
*
* @return T The value of the option
* @var T The value of this option (read-only)
* @throws InvalidArgumentException If called on a `None` option
*/
public function get(): mixed
{
return match (true) {
$this->isSome() => $this->value,
default => throw new InvalidArgumentException('Cannot get the value of a None option'),
public mixed $value {
get => match ($this->val) {
null => throw new InvalidArgumentException('Cannot get the value of a None option'),
default => $this->val,
};
}
/**
* Does this option have a `None` value?
*
* @return bool True if the option is `None`, false if it is `Some`
*/
public function isNone(): bool
{
return is_null($this->value);
/** @var bool True if the option is `None`, false if it is `Some` */
public bool $isNone {
get => is_null($this->val);
}
/**
* Does this option have a `Some` value?
*
* @return bool True if the option is `Some`, false if it is `None`
*/
public function isSome(): bool
{
return !$this->isNone();
/** @var bool True if the option is `Some`, false if it is `None` */
public bool $isSome{
get => !$this->isNone;
}
/**
@@ -79,7 +67,7 @@ readonly class Option
*/
public function getOrDefault(mixed $default): mixed
{
return $this->value ?? $default;
return $this->val ?? $default;
}
/**
@@ -91,18 +79,19 @@ readonly class Option
*/
public function getOrCall(callable $f): mixed
{
return $this->value ?? $f();
return $this->val ?? $f();
}
/**
* Get the value, or throw the
* Get the value, or throw the exception using the given function
*
* @param callable(): Exception $exFunc A function to construct the exception to throw
* @return T The value of the option if `Some`
* @throws Exception If the option is `None`
*/
public function getOrThrow(callable $exFunc): mixed
{
return $this->value ?? throw $exFunc();
return $this->val ?? throw $exFunc();
}
/**
@@ -117,7 +106,7 @@ readonly class Option
*/
public function bind(callable $f): Option
{
return $this->isNone() ? $this : $f($this->get());
return $this->isNone ? $this : $f($this->val);
}
/**
@@ -130,8 +119,8 @@ readonly class Option
public function contains(mixed $value, bool $strict = true): bool
{
return match (true) {
$this->isNone() => false,
default => $strict ? $this->value === $value : $this->value == $value,
$this->isNone => false,
default => $strict ? $this->val === $value : $this->val == $value,
};
}
@@ -143,7 +132,7 @@ readonly class Option
*/
public function exists(callable $f): bool
{
return $this->isSome() ? $f($this->value) : false;
return $this->isSome ? $f($this->val) : false;
}
/**
@@ -155,7 +144,7 @@ readonly class Option
*/
public function map(callable $f): self
{
return $this->isSome() ? self::Some($f($this->get())) : $this;
return $this->isSome ? self::Some($f($this->val)) : $this;
}
/**
@@ -165,8 +154,8 @@ readonly class Option
*/
public function iter(callable $f): void
{
if ($this->isSome()) {
$f($this->value);
if ($this->isSome) {
$f($this->val);
}
}
@@ -178,7 +167,7 @@ readonly class Option
*/
public function filter(callable $f): self
{
return $this->isNone() || $this->exists($f) ? $this : self::None();
return $this->isNone || $this->exists($f) ? $this : self::None();
}
/**
@@ -188,7 +177,7 @@ readonly class Option
*/
public function unwrap(): mixed
{
return $this->value;
return $this->val;
}
/**
@@ -210,7 +199,7 @@ readonly class Option
*/
public function toArray(): array
{
return $this->isSome() ? [$this->value] : [];
return $this->isSome ? [$this->val] : [];
}
/**
@@ -221,8 +210,8 @@ readonly class Option
public function toPhpOption(): mixed
{
return match (true) {
$this->isNone() && class_exists('PhpOption\None') => call_user_func('PhpOption\None::create'),
class_exists('PhpOption\Some') => call_user_func('PhpOption\Some::create', $this->value),
$this->isNone && class_exists('PhpOption\None') => call_user_func('PhpOption\None::create'),
class_exists('PhpOption\Some') => call_user_func('PhpOption\Some::create', $this->val),
default => throw new Error('PhpOption types could not be found'),
};
}
@@ -261,7 +250,7 @@ readonly class Option
{
return match (true) {
is_object($value) && is_a($value, 'PhpOption\Option') =>
$value->isDefined() ? self::Some($value->get()) : self::None(),
$value->isDefined() ? self::Some($value->get()) : self::None(),
default => new self($value),
};
}

View File

@@ -25,7 +25,7 @@ use InvalidArgumentException;
* @template TOK The type of the OK result
* @template TError The type of the error result
*/
readonly class Result
class Result
{
/** @var Option<TOK> The OK value for this result */
private Option $okValue;
@@ -45,46 +45,24 @@ readonly class Result
$this->errorValue = Option::of($errorValue);
}
/**
* Get the value for an `OK` result
*
* @return TOK The OK value for this result
* @throws InvalidArgumentException If the result is an `Error` result
*/
public function getOK(): mixed
{
return $this->okValue->get();
/** @var TOK The OK value (will throw if result is not OK) */
public mixed $ok {
get => $this->okValue->value;
}
/**
* Get the value for an `Error` result
*
* @return TError The error value for this result
* @throws InvalidArgumentException If the result is an `OK` result
*/
public function getError(): mixed
{
return $this->errorValue->get();
/** @var TError The error value (will throw if result is not Error) */
public mixed $error {
get => $this->errorValue->value;
}
/**
* Is this result `OK`?
*
* @return bool True if the result is `OK`, false if it is `Error`
*/
public function isOK(): bool
{
return $this->okValue->isSome();
/** @var bool True if the result is `OK`, false if it is `Error` */
public bool $isOK {
get => $this->okValue->isSome;
}
/**
* Is this result `Error`?
*
* @return bool True if the result is `Error`, false if it is `OK`
*/
public function isError(): bool
{
return $this->errorValue->isSome();
/** @var bool True if the result is `Error`, false if it is `OK` */
public bool $isError {
get => $this->errorValue->isSome;
}
/**
@@ -100,7 +78,7 @@ readonly class Result
*/
public function bind(callable $f): Result
{
return $this->isError() ? $this : $f($this->getOK());
return $this->isError ? $this : $f($this->ok);
}
/**
@@ -113,8 +91,8 @@ readonly class Result
public function contains(mixed $value, bool $strict = true): bool
{
return match (true) {
$this->isError() => false,
default => $this->okValue->contains($value, $strict),
$this->isError => false,
default => $this->okValue->contains($value, $strict),
};
}
@@ -126,7 +104,7 @@ readonly class Result
*/
public function exists(callable $f): bool
{
return $this->isOK() ? $f($this->okValue->get()) : false;
return $this->isOK ? $f($this->ok) : false;
}
/**
@@ -138,7 +116,7 @@ readonly class Result
*/
public function map(callable $f): self
{
return $this->isOK() ? self::OK($f($this->getOK())) : $this;
return $this->isOK ? self::OK($f($this->ok)) : $this;
}
/**
@@ -150,7 +128,7 @@ readonly class Result
*/
public function mapError(callable $f): self
{
return $this->isError() ? self::Error($f($this->getError())) : $this;
return $this->isError ? self::Error($f($this->error)) : $this;
}
/**
@@ -160,8 +138,8 @@ readonly class Result
*/
public function iter(callable $f): void
{
if ($this->isOK()) {
$f($this->getOK());
if ($this->isOK) {
$f($this->ok);
}
}
@@ -182,7 +160,7 @@ readonly class Result
*/
public function toOption(): Option
{
return $this->isOK() ? Option::Some($this->getOK()) : Option::None();
return $this->isOK ? Option::Some($this->ok) : Option::None();
}
/**