Skip to content

Commit

Permalink
Refactor Context::load()
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <[email protected]>
  • Loading branch information
kamil-tekiela committed Jan 15, 2024
1 parent 3247f64 commit a764d81
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,6 @@
<code>ContextMySql50600</code>
</UnusedClass>
</file>
<file src="src/Contexts/ContextMySql50700.php">
<UnusedClass>
<code>ContextMySql50700</code>
</UnusedClass>
</file>
<file src="src/Contexts/ContextMySql80000.php">
<UnusedClass>
<code>ContextMySql80000</code>
Expand Down
25 changes: 11 additions & 14 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpMyAdmin\SqlParser;

use PhpMyAdmin\SqlParser\Contexts\ContextMySql50700;

use function class_exists;
use function explode;
use function in_array;
Expand Down Expand Up @@ -43,21 +45,16 @@ abstract class Context
*/
public const OPERATOR_MAX_LENGTH = 4;

/**
* The name of the default content.
*/
public static string $defaultContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700';

/**
* The name of the loaded context.
*/
public static string $loadedContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700';
public static string $loadedContext = ContextMySql50700::class;

/**
* The prefix concatenated to the context name when an incomplete class name
* is specified.
*/
public static string $contextPrefix = '\\PhpMyAdmin\\SqlParser\\Contexts\\Context';
public static string $contextPrefix = 'PhpMyAdmin\\SqlParser\\Contexts\\Context';

/**
* List of keywords.
Expand Down Expand Up @@ -521,19 +518,19 @@ public static function isSeparator(string $string): bool
*/
public static function load(string $context = ''): bool
{
if (empty($context)) {
$context = self::$defaultContext;
if ($context === '') {
$context = ContextMySql50700::class;
}

if ($context[0] !== '\\') {
if (! class_exists($context)) {
if (! class_exists(self::$contextPrefix . $context)) {
return false;
}

// Short context name (must be formatted into class name).
$context = self::$contextPrefix . $context;
}

if (! class_exists($context)) {
return false;
}

self::$loadedContext = $context;
self::$keywords = $context::$keywords;

Expand Down
2 changes: 1 addition & 1 deletion src/Tools/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static function build(
Context::load('MariaDb' . $mariaDbVersion);
} else {
// Load the default context to be sure there is no side effects
Context::load('');
Context::load();

Check warning on line 172 in src/Tools/TestGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/Tools/TestGenerator.php#L172

Added line #L172 was not covered by tests
}

$test = static::generate($query, $type);
Expand Down
13 changes: 6 additions & 7 deletions tests/Lexer/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ public static function tearDownAfterClass(): void
public function testLoad(): void
{
// Default context is 5.7.0.
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$loadedContext);
$this->assertEquals('PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$loadedContext);
$this->assertArrayHasKey('STORED', Context::$keywords);
$this->assertArrayNotHasKey('AUTHORS', Context::$keywords);

// Restoring context.
Context::load('');
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$defaultContext);
Context::load();
$this->assertArrayHasKey('STORED', Context::$keywords);
$this->assertArrayNotHasKey('AUTHORS', Context::$keywords);
}
Expand All @@ -39,12 +38,12 @@ public function testLoadClosest(string $context, string|null $expected): void
{
$this->assertEquals($expected, Context::loadClosest($context));
if ($expected !== null) {
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\Context' . $expected, Context::$loadedContext);
$this->assertEquals('PhpMyAdmin\\SqlParser\\Contexts\\Context' . $expected, Context::$loadedContext);
$this->assertTrue(class_exists(Context::$loadedContext));
}

// Restoring context.
Context::load('');
Context::load();
}

/**
Expand Down Expand Up @@ -89,10 +88,10 @@ public static function contextLoadingProvider(): array
public function testLoadAll(string $context): void
{
Context::load($context);
$this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\Context' . $context, Context::$loadedContext);
$this->assertEquals('PhpMyAdmin\\SqlParser\\Contexts\\Context' . $context, Context::$loadedContext);

// Restoring context.
Context::load('');
Context::load();
}

/** @return string[][] */
Expand Down

0 comments on commit a764d81

Please sign in to comment.