Skip to content

Commit 0e8fbc5

Browse files
committed
Apply review suggestions
removing parameter uniontypes & unnecessary typechecking, change switch to match
1 parent eb5ac10 commit 0e8fbc5

File tree

1 file changed

+29
-58
lines changed

1 file changed

+29
-58
lines changed

src/Filter.php

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace ipl\Stdlib;
44

5-
use DateTime;
65
use InvalidArgumentException;
76
use ipl\Stdlib\Filter\All;
87
use ipl\Stdlib\Filter\Any;
@@ -150,11 +149,11 @@ protected function matchNone(None $rules, object $row): bool
150149
* Create a rule that matches rows with a column that **equals** the given value
151150
*
152151
* @param string $column
153-
* @param array<mixed>|bool|float|int|string|DateTime|null $value
152+
* @param array<mixed>|bool|float|int|string $value
154153
*
155154
* @return Condition
156155
*/
157-
public static function equal(string $column, array|bool|float|int|string|DateTime|null $value): Condition
156+
public static function equal(string $column, $value): Condition
158157
{
159158
return new Equal($column, $value);
160159
}
@@ -169,15 +168,6 @@ public static function equal(string $column, array|bool|float|int|string|DateTim
169168
*/
170169
protected function matchEqual(Equal|Unequal $rule, object $row): bool
171170
{
172-
if (! $rule instanceof Equal && ! $rule instanceof Unequal) {
173-
throw new InvalidArgumentException(sprintf(
174-
'Rule must be of type %s or %s, got %s instead',
175-
Equal::class,
176-
Unequal::class,
177-
get_php_type($rule)
178-
));
179-
}
180-
181171
$rowValue = $this->extractValue($rule->getColumn(), $row);
182172
$value = $rule->getValue();
183173
$this->normalizeTypes($rowValue, $value);
@@ -220,15 +210,6 @@ public static function like(string $column, string|array|null $value): Condition
220210
*/
221211
protected function matchSimilar(Like|Unlike $rule, object $row): bool
222212
{
223-
if (! $rule instanceof Like && ! $rule instanceof Unlike) {
224-
throw new InvalidArgumentException(sprintf(
225-
'Rule must be of type %s or %s, got %s instead',
226-
Like::class,
227-
Unlike::class,
228-
get_php_type($rule)
229-
));
230-
}
231-
232213
$rowValue = $this->extractValue($rule->getColumn(), $row);
233214
$value = $rule->getValue();
234215
$this->normalizeTypes($rowValue, $value);
@@ -320,11 +301,11 @@ protected function performSimilarityMatch(mixed $value, mixed $rowValue, bool $i
320301
* Create a rule that matches rows with a column that is **unequal** with the given value
321302
*
322303
* @param string $column
323-
* @param array<mixed>|bool|float|int|string|DateTime|null $value
304+
* @param array<mixed>|bool|float|int|string $value
324305
*
325306
* @return Condition
326307
*/
327-
public static function unequal(string $column, array|bool|float|int|string|DateTime|null $value): Condition
308+
public static function unequal(string $column, $value): Condition
328309
{
329310
return new Unequal($column, $value);
330311
}
@@ -374,11 +355,11 @@ protected function matchUnlike(Unlike $rule, object $row): bool
374355
* Create a rule that matches rows with a column that is **greater** than the given value
375356
*
376357
* @param string $column
377-
* @param float|int|string|DateTime|null $value
358+
* @param float|int|string $value
378359
*
379360
* @return Condition
380361
*/
381-
public static function greaterThan(string $column, float|int|string|DateTime|null $value): Condition
362+
public static function greaterThan(string $column, $value): Condition
382363
{
383364
return new GreaterThan($column, $value);
384365
}
@@ -403,11 +384,11 @@ protected function matchGreaterThan(GreaterThan $rule, object $row): bool
403384
* Create a rule that matches rows with a column that is **less** than the given value
404385
*
405386
* @param string $column
406-
* @param float|int|string|DateTime|null $value
387+
* @param float|int|string $value
407388
*
408389
* @return Condition
409390
*/
410-
public static function lessThan(string $column, float|int|string|DateTime|null $value): Condition
391+
public static function lessThan(string $column, $value): Condition
411392
{
412393
return new LessThan($column, $value);
413394
}
@@ -432,11 +413,11 @@ protected function matchLessThan(LessThan $rule, object $row): bool
432413
* Create a rule that matches rows with a column that is **greater** than or **equal** to the given value
433414
*
434415
* @param string $column
435-
* @param float|int|string|DateTime|null $value
416+
* @param float|int|string $value
436417
*
437418
* @return Condition
438419
*/
439-
public static function greaterThanOrEqual(string $column, float|int|string|DateTime|null $value): Condition
420+
public static function greaterThanOrEqual(string $column, $value): Condition
440421
{
441422
return new GreaterThanOrEqual($column, $value);
442423
}
@@ -461,11 +442,11 @@ protected function matchGreaterThanOrEqual(GreaterThanOrEqual $rule, object $row
461442
* Create a rule that matches rows with a column that is **less** than or **equal** to the given value
462443
*
463444
* @param string $column
464-
* @param float|int|string|DateTime|null $value
445+
* @param float|int|string $value
465446
*
466447
* @return Condition
467448
*/
468-
public static function lessThanOrEqual(string $column, float|int|string|DateTime|null $value): Condition
449+
public static function lessThanOrEqual(string $column, $value): Condition
469450
{
470451
return new LessThanOrEqual($column, $value);
471452
}
@@ -496,35 +477,25 @@ protected function matchLessThanOrEqual(LessThanOrEqual $rule, object $row): boo
496477
*/
497478
protected function performMatch(Rule $rule, object $row): bool
498479
{
499-
switch (true) {
500-
case $rule instanceof All:
501-
return $this->matchAll($rule, $row);
502-
case $rule instanceof Any:
503-
return $this->matchAny($rule, $row);
504-
case $rule instanceof Like:
505-
return $this->matchSimilar($rule, $row);
506-
case $rule instanceof Equal:
507-
return $this->matchEqual($rule, $row);
508-
case $rule instanceof GreaterThan:
509-
return $this->matchGreaterThan($rule, $row);
510-
case $rule instanceof GreaterThanOrEqual:
511-
return $this->matchGreaterThanOrEqual($rule, $row);
512-
case $rule instanceof LessThan:
513-
return $this->matchLessThan($rule, $row);
514-
case $rule instanceof LessThanOrEqual:
515-
return $this->matchLessThanOrEqual($rule, $row);
516-
case $rule instanceof None:
517-
return $this->matchNone($rule, $row);
518-
case $rule instanceof Unequal:
519-
return $this->matchUnequal($rule, $row);
520-
case $rule instanceof Unlike:
521-
return $this->matchUnlike($rule, $row);
522-
default:
523-
throw new InvalidArgumentException(sprintf(
480+
return match (true) {
481+
$rule instanceof All => $this->matchAll($rule, $row),
482+
$rule instanceof Any => $this->matchAny($rule, $row),
483+
$rule instanceof Like => $this->matchSimilar($rule, $row),
484+
$rule instanceof Equal => $this->matchEqual($rule, $row),
485+
$rule instanceof GreaterThan => $this->matchGreaterThan($rule, $row),
486+
$rule instanceof GreaterThanOrEqual => $this->matchGreaterThanOrEqual($rule, $row),
487+
$rule instanceof LessThan => $this->matchLessThan($rule, $row),
488+
$rule instanceof LessThanOrEqual => $this->matchLessThanOrEqual($rule, $row),
489+
$rule instanceof None => $this->matchNone($rule, $row),
490+
$rule instanceof Unequal => $this->matchUnequal($rule, $row),
491+
$rule instanceof Unlike => $this->matchUnlike($rule, $row),
492+
default => throw new InvalidArgumentException(
493+
sprintf(
524494
'Unable to match filter. Rule type %s is unknown',
525495
get_class($rule)
526-
));
527-
}
496+
)
497+
),
498+
};
528499
}
529500

530501
/**

0 commit comments

Comments
 (0)