Skip to content

Commit b352f17

Browse files
committed
Remove "Wrapper" abstract class
With PHP's promoted properties, there is little reason to have an abstract class that only provides a constructor and a proxy method. The greater part of the classes that extended Wrapper had their own implementation of evaluate(), hence it made very little sense to keep Wrapper in the codebase. Assisted-by: Claude Code (Opus 4.5)
1 parent bcc60ec commit b352f17

File tree

19 files changed

+73
-144
lines changed

19 files changed

+73
-144
lines changed

docs/migrating-from-v2-to-v3.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ final class Custom implements Validator
556556
Base classes available in `Respect\Validation\Validators\Core`:
557557

558558
- `Simple` - For validators with simple boolean logic
559-
- `Wrapper` - For validators that wrap another validator
560559
- `Composite` - For validators that combine multiple validators
561560
- `Envelope` - For validators that modify how another validator works
562561

src/ValidatorBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use function is_string;
2727

2828
/** @mixin Builder */
29-
final readonly class ValidatorBuilder implements Validator, Nameable
29+
final readonly class ValidatorBuilder implements Nameable
3030
{
3131
/** @var array<Validator> */
3232
private array $validators;

src/Validators/Core/FilteredArray.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
namespace Respect\Validation\Validators\Core;
1313

1414
use Respect\Validation\Result;
15+
use Respect\Validation\Validator;
1516
use Respect\Validation\Validators\IterableType;
1617

1718
use function is_array;
1819
use function iterator_to_array;
1920

20-
abstract class FilteredArray extends Wrapper
21+
abstract class FilteredArray implements Validator
2122
{
23+
public function __construct(
24+
protected Validator $validator,
25+
) {
26+
}
27+
2228
public function evaluate(mixed $input): Result
2329
{
2430
$iterableResult = (new IterableType())->evaluate($input);

src/Validators/Core/Nameable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
namespace Respect\Validation\Validators\Core;
1616

1717
use Respect\Validation\Name;
18+
use Respect\Validation\Validator;
1819

19-
interface Nameable
20+
interface Nameable extends Validator
2021
{
2122
public function getName(): Name|null;
2223
}

src/Validators/Core/Reducer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@
1010

1111
namespace Respect\Validation\Validators\Core;
1212

13+
use Respect\Validation\Result;
1314
use Respect\Validation\Validator;
1415
use Respect\Validation\Validators\AllOf;
1516

16-
final class Reducer extends Wrapper
17+
final readonly class Reducer implements Validator
1718
{
19+
private Validator $validator;
20+
1821
public function __construct(Validator $validator1, Validator ...$validators)
1922
{
20-
parent::__construct($validators === [] ? $validator1 : new AllOf($validator1, ...$validators));
23+
$this->validator = $validators === [] ? $validator1 : new AllOf($validator1, ...$validators);
24+
}
25+
26+
public function evaluate(mixed $input): Result
27+
{
28+
return $this->validator->evaluate($input);
2129
}
2230
}

src/Validators/Core/Wrapper.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Validators/Key.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
use Respect\Validation\Result;
2121
use Respect\Validation\Validator;
2222
use Respect\Validation\Validators\Core\KeyRelated;
23-
use Respect\Validation\Validators\Core\Wrapper;
2423

2524
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26-
final class Key extends Wrapper implements KeyRelated
25+
final readonly class Key implements KeyRelated
2726
{
2827
public function __construct(
29-
private readonly int|string $key,
30-
Validator $validator,
28+
private int|string $key,
29+
private Validator $validator,
3130
) {
32-
parent::__construct($validator);
3331
}
3432

3533
public function getKey(): int|string

src/Validators/KeyOptional.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
use Respect\Validation\Result;
1616
use Respect\Validation\Validator;
1717
use Respect\Validation\Validators\Core\KeyRelated;
18-
use Respect\Validation\Validators\Core\Wrapper;
1918

2019
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
21-
final class KeyOptional extends Wrapper implements KeyRelated
20+
final readonly class KeyOptional implements KeyRelated
2221
{
2322
public function __construct(
24-
private readonly int|string $key,
25-
Validator $validator,
23+
private int|string $key,
24+
private Validator $validator,
2625
) {
27-
parent::__construct($validator);
2826
}
2927

3028
public function getKey(): int|string

src/Validators/Length.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Countable as PhpCountable;
2323
use Respect\Validation\Message\Template;
2424
use Respect\Validation\Result;
25-
use Respect\Validation\Validators\Core\Wrapper;
25+
use Respect\Validation\Validator;
2626

2727
use function count;
2828
use function is_array;
@@ -40,10 +40,15 @@
4040
'{{subject}} must not be a countable value or a string',
4141
self::TEMPLATE_WRONG_TYPE,
4242
)]
43-
final class Length extends Wrapper
43+
final readonly class Length implements Validator
4444
{
4545
public const string TEMPLATE_WRONG_TYPE = '__wrong_type__';
4646

47+
public function __construct(
48+
private Validator $validator,
49+
) {
50+
}
51+
4752
public function evaluate(mixed $input): Result
4853
{
4954
$length = $this->extractLength($input);

src/Validators/Named.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
use Respect\Validation\Result;
1616
use Respect\Validation\Validator;
1717
use Respect\Validation\Validators\Core\Nameable;
18-
use Respect\Validation\Validators\Core\Wrapper;
1918

2019
use function is_string;
2120

2221
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
23-
final class Named extends Wrapper implements Nameable
22+
final readonly class Named implements Nameable
2423
{
25-
private readonly Name $name;
26-
27-
public function __construct(string|Name $name, Validator $validator)
28-
{
29-
parent::__construct($validator);
24+
private Name $name;
3025

26+
public function __construct(
27+
string|Name $name,
28+
private Validator $validator,
29+
) {
3130
$this->name = is_string($name) ? new Name($name) : $name;
3231
}
3332

0 commit comments

Comments
 (0)