diff --git a/CHANGELOG.md b/CHANGELOG.md index c8e9b3f30..39ddb65dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Support table structure with `COMPRESSED` columns (#351) * Add `#[\AllowDynamicProperties]` on `Statement` and `Expression` classes for PHP 8.2 support * Support `ALTER` queries of `PARTITIONS` (#329) +* Change `Context::load()` error handling to returning a boolean value instead of throwing a `LoaderException` (#384) ## [5.5.0] - 2021-12-08 diff --git a/src/Context.php b/src/Context.php index 8087e36cc..9b0954477 100644 --- a/src/Context.php +++ b/src/Context.php @@ -4,8 +4,6 @@ namespace PhpMyAdmin\SqlParser; -use PhpMyAdmin\SqlParser\Exceptions\LoaderException; - use function class_exists; use function explode; use function in_array; @@ -526,9 +524,9 @@ public static function isSeparator(string $string): bool * * @param string $context name of the context or full class name that defines the context * - * @throws LoaderException if the specified context doesn't exist. + * @return bool true if the context was loaded, false otherwise */ - public static function load(string $context = ''): void + public static function load(string $context = ''): bool { if (empty($context)) { $context = self::$defaultContext; @@ -540,11 +538,13 @@ public static function load(string $context = ''): void } if (! class_exists($context)) { - throw @new LoaderException('Specified context ("' . $context . '") does not exist.', $context); + return false; } self::$loadedContext = $context; self::$keywords = $context::$keywords; + + return true; } /** @@ -563,24 +563,22 @@ public static function loadClosest(string $context = ''): ?string { $length = strlen($context); for ($i = $length; $i > 0;) { - try { - /* Trying to load the new context */ - static::load($context); - + /* Trying to load the new context */ + if (static::load($context)) { return $context; - } catch (LoaderException $e) { - /* Replace last two non zero digits by zeroes */ - do { - $i -= 2; - $part = substr($context, $i, 2); - /* No more numeric parts to strip */ - if (! is_numeric($part)) { - break 2; - } - } while (intval($part) === 0 && $i > 0); - - $context = substr($context, 0, $i) . '00' . substr($context, $i + 2); } + + /* Replace last two non zero digits by zeroes */ + do { + $i -= 2; + $part = substr($context, $i, 2); + /* No more numeric parts to strip */ + if (! is_numeric($part)) { + break 2; + } + } while (intval($part) === 0 && $i > 0); + + $context = substr($context, 0, $i) . '00' . substr($context, $i + 2); } /* Fallback to loading at least matching engine */ diff --git a/src/Exceptions/LoaderException.php b/src/Exceptions/LoaderException.php deleted file mode 100644 index 58a53fcde..000000000 --- a/src/Exceptions/LoaderException.php +++ /dev/null @@ -1,31 +0,0 @@ -name = $name; - } -} diff --git a/tests/Lexer/ContextTest.php b/tests/Lexer/ContextTest.php index 363945852..47576ed39 100644 --- a/tests/Lexer/ContextTest.php +++ b/tests/Lexer/ContextTest.php @@ -6,7 +6,6 @@ use PhpMyAdmin\SqlParser\Context; use PhpMyAdmin\SqlParser\Tests\TestCase; -use Throwable; use function class_exists; @@ -114,11 +113,7 @@ public function contextNamesProvider(): array public function testLoadError(): void { - $this->expectExceptionMessage( - 'Specified context ("\PhpMyAdmin\SqlParser\Contexts\ContextFoo") does not exist.' - ); - $this->expectException(Throwable::class); - Context::load('Foo'); + $this->assertFalse(Context::load('Foo')); } /**