From cd303a3a12a7e7e34cd026942dc8a03cfcb0b099 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 14 Mar 2023 21:20:02 +0100 Subject: [PATCH] Filter: remove duplicated code in combine() already assume by __construct() --- src/Filter.php | 42 +++++++++++------------------------------- tests/FilterTest.php | 4 ++-- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/Filter.php b/src/Filter.php index cbd2c78..76cbcbb 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -146,6 +146,9 @@ class Filter { /** * Constructor * @param array|bool> $args + * @throws CombineException + * @throws FilterException + * @throws ParserException * @return void */ public function __construct(...$args) { @@ -181,7 +184,7 @@ class Filter { // Check number of filters against logical operator if (self :: is_not_op($this -> log_op) && count($this -> sub_filters) != 1) - throw new FilterException( + throw new CombineException( 'Invalid constructor arguments: NOT operator must be followed by exactly one filter'); return; } @@ -361,40 +364,17 @@ class Filter { * * @param string $log_op The locical operator. May be "and", "or", "not" or the subsequent logical * equivalents "&", "|", "!" - * @param array> $args LDAP filters to combine: could be - * LDAP objects, LDAP strings or array of - * LDAP objects or LDAP strings. + * @param array> $filters LDAP filters to combine: could be + * LDAP objects, LDAP strings or array + * of LDAP objects or LDAP strings. * @throws CombineException + * @throws FilterException + * @throws ParserException * @return Filter * @static */ - public static function combine($log_op, ...$args) { - // Unalias & check logical operator - $log_op = self :: unalias_log_op($log_op); - if (!$log_op) throw new CombineException('Invalid logical operator provided!'); - - // Convert args as filters - $filters = array(); - foreach ($args as $arg) { - if (!is_array($arg)) - $arg = array($arg); - foreach ($arg as $a) { - if (is_string($a)) - $a = self :: parse($a); - if (!$a instanceof Filter) - throw new CombineException( - 'Invalid filter provided: must be a Filter object or a LDAP filter string!' - ); - $filters[] = $a; - } - } - - // Check number of filters against logical operator - if (self :: is_not_op($log_op)) { - if (count($filters) != 1) - throw new CombineException('NOT operator accept only one filter!'); - } - + public static function combine($log_op, ...$filters) { + // @phpstan-ignore-next-line return new Filter($log_op, ...$filters); } diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 18be116..27c45d4 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -434,7 +434,7 @@ final class FilterTest extends TestCase { * @covers \EesyLDAP\Filter::combine */ public function testCombineInvalidLogOp() { - $this->expectException(CombineException::class); + $this->expectException(FilterException::class); $a = Filter::combine('X', new Filter('a', '=', 'b'), new Filter('c', '>=', '2')); } @@ -442,7 +442,7 @@ final class FilterTest extends TestCase { * @covers \EesyLDAP\Filter::combine */ public function testCombineInvalidFilter() { - $this->expectException(CombineException::class); + $this->expectException(FilterException::class); // @phpstan-ignore-next-line $a = Filter::combine('&', new Filter('a', '=', 'b'), new FilterException('test')); }