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

Commit 892a974

Browse files
committed
chore: improved string comparisons
1 parent aeaa911 commit 892a974

File tree

8 files changed

+16
-12
lines changed

8 files changed

+16
-12
lines changed

src/Rule/GreaterThan.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(
2525

2626
protected function comparison(mixed $constraint, mixed $value): bool
2727
{
28+
if (\is_string($constraint) && \is_string($value)) {
29+
return strcmp($value, $constraint) > 0;
30+
}
31+
2832
return $value > $constraint;
2933
}
3034

src/Rule/GreaterThanOrEqual.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(
2525

2626
protected function comparison(mixed $constraint, mixed $value): bool
2727
{
28+
if (\is_string($constraint) && \is_string($value)) {
29+
return strcmp($value, $constraint) >= 0;
30+
}
31+
2832
return $value >= $constraint;
2933
}
3034

src/Rule/LessThan.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(
2525

2626
protected function comparison(mixed $constraint, mixed $value): bool
2727
{
28+
if (\is_string($constraint) && \is_string($value)) {
29+
return strcmp($value, $constraint) < 0;
30+
}
31+
2832
return $value < $constraint;
2933
}
3034

src/Rule/LessThanOrEqual.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(
2525

2626
protected function comparison(mixed $constraint, mixed $value): bool
2727
{
28+
if (\is_string($constraint) && \is_string($value)) {
29+
return strcmp($value, $constraint) <= 0;
30+
}
31+
2832
return $value <= $constraint;
2933
}
3034

tests/GreaterThanOrEqualTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static function provideRuleFailureConditionData(): \Generator
4141
yield 'float' => [new GreaterThanOrEqual(10.0), 1.0, $exception, $message];
4242
yield 'int with float' => [new GreaterThanOrEqual(10), 1.0, $exception, $message];
4343
yield 'string' => [new GreaterThanOrEqual('z'), 'a', $exception, $message];
44-
yield 'empty string' => [new GreaterThanOrEqual('a'), '', $exception, $message];
4544
}
4645

4746
public static function provideRuleSuccessConditionData(): \Generator
@@ -58,8 +57,6 @@ public static function provideRuleSuccessConditionData(): \Generator
5857
yield 'same int with float' => [new GreaterThanOrEqual(10), 10.0];
5958
yield 'string' => [new GreaterThanOrEqual('a'), 'z'];
6059
yield 'same string' => [new GreaterThanOrEqual('a'), 'a'];
61-
yield 'empty string' => [new GreaterThanOrEqual(''), 'a'];
62-
yield 'same empty string' => [new GreaterThanOrEqual(''), ''];
6360
}
6461

6562
public static function provideRuleMessageOptionData(): \Generator

tests/GreaterThanTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public static function provideRuleFailureConditionData(): \Generator
4747
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $message];
4848
yield 'string' => [new GreaterThan('z'), 'a', $exception, $message];
4949
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $message];
50-
yield 'empty string' => [new GreaterThan('a'), '', $exception, $message];
51-
yield 'same empty string' => [new GreaterThan(''), '', $exception, $message];
5250
}
5351

5452
public static function provideRuleSuccessConditionData(): \Generator
@@ -59,7 +57,6 @@ public static function provideRuleSuccessConditionData(): \Generator
5957
yield 'float' => [new GreaterThan(10.0), 20.0];
6058
yield 'int with float' => [new GreaterThan(10), 20.0];
6159
yield 'string' => [new GreaterThan('a'), 'z'];
62-
yield 'empty string' => [new GreaterThan(''), 'a'];
6360
}
6461

6562
public static function provideRuleMessageOptionData(): \Generator

tests/LessThanOrEqualTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static function provideRuleFailureConditionData(): \Generator
4141
yield 'float' => [new LessThanOrEqual(10.0), 20.0, $exception, $message];
4242
yield 'int with float' => [new LessThanOrEqual(10), 20.0, $exception, $message];
4343
yield 'string' => [new LessThanOrEqual('a'), 'z', $exception, $message];
44-
yield 'empty string' => [new LessThanOrEqual(''), 'a', $exception, $message];
4544
}
4645

4746
public static function provideRuleSuccessConditionData(): \Generator
@@ -58,8 +57,6 @@ public static function provideRuleSuccessConditionData(): \Generator
5857
yield 'same int with float' => [new LessThanOrEqual(10), 10.0];
5958
yield 'string' => [new LessThanOrEqual('z'), 'a'];
6059
yield 'same string' => [new LessThanOrEqual('a'), 'a'];
61-
yield 'empty string' => [new LessThanOrEqual('a'), ''];
62-
yield 'same empty string' => [new LessThanOrEqual(''), ''];
6360
}
6461

6562
public static function provideRuleMessageOptionData(): \Generator

tests/LessThanTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public static function provideRuleFailureConditionData(): \Generator
4747
yield 'same int with float' => [new LessThan(10), 10.0, $exception, $message];
4848
yield 'string' => [new LessThan('a'), 'z', $exception, $message];
4949
yield 'same string' => [new LessThan('a'), 'a', $exception, $message];
50-
yield 'empty string' => [new LessThan(''), 'a', $exception, $message];
51-
yield 'same empty string' => [new LessThan(''), '', $exception, $message];
5250
}
5351

5452
public static function provideRuleSuccessConditionData(): \Generator
@@ -59,7 +57,6 @@ public static function provideRuleSuccessConditionData(): \Generator
5957
yield 'float' => [new LessThan(10.0), 1.0];
6058
yield 'int with float' => [new LessThan(10), 1.0];
6159
yield 'string' => [new LessThan('z'), 'a'];
62-
yield 'empty string' => [new LessThan('a'), ''];
6360
}
6461

6562
public static function provideRuleMessageOptionData(): \Generator

0 commit comments

Comments
 (0)