Change functions to properties
- Force PHP 8.4
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user