From d0dd5b4594314be5b01b9acdb7c01acf3eec75b8 Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Wed, 16 Jun 2021 07:42:32 -0700 Subject: [PATCH] Use WildcardMatch Per suggestion from @MarkBaker. WildcardMatch did not handle double tilde correctly. It has been changed to do so and its logic simplified (and commented). Existing AutoFilter test covered this situation, but I added a test for MATCH as well. --- phpstan-baseline.neon | 10 ------- .../Calculation/Internal/WildcardMatch.php | 28 +++++++++---------- src/PhpSpreadsheet/Worksheet/AutoFilter.php | 11 ++------ tests/data/Calculation/LookupRef/MATCH.php | 6 ++++ 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ec8e0f4d..5e19caea 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -755,16 +755,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Calculation/Internal/MakeMatrix.php - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Internal\\\\WildcardMatch\\:\\:wildcard\\(\\) should return string but returns string\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Internal\\\\WildcardMatch\\:\\:compare\\(\\) has parameter \\$value with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php - - message: "#^Call to function is_string\\(\\) with null will always evaluate to false\\.$#" count: 3 diff --git a/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php b/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php index 2ba20346..5731569f 100644 --- a/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php +++ b/src/PhpSpreadsheet/Calculation/Internal/WildcardMatch.php @@ -5,30 +5,30 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\Internal; class WildcardMatch { private const SEARCH_SET = [ - '/(?getValue(); if (!is_array($ruleValue) && !is_numeric($ruleValue)) { // Convert to a regexp allowing for regexp reserved characters, wildcards and escaped wildcards - $ruleValue = preg_quote("$ruleValue"); - $ruleValue = str_replace(self::FROM_REPLACE, self::TO_REPLACE, $ruleValue); + $ruleValue = WildcardMatch::wildcard($ruleValue); if (trim($ruleValue) == '') { $customRuleForBlanks = true; $ruleValue = trim($ruleValue); diff --git a/tests/data/Calculation/LookupRef/MATCH.php b/tests/data/Calculation/LookupRef/MATCH.php index 03d9bfff..47b92528 100644 --- a/tests/data/Calculation/LookupRef/MATCH.php +++ b/tests/data/Calculation/LookupRef/MATCH.php @@ -346,4 +346,10 @@ return [ ['Obtuse', 'Amuse', 'Obverse', 'Inverse', 'Assurance', 'Amplitude', 'Adverse', 'Apartment'], 0, ], + [ + 3, // Expected + '*~~*', // contains a tilde + ['aAAAAA', 'a123456*c', 'abc~xyz', 'alembic'], + 0, + ], ];