Skip to content

Commit feb6a86

Browse files
committed
Merge #384 - Change error handling from throwing an exception to returning a boolean
Pull-request: #384 Signed-off-by: William Desportes <[email protected]>
2 parents 9b6be66 + 233788b commit feb6a86

File tree

4 files changed

+21
-58
lines changed

4 files changed

+21
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Support table structure with `COMPRESSED` columns (#351)
1111
* Add `#[\AllowDynamicProperties]` on `Statement` and `Expression` classes for PHP 8.2 support
1212
* Support `ALTER` queries of `PARTITIONS` (#329)
13+
* Change `Context::load()` error handling to returning a boolean value instead of throwing a `LoaderException` (#384)
1314

1415
## [5.5.0] - 2021-12-08
1516

src/Context.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser;
66

7-
use PhpMyAdmin\SqlParser\Exceptions\LoaderException;
8-
97
use function class_exists;
108
use function explode;
119
use function in_array;
@@ -526,9 +524,9 @@ public static function isSeparator(string $string): bool
526524
*
527525
* @param string $context name of the context or full class name that defines the context
528526
*
529-
* @throws LoaderException if the specified context doesn't exist.
527+
* @return bool true if the context was loaded, false otherwise
530528
*/
531-
public static function load(string $context = ''): void
529+
public static function load(string $context = ''): bool
532530
{
533531
if (empty($context)) {
534532
$context = self::$defaultContext;
@@ -540,11 +538,13 @@ public static function load(string $context = ''): void
540538
}
541539

542540
if (! class_exists($context)) {
543-
throw @new LoaderException('Specified context ("' . $context . '") does not exist.', $context);
541+
return false;
544542
}
545543

546544
self::$loadedContext = $context;
547545
self::$keywords = $context::$keywords;
546+
547+
return true;
548548
}
549549

550550
/**
@@ -563,24 +563,22 @@ public static function loadClosest(string $context = ''): ?string
563563
{
564564
$length = strlen($context);
565565
for ($i = $length; $i > 0;) {
566-
try {
567-
/* Trying to load the new context */
568-
static::load($context);
569-
566+
/* Trying to load the new context */
567+
if (static::load($context)) {
570568
return $context;
571-
} catch (LoaderException $e) {
572-
/* Replace last two non zero digits by zeroes */
573-
do {
574-
$i -= 2;
575-
$part = substr($context, $i, 2);
576-
/* No more numeric parts to strip */
577-
if (! is_numeric($part)) {
578-
break 2;
579-
}
580-
} while (intval($part) === 0 && $i > 0);
581-
582-
$context = substr($context, 0, $i) . '00' . substr($context, $i + 2);
583569
}
570+
571+
/* Replace last two non zero digits by zeroes */
572+
do {
573+
$i -= 2;
574+
$part = substr($context, $i, 2);
575+
/* No more numeric parts to strip */
576+
if (! is_numeric($part)) {
577+
break 2;
578+
}
579+
} while (intval($part) === 0 && $i > 0);
580+
581+
$context = substr($context, 0, $i) . '00' . substr($context, $i + 2);
584582
}
585583

586584
/* Fallback to loading at least matching engine */

src/Exceptions/LoaderException.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/Lexer/ContextTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpMyAdmin\SqlParser\Context;
88
use PhpMyAdmin\SqlParser\Tests\TestCase;
9-
use Throwable;
109

1110
use function class_exists;
1211

@@ -114,11 +113,7 @@ public function contextNamesProvider(): array
114113

115114
public function testLoadError(): void
116115
{
117-
$this->expectExceptionMessage(
118-
'Specified context ("\PhpMyAdmin\SqlParser\Contexts\ContextFoo") does not exist.'
119-
);
120-
$this->expectException(Throwable::class);
121-
Context::load('Foo');
116+
$this->assertFalse(Context::load('Foo'));
122117
}
123118

124119
/**

0 commit comments

Comments
 (0)