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

Commit 7641715

Browse files
committed
chore: changed comparable check to be more obvious
1 parent 39bd924 commit 7641715

File tree

8 files changed

+81
-46
lines changed

8 files changed

+81
-46
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class UnexpectedComparableException extends UnexpectedValueException
6+
{
7+
public function __construct(string $value1, string $value2)
8+
{
9+
$message = \sprintf('Cannot compare a type "%s" with a type "%s".', $value1, $value2);
10+
11+
parent::__construct($message);
12+
}
13+
}

src/Rule/GreaterThan.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\IsComparableTrait;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

910
class GreaterThan extends AbstractRule implements RuleInterface
1011
{
11-
use AssertIsComparableTrait;
12+
use IsComparableTrait;
1213

1314
private array $options;
1415

@@ -28,7 +29,12 @@ public function __construct(private readonly mixed $constraint, array $options =
2829
*/
2930
public function assert(mixed $value, string $name): void
3031
{
31-
$this->assertIsComparable($this->constraint, $value);
32+
if (!$this->isComparable($this->constraint, $value)) {
33+
throw new UnexpectedComparableException(
34+
get_debug_type($this->constraint),
35+
get_debug_type($value)
36+
);
37+
}
3238

3339
if (!($value > $this->constraint)) {
3440
throw new GreaterThanException(

src/Rule/GreaterThanOrEqual.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanOrEqualException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\IsComparableTrait;
89
use Symfony\Component\OptionsResolver\OptionsResolver;
910

1011
class GreaterThanOrEqual extends AbstractRule implements RuleInterface
1112
{
12-
use AssertIsComparableTrait;
13+
use IsComparableTrait;
1314

1415
private array $options;
1516

@@ -29,7 +30,12 @@ public function __construct(private readonly mixed $constraint, array $options =
2930
*/
3031
public function assert(mixed $value, string $name): void
3132
{
32-
$this->assertIsComparable($this->constraint, $value);
33+
if (!$this->isComparable($this->constraint, $value)) {
34+
throw new UnexpectedComparableException(
35+
get_debug_type($this->constraint),
36+
get_debug_type($value)
37+
);
38+
}
3339

3440
if (!($value >= $this->constraint)) {
3541
throw new GreaterThanOrEqualException(

src/Rule/LessThan.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\IsComparableTrait;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

910
class LessThan extends AbstractRule implements RuleInterface
1011
{
11-
use AssertIsComparableTrait;
12+
use IsComparableTrait;
1213

1314
private array $options;
1415

@@ -28,7 +29,12 @@ public function __construct(private readonly mixed $constraint, array $options =
2829
*/
2930
public function assert(mixed $value, string $name): void
3031
{
31-
$this->assertIsComparable($this->constraint, $value);
32+
if (!$this->isComparable($this->constraint, $value)) {
33+
throw new UnexpectedComparableException(
34+
get_debug_type($this->constraint),
35+
get_debug_type($value)
36+
);
37+
}
3238

3339
if (!($value < $this->constraint)) {
3440
throw new LessThanException(

src/Rule/LessThanOrEqual.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanOrEqualException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\IsComparableTrait;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

910
class LessThanOrEqual extends AbstractRule implements RuleInterface
1011
{
11-
use AssertIsComparableTrait;
12+
use IsComparableTrait;
1213

1314
private array $options;
1415

@@ -28,7 +29,12 @@ public function __construct(private readonly mixed $constraint, array $options =
2829
*/
2930
public function assert(mixed $value, string $name): void
3031
{
31-
$this->assertIsComparable($this->constraint, $value);
32+
if (!$this->isComparable($this->constraint, $value)) {
33+
throw new UnexpectedComparableException(
34+
get_debug_type($this->constraint),
35+
get_debug_type($value)
36+
);
37+
}
3238

3339
if (!($value <= $this->constraint)) {
3440
throw new LessThanOrEqualException(

src/Rule/Range.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RangeException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
67
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\IsComparableTrait;
89
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
910
use Symfony\Component\OptionsResolver\OptionsResolver;
1011

1112
class Range extends AbstractRule implements RuleInterface
1213
{
13-
use AssertIsComparableTrait;
14+
use IsComparableTrait;
1415

1516
private array $options;
1617

@@ -31,7 +32,12 @@ public function __construct(
3132

3233
public function assert(mixed $value, string $name): void
3334
{
34-
$this->assertIsComparable($this->minConstraint, $this->maxConstraint);
35+
if (!$this->isComparable($this->minConstraint, $this->maxConstraint)) {
36+
throw new UnexpectedComparableException(
37+
get_debug_type($this->minConstraint),
38+
get_debug_type($this->maxConstraint)
39+
);
40+
}
3541

3642
if (
3743
!Validator

src/Rule/Util/AssertIsComparableTrait.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule\Util;
4+
5+
trait IsComparableTrait
6+
{
7+
private function isComparable(mixed $value1, mixed $value2): bool
8+
{
9+
if ($value1 instanceof \DateTimeInterface && $value2 instanceof \DateTimeInterface) {
10+
return true;
11+
}
12+
13+
if (\is_numeric($value1) && \is_numeric($value2)) {
14+
return true;
15+
}
16+
17+
if (\is_string($value1) && \is_string($value2)) {
18+
return true;
19+
}
20+
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)