Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve naming and delete unnecessary Mode #1526

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions library/Message/StandardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Respect\Stringifier\Stringifier;
use Respect\Validation\Message\Placeholder\Listed;
use Respect\Validation\Message\Placeholder\Quoted;
use Respect\Validation\Mode;
use Respect\Validation\Result;
use Respect\Validation\Rule;

Expand Down Expand Up @@ -115,7 +114,7 @@ private function getTemplateMessage(Result $result): string
continue;
}

if ($result->mode == Mode::INVERTED) {
if ($result->hasInvertedMode) {
return $template->inverted;
}

Expand Down
16 changes: 0 additions & 16 deletions library/Mode.php

This file was deleted.

20 changes: 10 additions & 10 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
public readonly Rule $rule,
public readonly array $parameters = [],
public readonly string $template = Rule::TEMPLATE_STANDARD,
public readonly Mode $mode = Mode::DEFAULT,
public readonly bool $hasInvertedMode = false,
public readonly ?string $name = null,
?string $id = null,
public readonly ?Result $adjacent = null,
Expand Down Expand Up @@ -199,22 +199,22 @@ public function withAdjacent(Result $adjacent): self
return $this->clone(adjacent: $adjacent);
}

public function withInvertedValidation(): self
public function withToggledValidation(): self
{
return $this->clone(
isValid: !$this->isValid,
adjacent: $this->adjacent?->withInvertedValidation(),
children: array_map(static fn (Result $child) => $child->withInvertedValidation(), $this->children),
adjacent: $this->adjacent?->withToggledValidation(),
children: array_map(static fn (Result $child) => $child->withToggledValidation(), $this->children),
);
}

public function withInvertedMode(): self
public function withToggledModeAndValidation(): self
{
return $this->clone(
isValid: !$this->isValid,
mode: $this->mode == Mode::DEFAULT ? Mode::INVERTED : Mode::DEFAULT,
adjacent: $this->adjacent?->withInvertedMode(),
children: array_map(static fn (Result $child) => $child->withInvertedMode(), $this->children),
mode: !$this->hasInvertedMode,
adjacent: $this->adjacent?->withToggledModeAndValidation(),
children: array_map(static fn (Result $child) => $child->withToggledModeAndValidation(), $this->children),
);
}

