Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1868f88

Browse files
committed
chore: changed validatable check to be more obvious
1 parent 7641715 commit 1868f88

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/Rule/All.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\AllException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsValidatableTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\IsValidatableTrait;
99
use Symfony\Component\OptionsResolver\OptionsResolver;
1010

1111
class All extends AbstractRule implements RuleInterface
1212
{
13-
use AssertIsValidatableTrait;
13+
use IsValidatableTrait;
1414

1515
private array $options;
1616

@@ -33,7 +33,11 @@ public function __construct(
3333

3434
public function assert(mixed $value, string $name): void
3535
{
36-
$this->assertIsValidatable($this->constraints);
36+
if (!$this->isValidatable($this->constraints)) {
37+
throw new UnexpectedValueException(
38+
'All constraints must be of type "RuleInterface".'
39+
);
40+
}
3741

3842
if (!\is_array($value)) {
3943
throw new UnexpectedValueException(

src/Rule/Util/AssertIsValidatableTrait.php renamed to src/Rule/Util/IsValidatableTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
77

8-
trait AssertIsValidatableTrait
8+
trait IsValidatableTrait
99
{
10-
private function assertIsValidatable(array $constraints): bool
10+
private function isValidatable(array $constraints): bool
1111
{
1212
foreach ($constraints as $constraint) {
1313
if (!$constraint instanceof RuleInterface) {
14-
throw new UnexpectedValueException(
15-
\sprintf('Expected constraint of type "RuleInterface", "%s" given.', get_debug_type($constraint))
16-
);
14+
return false;
1715
}
1816
}
1917

tests/AllTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
2424
yield 'invalid constraint' => [
2525
new All([new NotBlank(), 'invalid']),
2626
[1, 2, 3],
27-
'/Expected constraint of type "RuleInterface", "(.*)" given./'
27+
'/All constraints must be of type "RuleInterface"./'
2828
];
2929
yield 'invalid value type' => [
3030
new All([new NotBlank()]),

0 commit comments

Comments
 (0)