Skip to content

Commit e840d83

Browse files
committed
Change error handling to returning a boolean value
In case an error occurs during loading of a context, let the Context::load() method return a boolean value indicating whether the context was successfully loaded or not. Throwing an Exception causes a false positive in debuggers,since the Context::load() method does the correct thing anyway
1 parent 0f4f8de commit e840d83

2 files changed

Lines changed: 19 additions & 54 deletions

File tree

src/Context.php

Lines changed: 19 additions & 23 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 intval;
@@ -588,11 +586,9 @@ public static function isSeparator($str)
588586
*
589587
* @param string $context name of the context or full class name that defines the context
590588
*
591-
* @return void
592-
*
593-
* @throws LoaderException if the specified context doesn't exist.
589+
* @return bool true if the context was loaded, false otherwise
594590
*/
595-
public static function load($context = '')
591+
public static function load($context = ''): bool
596592
{
597593
if (empty($context)) {
598594
$context = self::$defaultContext;
@@ -604,11 +600,13 @@ public static function load($context = '')
604600
}
605601

606602
if (! class_exists($context)) {
607-
throw @new LoaderException('Specified context ("' . $context . '") does not exist.', $context);
603+
return false;
608604
}
609605

610606
self::$loadedContext = $context;
611607
self::$KEYWORDS = $context::$KEYWORDS;
608+
609+
return true;
612610
}
613611

614612
/**
@@ -628,24 +626,22 @@ public static function loadClosest($context = '')
628626
{
629627
$length = strlen($context);
630628
for ($i = $length; $i > 0;) {
631-
try {
632-
/* Trying to load the new context */
633-
static::load($context);
634-
629+
/* Trying to load the new context */
630+
if (static::load($context)) {
635631
return $context;
636-
} catch (LoaderException $e) {
637-
/* Replace last two non zero digits by zeroes */
638-
do {
639-
$i -= 2;
640-
$part = substr($context, $i, 2);
641-
/* No more numeric parts to strip */
642-
if (! is_numeric($part)) {
643-
break 2;
644-
}
645-
} while (intval($part) === 0 && $i > 0);
646-
647-
$context = substr($context, 0, $i) . '00' . substr($context, $i + 2);
648632
}
633+
634+
/* Replace last two non zero digits by zeroes */
635+
do {
636+
$i -= 2;
637+
$part = substr($context, $i, 2);
638+
/* No more numeric parts to strip */
639+
if (! is_numeric($part)) {
640+
break 2;
641+
}
642+
} while (intval($part) === 0 && $i > 0);
643+
644+
$context = substr($context, 0, $i) . '00' . substr($context, $i + 2);
649645
}
650646

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

src/Exceptions/LoaderException.php

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

0 commit comments

Comments
 (0)