Expand Down Expand Up @@ -246,7 +246,7 @@ private function clone(
mixed $input = null,
?array $parameters = null,
?string $template = null,
?Mode $mode = null,
?bool $mode = null,
?string $name = null,
?string $id = null,
?Result $adjacent = null,
Expand All @@ -259,7 +259,7 @@ private function clone(
$this->rule,
$parameters ?? $this->parameters,
$template ?? $this->template,
$mode ?? $this->mode,
$mode ?? $this->hasInvertedMode,
$name ?? $this->name,
$id ?? $this->id,
$adjacent ?? $this->adjacent,
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/KeyOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function evaluate(mixed $input): Result
{
$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
if (!$keyExistsResult->isValid) {
return $keyExistsResult->withNameFrom($this->rule)->withInvertedMode();
return $keyExistsResult->withNameFrom($this->rule)->withToggledModeAndValidation();
}

return (new Key($this->key, $this->rule))->evaluate($input);
Expand Down
5 changes: 4 additions & 1 deletion library/Rules/NoneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ final class NoneOf extends Composite

public function evaluate(mixed $input): Result
{
$children = array_map(static fn (Rule $rule) => $rule->evaluate($input)->withInvertedMode(), $this->rules);
$children = array_map(
static fn (Rule $rule) => $rule->evaluate($input)->withToggledModeAndValidation(),
$this->rules
);
$valid = array_reduce($children, static fn (bool $carry, Result $result) => $carry && $result->isValid, true);
$failed = array_filter($children, static fn (Result $result): bool => !$result->isValid);
$template = self::TEMPLATE_SOME;
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ final class Not extends Wrapper
{
public function evaluate(mixed $input): Result
{
return $this->rule->evaluate($input)->withInvertedMode()->withPrefix('not');
return $this->rule->evaluate($input)->withToggledModeAndValidation()->withPrefix('not');
}
}
2 changes: 1 addition & 1 deletion library/Rules/NullOr.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function evaluate(mixed $input): Result
}

if (!$result->isValid) {
return $this->enrichResult($result->withInvertedValidation());
return $this->enrichResult($result->withToggledValidation());
}

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

$template = self::TEMPLATE_MORE_THAN_ONE;
$children = array_map(
static fn (Result $child) => $child->isValid ? $child->withInvertedValidation() : $child,
static fn (Result $child) => $child->isValid ? $child->withToggledValidation() : $child,
$children
);
}
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/PropertyOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function evaluate(mixed $input): Result
{
$propertyExistsResult = (new PropertyExists($this->propertyName))->evaluate($input);
if (!$propertyExistsResult->isValid) {
return $propertyExistsResult->withNameFrom($this->rule)->withInvertedMode();
return $propertyExistsResult->withNameFrom($this->rule)->withToggledModeAndValidation();
}

return (new Property($this->propertyName, $this->rule))->evaluate($input);
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/UndefOr.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function evaluate(mixed $input): Result
}

if (!$result->isValid) {
return $this->enrichResult($result->withInvertedValidation());
return $this->enrichResult($result->withToggledValidation());
}

return $this->enrichResult($result);
Expand Down
9 changes: 4 additions & 5 deletions tests/library/Builders/ResultBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Respect\Validation\Test\Builders;

use Respect\Validation\Mode;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Test\Rules\Stub;
Expand All @@ -20,7 +19,7 @@ final class ResultBuilder

private mixed $input = 'input';

private Mode $mode = Mode::DEFAULT;
private bool $hasInvertedMode = false;

private string $template = Rule::TEMPLATE_STANDARD;

Expand Down Expand Up @@ -51,7 +50,7 @@ public function build(): Result
$this->rule,
$this->parameters,
$this->template,
$this->mode,
$this->hasInvertedMode,
$this->name,
$this->id,
$this->adjacent,
Expand Down Expand Up @@ -117,9 +116,9 @@ public function children(Result ...$children): self
return $this;
}

public function mode(Mode $mode): self
public function hasInvertedMode(): self
{
$this->mode = $mode;
$this->hasInvertedMode = true;

return $this;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/Message/StandardRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Message\Translator\ArrayTranslator;
use Respect\Validation\Message\Translator\DummyTranslator;
use Respect\Validation\Mode;
use Respect\Validation\Test\Builders\ResultBuilder;
use Respect\Validation\Test\Message\TestingStringifier;
use Respect\Validation\Test\TestCase;
Expand Down Expand Up @@ -318,7 +317,7 @@ public function itShouldRenderResultWithNonCustomTemplateAndInvertedMode(): void
$stringifier = new TestingStringifier();
$renderer = new StandardRenderer($stringifier);

$result = (new ResultBuilder())->mode(Mode::INVERTED)->build();
$result = (new ResultBuilder())->hasInvertedMode()->build();

self::assertSame(
sprintf('%s must not be a valid stub', $stringifier->stringify($result->input, 0)),
Expand All @@ -331,7 +330,7 @@ public function itShouldRenderResultWithNonCustomTemplateWhenCannotFindAttachedT
{
$renderer = new StandardRenderer(new TestingStringifier());

$result = (new ResultBuilder())->template('__not_standard__')->mode(Mode::INVERTED)->build();
$result = (new ResultBuilder())->template('__not_standard__')->hasInvertedMode()->build();

self::assertSame(
$result->template,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Rules/NotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function shouldInvertTheResultOfWrappedRule(): void

self::assertEquals(
$rule->evaluate('input'),
$wrapped->evaluate('input')->withPrefix('not')->withInvertedMode()
$wrapped->evaluate('input')->withPrefix('not')->withToggledModeAndValidation()
);
}

Expand Down
Loading