Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 19 additions & 21 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace PhpMyAdmin\SqlParser;

use PhpMyAdmin\SqlParser\Exceptions\LoaderException;

use function class_exists;
use function explode;
use function in_array;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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 */
Expand Down
31 changes: 0 additions & 31 deletions src/Exceptions/LoaderException.php

This file was deleted.

7 changes: 1 addition & 6 deletions tests/Lexer/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Tests\TestCase;
use Throwable;

use function class_exists;

Expand Down Expand Up @@ -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'));
}

/**
Expand Down