Do not allow cloning Result with different parameters

So far, I haven't seen any real case for that, but it's not like I have
a strong case for not allowing that, I just want to keep the code clean.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-02-23 01:52:50 +01:00
parent 5b81e96973
commit 74fd472b66
No known key found for this signature in database
GPG key ID: 221E9281655813A6
18 changed files with 53 additions and 51 deletions

View file

@ -33,8 +33,8 @@ final class Result
public readonly bool $isValid,
public readonly mixed $input,
public readonly Rule $rule,
string $template = Rule::TEMPLATE_STANDARD,
public readonly array $parameters = [],
string $template = Rule::TEMPLATE_STANDARD,
public readonly Mode $mode = Mode::DEFAULT,
?string $name = null,
?string $id = null,
@ -47,14 +47,24 @@ final class Result
$this->children = $children;
}
public static function failed(mixed $input, Rule $rule, string $template = Rule::TEMPLATE_STANDARD): self
{
return new self(false, $input, $rule, $template);
/** @param array<string, mixed> $parameters */
public static function failed(
mixed $input,
Rule $rule,
array $parameters = [],
string $template = Rule::TEMPLATE_STANDARD
): self {
return new self(false, $input, $rule, $parameters, $template);
}
public static function passed(mixed $input, Rule $rule, string $template = Rule::TEMPLATE_STANDARD): self
{
return new self(true, $input, $rule, $template);
/** @param array<string, mixed> $parameters */
public static function passed(
mixed $input,
Rule $rule,
array $parameters = [],
string $template = Rule::TEMPLATE_STANDARD
): self {
return new self(true, $input, $rule, $parameters, $template);
}
public function withTemplate(string $template): self
@ -72,12 +82,6 @@ final class Result
return $this->clone(children: $children);
}
/** @param array<string, mixed> $parameters */
public function withParameters(array $parameters): self
{
return $this->clone(parameters: $parameters);
}
public function withNameIfMissing(string $name): self
{
return $this->clone(
@ -127,13 +131,11 @@ final class Result
}
/**
* @param array<string, mixed>|null $parameters
* @param array<Result>|null $children
*/
private function clone(
?bool $isValid = null,
?string $template = null,
?array $parameters = null,
?Mode $mode = null,
?string $name = null,
?string $id = null,
@ -144,8 +146,8 @@ final class Result
$isValid ?? $this->isValid,
$this->input,
$this->rule,
$this->parameters,
$template ?? $this->template,
$parameters ?? $this->parameters,
$mode ?? $this->mode,
$name ?? $this->name,
$id ?? $this->id,

View file

@ -36,16 +36,16 @@ abstract class AbstractRelated extends AbstractRule
$name = $this->getName() ?? (string) $this->reference;
$hasReference = $this->hasReference($input);
if ($this->mandatory && !$hasReference) {
return Result::failed($input, $this, self::TEMPLATE_NOT_PRESENT)->withNameIfMissing($name);
return Result::failed($input, $this, [], self::TEMPLATE_NOT_PRESENT)->withNameIfMissing($name);
}
if ($this->rule === null || !$hasReference) {
return Result::passed($input, $this, self::TEMPLATE_NOT_PRESENT)->withNameIfMissing($name);
return Result::passed($input, $this, [], self::TEMPLATE_NOT_PRESENT)->withNameIfMissing($name);
}
$result = $this->rule->evaluate($this->getReferenceValue($input));
return (new Result($result->isValid, $input, $this, self::TEMPLATE_INVALID))
return (new Result($result->isValid, $input, $this, [], self::TEMPLATE_INVALID))
->withChildren($result)
->withNameIfMissing($name);
}

View file

@ -31,8 +31,13 @@ abstract class AbstractRule implements Validatable
public function evaluate(mixed $input): Result
{
return (new Result($this->validate($input), $input, $this, $this->getStandardTemplate($input)))
->withParameters($this->getParams());
return new Result(
$this->validate($input),
$input,
$this,
$this->getParams(),
$this->getStandardTemplate($input)
);
}
public function check(mixed $input): void

View file

@ -43,6 +43,6 @@ final class AllOf extends Composite
$template = self::TEMPLATE_NONE;
}
return (new Result($valid, $input, $this, $template))->withChildren(...$children);
return (new Result($valid, $input, $this, [], $template))->withChildren(...$children);
}
}

View file

@ -45,7 +45,7 @@ final class Call extends AbstractRule
} catch (Throwable) {
restore_error_handler();
return Result::failed($input, $this)->withParameters(['callable' => $this->callable]);
return Result::failed($input, $this, ['callable' => $this->callable]);
}
}

View file

@ -33,9 +33,9 @@ abstract class Comparison extends Standard
$parameters = ['compareTo' => $this->compareTo];
if (!$this->isAbleToCompareValues($left, $right)) {
return Result::failed($input, $this)->withParameters($parameters);
return Result::failed($input, $this, $parameters);
}
return (new Result($this->compare($left, $right), $input, $this))->withParameters($parameters);
return new Result($this->compare($left, $right), $input, $this, $parameters);
}
}

View file

@ -33,10 +33,10 @@ final class Decimal extends Standard
{
$parameters = ['decimals' => $this->decimals];
if (!is_numeric($input)) {
return Result::failed($input, $this)->withParameters($parameters);
return Result::failed($input, $this, $parameters);
}
return new Result($this->isValidDecimal($input), $input, $this, parameters: $parameters);
return new Result($this->isValidDecimal($input), $input, $this, $parameters);
}
private function isValidDecimal(mixed $input): bool

