Skip to content

Commit 9be1871

Browse files
annexus254williamdes
authored andcommitted
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 9b6be66 commit 9be1871

2 files changed

Lines changed: 19 additions & 52 deletions

File tree

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.

0 commit comments

Comments
 (0)