Skip to content

Commit dbee88a

Browse files
committed
Context should not be directly loadable
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 3dcaadb commit dbee88a

33 files changed

+74
-142
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@
156156
<InvalidPropertyAssignmentValue>
157157
<code><![CDATA[[]]]></code>
158158
</InvalidPropertyAssignmentValue>
159-
<InvalidPropertyFetch>
160-
<code><![CDATA[$context::$keywords]]></code>
161-
</InvalidPropertyFetch>
162159
<MixedAssignment>
163160
<code><![CDATA[self::$keywords]]></code>
164161
</MixedAssignment>

src/Context.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* Holds the configuration of the context that is currently used.
2929
*/
30-
abstract class Context
30+
final class Context
3131
{
3232
/**
3333
* The maximum length of a keyword.
@@ -55,7 +55,7 @@ abstract class Context
5555
* The prefix concatenated to the context name when an incomplete class name
5656
* is specified.
5757
*/
58-
public static string $contextPrefix = 'PhpMyAdmin\\SqlParser\\Contexts\\Context';
58+
private const CONTEXT_PREFIX = 'PhpMyAdmin\\SqlParser\\Contexts\\Context';
5959

6060
/**
6161
* List of keywords.
@@ -486,22 +486,19 @@ public static function load(string $context = ''): bool
486486
$context = ContextMySql50700::class;
487487
}
488488

489-
if (! class_exists($context)) {
490-
if (! class_exists(self::$contextPrefix . $context)) {
491-
return false;
492-
}
493-
494-
// Could be the fully qualified class name was given, like `ContextDBMS::class`.
495-
if (class_exists('\\' . $context)) {
496-
$context = '\\' . $context;
497-
} else {
498-
// Short context name (must be formatted into class name).
499-
$context = self::$contextPrefix . $context;
489+
$contextClass = $context;
490+
if (! class_exists($contextClass)) {
491+
$contextClass = self::CONTEXT_PREFIX . $context;
492+
if (! class_exists($contextClass)) {
493+
$contextClass = '\\' . $context;
494+
if (! class_exists($contextClass)) {
495+
return false;
496+
}
500497
}
501498
}
502499

503-
self::$loadedContext = $context;
504-
self::$keywords = $context::$keywords;
500+
self::$loadedContext = $contextClass;
501+
self::$keywords = $contextClass::KEYWORDS;
505502

506503
return true;
507504
}

src/Contexts/ContextMariaDb100000.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100000 extends Context
17+
final class ContextMariaDb100000
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100000 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACTION' => Token::FLAG_KEYWORD,
3331
'AFTER' => Token::FLAG_KEYWORD,
3432
'AGGREGATE' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100100.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100100 extends Context
17+
final class ContextMariaDb100100
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100100 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100200.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100200 extends Context
17+
final class ContextMariaDb100200
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100200 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100300.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100300 extends Context
17+
final class ContextMariaDb100300
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100300 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100400.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100400 extends Context
17+
final class ContextMariaDb100400
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100400 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100500.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100500 extends Context
17+
final class ContextMariaDb100500
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100500 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100600.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100600 extends Context
17+
final class ContextMariaDb100600
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100600 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100700.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100700 extends Context
17+
final class ContextMariaDb100700
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100700 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100800.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100800 extends Context
17+
final class ContextMariaDb100800
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100800 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb100900.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb100900 extends Context
17+
final class ContextMariaDb100900
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb100900 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

src/Contexts/ContextMariaDb101000.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpMyAdmin\SqlParser\Contexts;
66

7-
use PhpMyAdmin\SqlParser\Context;
87
use PhpMyAdmin\SqlParser\Token;
98

109
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @see https://mariadb.com/kb/en/reserved-words/
1716
*/
18-
class ContextMariaDb101000 extends Context
17+
final class ContextMariaDb101000
1918
{
2019
/**
2120
* List of keywords.
@@ -24,11 +23,10 @@ class ContextMariaDb101000 extends Context
2423
*
2524
* @see Token
2625
*
27-
* @var array<string,int>
2826
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
2927
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3028
*/
31-
public static array $keywords = [
29+
public const KEYWORDS = [
3230
'ACCOUNT' => Token::FLAG_KEYWORD,
3331
'ACTION' => Token::FLAG_KEYWORD,
3432
'AFTER' => Token::FLAG_KEYWORD,

0 commit comments

Comments
 (0)