View file

@ -23,7 +23,6 @@ abstract class Envelope extends Standard
public function evaluate(mixed $input): Result
{
return (new Result($this->rule->evaluate($input)->isValid, $input, $this))
->withParameters($this->parameters);
return new Result($this->rule->evaluate($input)->isValid, $input, $this, $this->parameters);
}
}

View file

@ -33,15 +33,13 @@ final class Extension extends Standard
{
$parameters = ['extension' => $this->extension];
if ($input instanceof SplFileInfo) {
return (new Result($this->extension === $input->getExtension(), $input, $this))
->withParameters($parameters);
return new Result($this->extension === $input->getExtension(), $input, $this, $parameters);
}
if (!is_string($input)) {
return Result::failed($input, $this)->withParameters($parameters);
return Result::failed($input, $this, $parameters);
}
return (new Result($this->extension === pathinfo($input, PATHINFO_EXTENSION), $input, $this))
->withParameters($parameters);
return new Result($this->extension === pathinfo($input, PATHINFO_EXTENSION), $input, $this, $parameters);
}
}

View file

@ -34,18 +34,18 @@ abstract class Filter extends Standard
$template = $this->additionalChars ? self::TEMPLATE_EXTRA : self::TEMPLATE_STANDARD;
$parameters = $this->additionalChars ? ['additionalChars' => $this->additionalChars] : [];
if (!is_scalar($input)) {
return Result::failed($input, $this, $template)->withParameters($parameters);
return Result::failed($input, $this, $parameters, $template);
}
$stringInput = (string) $input;
if ($stringInput === '') {
return Result::failed($input, $this, $template)->withParameters($parameters);
return Result::failed($input, $this, $parameters, $template);
}
$filteredInput = $this->filter($stringInput);
$isValid = $filteredInput === '' || $this->isValid($filteredInput);
return new Result($isValid, $input, $this, $template, $parameters);
return new Result($isValid, $input, $this, $parameters, $template);
}
private function filter(string $input): string

View file

@ -64,14 +64,12 @@ final class KeySet extends Wrapper
$missingKeys = array_diff($this->keys, $inputKeys);
if (count($missingKeys) > 0) {
return Result::failed($input, $this, self::TEMPLATE_MISSING)
->withParameters(['missingKeys' => array_values($missingKeys)]);
return Result::failed($input, $this, ['missingKeys' => array_values($missingKeys)], self::TEMPLATE_MISSING);
}
$extraKeys = array_diff($inputKeys, $this->keys);
if (count($extraKeys) > 0) {
return Result::failed($input, $this, self::TEMPLATE_EXTRA)
->withParameters(['extraKeys' => array_values($extraKeys)]);
return Result::failed($input, $this, ['extraKeys' => array_values($extraKeys)], self::TEMPLATE_EXTRA);
}
return parent::evaluate($input);

View file

@ -33,7 +33,7 @@ final class Nullable extends Wrapper
}
if ($this->getName()) {
return Result::passed($input, $this, self::TEMPLATE_NAMED);
return Result::passed($input, $this, [], self::TEMPLATE_NAMED);
}
return Result::passed($input, $this);

View file

@ -36,7 +36,7 @@ final class Optional extends Wrapper
}
if ($this->getName()) {
return Result::passed($input, $this, self::TEMPLATE_NAMED);
return Result::passed($input, $this, [], self::TEMPLATE_NAMED);
}
return Result::passed($input, $this);

View file

@ -30,9 +30,9 @@ final class Regex extends Standard
{
$parameters = ['regex' => $this->regex];
if (!is_scalar($input)) {
return Result::failed($input, $this)->withParameters($parameters);
return Result::failed($input, $this, $parameters);
}
return new Result(preg_match($this->regex, (string) $input) === 1, $input, $this, parameters: $parameters);
return new Result(preg_match($this->regex, (string) $input) === 1, $input, $this, $parameters);
}
}

View file

@ -15,6 +15,6 @@ abstract class Simple extends Standard
{
public function evaluate(mixed $input): Result
{
return new Result($this->validate($input), $input, $this, self::TEMPLATE_STANDARD);
return new Result($this->validate($input), $input, $this);
}
}

View file

@ -56,9 +56,9 @@ final class SubdivisionCode extends Standard
$parameters = ['countryName' => $this->country->getName()];
$subdivision = $this->subdivisions->getByCode($this->country->getAlpha2() . '-' . $input);
if ($this->isUndefined($input) && $subdivision === null) {
return Result::passed($input, $this)->withParameters($parameters);
return Result::passed($input, $this, $parameters);
}
return (new Result($subdivision !== null, $input, $this))->withParameters($parameters);
return new Result($subdivision !== null, $input, $this, $parameters);
}
}

View file

@ -33,9 +33,9 @@ final class Subset extends Standard
{
$parameters = ['superset' => $this->superset];
if (!is_array($input)) {
return Result::failed($input, $this)->withParameters($parameters);
return Result::failed($input, $this, $parameters);
}
return new Result(array_diff($input, $this->superset) === [], $input, $this, parameters: $parameters);
return new Result(array_diff($input, $this->superset) === [], $input, $this, $parameters);
}
}

View file

@ -50,8 +50,8 @@ final class ResultBuilder
$this->isValid,
$this->input,
$this->rule,
$this->template,
$this->parameters,
$this->template,
$this->mode,
$this->name,
$this->id,