Skip to content

Commit a764d81

Browse files
committed
Refactor Context::load()
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 3247f64 commit a764d81

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,6 @@
546546
<code>ContextMySql50600</code>
547547
</UnusedClass>
548548
</file>
549-
<file src="src/Contexts/ContextMySql50700.php">
550-
<UnusedClass>
551-
<code>ContextMySql50700</code>
552-
</UnusedClass>
553-
</file>
554549
<file src="src/Contexts/ContextMySql80000.php">
555550
<UnusedClass>
556551
<code>ContextMySql80000</code>

src/Context.php

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

55
namespace PhpMyAdmin\SqlParser;
66

7+
use PhpMyAdmin\SqlParser\Contexts\ContextMySql50700;
8+
79
use function class_exists;
810
use function explode;
911
use function in_array;
@@ -43,21 +45,16 @@ abstract class Context
4345
*/
4446
public const OPERATOR_MAX_LENGTH = 4;
4547

46-
/**
47-
* The name of the default content.
48-
*/
49-
public static string $defaultContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700';
50-
5148
/**
5249
* The name of the loaded context.
5350
*/
54-
public static string $loadedContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700';
51+
public static string $loadedContext = ContextMySql50700::class;
5552

5653
/**
5754
* The prefix concatenated to the context name when an incomplete class name
5855
* is specified.
5956
*/
60-
public static string $contextPrefix = '\\PhpMyAdmin\\SqlParser\\Contexts\\Context';
57+
public static string $contextPrefix = 'PhpMyAdmin\\SqlParser\\Contexts\\Context';
6158

6259
/**
6360
* List of keywords.
@@ -521,19 +518,19 @@ public static function isSeparator(string $string): bool
521518
*/
522519
public static function load(string $context = ''): bool
523520
{
524-
if (empty($context)) {
525-
$context = self::$defaultContext;
521+
if ($context === '') {
522+
$context = ContextMySql50700::class;
526523
}
527524

528-
if ($context[0] !== '\\') {
525+
if (! class_exists($context)) {
526+
if (! class_exists(self::$contextPrefix . $context)) {
527+
return false;
528+
}
529+
529530
// Short context name (must be formatted into class name).
530531
$context = self::$contextPrefix . $context;
531532
}
532533

533-
if (! class_exists($context)) {
534-
return false;
535-
}
536-
537534
self::$loadedContext = $context;
538535
self::$keywords = $context::$keywords;
539536

src/Tools/TestGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static function build(
169169
Context::load('MariaDb' . $mariaDbVersion);
170170
} else {
171171
// Load the default context to be sure there is no side effects
172-
Context::load('');
172+
Context::load();
173173
}
174174

175175
$test = static::generate($query, $type);

tests/Lexer/ContextTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ public static function tearDownAfterClass(): void
2020
public function testLoad(): void
2121
{
2222
// Default context is 5.7.0.
23-
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$loadedContext);
23+
$this->assertEquals('PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$loadedContext);
2424
$this->assertArrayHasKey('STORED', Context::$keywords);
2525
$this->assertArrayNotHasKey('AUTHORS', Context::$keywords);
2626

2727
// Restoring context.
28-
Context::load('');
29-
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$defaultContext);
28+
Context::load();
3029
$this->assertArrayHasKey('STORED', Context::$keywords);
3130
$this->assertArrayNotHasKey('AUTHORS', Context::$keywords);
3231
}
@@ -39,12 +38,12 @@ public function testLoadClosest(string $context, string|null $expected): void
3938
{
4039
$this->assertEquals($expected, Context::loadClosest($context));
4140
if ($expected !== null) {
42-
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\Context' . $expected, Context::$loadedContext);
41+
$this->assertEquals('PhpMyAdmin\\SqlParser\\Contexts\\Context' . $expected, Context::$loadedContext);
4342
$this->assertTrue(class_exists(Context::$loadedContext));
4443
}
4544

4645
// Restoring context.
47-
Context::load('');
46+
Context::load();
4847
}
4948

5049
/**
@@ -89,10 +88,10 @@ public static function contextLoadingProvider(): array
8988
public function testLoadAll(string $context): void
9089
{
9190
Context::load($context);
92-
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\Context' . $context, Context::$loadedContext);
91+
$this->assertEquals('PhpMyAdmin\\SqlParser\\Contexts\\Context' . $context, Context::$loadedContext);
9392

9493
// Restoring context.
95-
Context::load('');
94+
Context::load();
9695
}
9796

9897
/** @return string[][] */

0 commit comments

Comments
 (0)