Skip to content

Commit

Permalink
Do not allow cloning Result with different parameters
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
henriquemoody committed Feb 23, 2024
1 parent 5b81e96 commit 74fd472
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 51 deletions.
34 changes: 18 additions & 16 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function __construct(
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,
Expand All @@ -47,14 +47,24 @@ public function __construct(
$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
Expand All @@ -72,12 +82,6 @@ public function withChildren(Result ...$children): self
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(
Expand Down Expand Up @@ -127,13 +131,11 @@ public function isAlwaysVisible(): bool
}

/**
* @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,
Expand All @@ -144,8 +146,8 @@ private function clone(
$isValid ?? $this->isValid,
$this->input,
$this->rule,
$this->parameters,
$template ?? $this->template,
$parameters ?? $this->parameters,
$mode ?? $this->mode,
$name ?? $this->name,
$id ?? $this->id,
Expand Down
6 changes: 3 additions & 3 deletions library/Rules/AbstractRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public function evaluate(mixed $input): Result
$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);
}
Expand Down
9 changes: 7 additions & 2 deletions library/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ public function assert(mixed $input): void

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
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public function evaluate(mixed $input): Result
$template = self::TEMPLATE_NONE;
}

return (new Result($valid, $input, $this, $template))->withChildren(...$children);
return (new Result($valid, $input, $this, [], $template))->withChildren(...$children);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function evaluate(mixed $input): Result
} catch (Throwable) {
restore_error_handler();

return Result::failed($input, $this)->withParameters(['callable' => $this->callable]);
return Result::failed($input, $this, ['callable' => $this->callable]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function evaluate(mixed $input): Result
$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);
}
}
4 changes: 2 additions & 2 deletions library/Rules/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function evaluate(mixed $input): Result
{
$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
Expand Down
3 changes: 1 addition & 2 deletions library/Rules/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function __construct(

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);
}
}
8 changes: 3 additions & 5 deletions library/Rules/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ public function evaluate(mixed $input): Result
{
$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);
}
}
6 changes: 3 additions & 3 deletions library/Rules/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public function evaluate(mixed $input): Result
$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
Expand Down
6 changes: 2 additions & 4 deletions library/Rules/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ public function evaluate(mixed $input): Result

$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);
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function evaluate(mixed $input): Result
}

if ($this->getName()) {
return Result::passed($input, $this, self::TEMPLATE_NAMED);
return Result::passed($input, $this, [], self::TEMPLATE_NAMED);
}

return Result::passed($input, $this);
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function evaluate(mixed $input): Result
}

if ($this->getName()) {
return Result::passed($input, $this, self::TEMPLATE_NAMED);
return Result::passed($input, $this, [], self::TEMPLATE_NAMED);
}

return Result::passed($input, $this);
Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function evaluate(mixed $input): Result
{
$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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions library/Rules/SubdivisionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function evaluate(mixed $input): Result
$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);
}
}
4 changes: 2 additions & 2 deletions library/Rules/Subset.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function evaluate(mixed $input): Result
{
$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);
}
}
2 changes: 1 addition & 1 deletion tests/library/Builders/ResultBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function build(): Result
$this->isValid,
$this->input,
$this->rule,
$this->template,
$this->parameters,
$this->template,
$this->mode,
$this->name,
$this->id,
Expand Down

0 comments on commit 74fd472

Please sign in to comment.