Skip to content

Commit 94ddfcd

Browse files
committed
Create named constructor to create Result
The constructor of `Result` has many arguments, but that's not the primary reason why I'm making this change. I want to change the constructor, and it will become more complicated, so having this named constructor will be useful in the next refactoring. With this change, I also made the `id` mandatory. That made the constructor look neater and most to promote almost all properties to the constructor. Another change was removing the `fromAdjacent` method, which was quite confusing. I created the `asAdjacentOf` method, which is a bit clearer. If anything, it makes all static methods named constructors. It will be a bit more verbose, but more intuitive.
1 parent 4840527 commit 94ddfcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+117
-100
lines changed

library/Result.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,70 @@
2121
/** @var array<Result> */
2222
public array $children;
2323

24-
public Id $id;
25-
2624
/** @param array<string, mixed> $parameters */
2725
public function __construct(
2826
public bool $hasPassed,
2927
public mixed $input,
3028
public Rule $rule,
29+
public Id $id,
3130
public array $parameters = [],
3231
public string $template = Rule::TEMPLATE_STANDARD,
3332
public bool $hasInvertedMode = false,
3433
public Name|null $name = null,
35-
Id|null $id = null,
3634
public Result|null $adjacent = null,
3735
public Path|null $path = null,
3836
Result ...$children,
3937
) {
40-
$this->id = $id ?? Id::fromRule($rule);
4138
$this->children = $children;
4239
}
4340

4441
/** @param array<string, mixed> $parameters */
45-
public static function failed(
42+
public static function of(
43+
bool $hasPassed,
4644
mixed $input,
4745
Rule $rule,
4846
array $parameters = [],
4947
string $template = Rule::TEMPLATE_STANDARD,
5048
): self {
51-
return new self(false, $input, $rule, $parameters, $template);
49+
return new self($hasPassed, $input, $rule, Id::fromRule($rule), $parameters, $template);
5250
}
5351

5452
/** @param array<string, mixed> $parameters */
55-
public static function passed(
53+
public static function failed(
5654
mixed $input,
5755
Rule $rule,
5856
array $parameters = [],
5957
string $template = Rule::TEMPLATE_STANDARD,
6058
): self {
61-
return new self(true, $input, $rule, $parameters, $template);
59+
return self::of(false, $input, $rule, $parameters, $template);
6260
}
6361

6462
/** @param array<string, mixed> $parameters */
65-
public static function fromAdjacent(
63+
public static function passed(
6664
mixed $input,
67-
string $prefix,
6865
Rule $rule,
69-
Result $adjacent,
7066
array $parameters = [],
7167
string $template = Rule::TEMPLATE_STANDARD,
72-
): Result {
73-
if ($adjacent->allowsAdjacent()) {
74-
return (new Result(
75-
$adjacent->hasPassed,
76-
$input,
77-
$rule,
78-
$parameters,
79-
$template,
80-
id: $adjacent->id->withPrefix($prefix),
81-
))->withAdjacent($adjacent->withInput($input));
82-
}
68+
): self {
69+
return self::of(true, $input, $rule, $parameters, $template);
70+
}
8371

84-
$childrenAsAdjacent = array_map(
85-
static fn(Result $child) => self::fromAdjacent($input, $prefix, $rule, $child, $parameters, $template),
86-
$adjacent->children,
87-
);
72+
public function asAdjacentOf(Result $result, string $prefix): Result
73+
{
74+
if ($this->allowsAdjacent()) {
75+
return clone ($result, [
76+
'id' => $this->id->withPrefix($prefix),
77+
'adjacent' => $this->withInput($result->input),
78+
]);
79+
}
8880

89-
return $adjacent->withInput($input)->withChildren(...$childrenAsAdjacent);
81+
return clone ($this, [
82+
'input' => $result->input,
83+
'children' => array_map(
84+
static fn(Result $child) => $child->asAdjacentOf($result, $prefix),
85+
$this->children,
86+
),
87+
]);
9088
}
9189

9290
public function withTemplate(string $template): self

library/Rules/AllOf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function evaluate(mixed $input): Result
4646
$template = self::TEMPLATE_ALL;
4747
}
4848

49-
return (new Result($valid, $input, $this, [], $template))->withChildren(...$children);
49+
return Result::of($valid, $input, $this, [], $template)->withChildren(...$children);
5050
}
5151
}

library/Rules/AnyOf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public function evaluate(mixed $input): Result
3434
false,
3535
);
3636

37-
return (new Result($valid, $input, $this))->withChildren(...$children);
37+
return Result::of($valid, $input, $this)->withChildren(...$children);
3838
}
3939
}

library/Rules/Base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838

3939
public function evaluate(mixed $input): Result
4040
{
41-
return new Result(
41+
return Result::of(
4242
(bool) preg_match('@^[' . mb_substr($this->chars, 0, $this->base) . ']+$@', (string) $input),
4343
$input,
4444
$this,

library/Rules/Charset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(string $charset, string ...$charsets)
4747

4848
public function evaluate(mixed $input): Result
4949
{
50-
return new Result(
50+
return Result::of(
5151
in_array(mb_detect_encoding($input, $this->charset, true), $this->charset),
5252
$input,
5353
$this,

library/Rules/Contains.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public function evaluate(mixed $input): Result
3737
{
3838
$parameters = ['containsValue' => $this->containsValue];
3939
if (is_array($input)) {
40-
return new Result(in_array($this->containsValue, $input, $this->identical), $input, $this, $parameters);
40+
return Result::of(in_array($this->containsValue, $input, $this->identical), $input, $this, $parameters);
4141
}
4242

4343
if (!is_scalar($input) || !is_scalar($this->containsValue)) {
4444
return Result::failed($input, $this, $parameters);
4545
}
4646

47-
return new Result(
47+
return Result::of(
4848
$this->validateString((string) $input, (string) $this->containsValue),
4949
$input,
5050
$this,

library/Rules/Core/Comparison.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function evaluate(mixed $input): Result
3333
return Result::failed($input, $this, $parameters);
3434
}
3535

36-
return new Result($this->compare($left, $right), $input, $this, $parameters);
36+
return Result::of($this->compare($left, $right), $input, $this, $parameters);
3737
}
3838

3939
abstract protected function compare(mixed $left, mixed $right): bool;

library/Rules/Core/Envelope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function __construct(
2323

2424
public function evaluate(mixed $input): Result
2525
{
26-
return new Result($this->rule->evaluate($input)->hasPassed, $input, $this, $this->parameters);
26+
return Result::of($this->rule->evaluate($input)->hasPassed, $input, $this, $this->parameters);
2727
}
2828
}

library/Rules/Core/FilteredString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function evaluate(mixed $input): Result
4444
$filteredInput = $this->filter($stringInput);
4545
$isValid = $filteredInput === '' || $this->isValid($filteredInput);
4646

47-
return new Result($isValid, $input, $this, $parameters, $template);
47+
return Result::of($isValid, $input, $this, $parameters, $template);
4848
}
4949

5050
abstract protected function isValid(string $input): bool;

library/Rules/Core/Simple.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ abstract public function isValid(mixed $input): bool;
1818

1919
public function evaluate(mixed $input): Result
2020
{
21-
return new Result($this->isValid($input), $input, $this);
21+
return Result::of($this->isValid($input), $input, $this);
2222
}
2323
}

0 commit comments

Comments
 (0)