6
6
7
7
use PhpMyAdmin \SqlParser \Contexts \ContextMySql50700 ;
8
8
9
+ use function array_map ;
9
10
use function class_exists ;
10
11
use function explode ;
11
12
use function in_array ;
26
27
*
27
28
* Holds the configuration of the context that is currently used.
28
29
*/
29
- abstract class Context
30
+ final class Context
30
31
{
31
32
/**
32
33
* The maximum length of a keyword.
@@ -54,7 +55,7 @@ abstract class Context
54
55
* The prefix concatenated to the context name when an incomplete class name
55
56
* is specified.
56
57
*/
57
- public static string $ contextPrefix = 'PhpMyAdmin \\SqlParser \\Contexts \\Context ' ;
58
+ private const CONTEXT_PREFIX = 'PhpMyAdmin \\SqlParser \\Contexts \\Context ' ;
58
59
59
60
/**
60
61
* List of keywords.
@@ -78,10 +79,8 @@ abstract class Context
78
79
79
80
/**
80
81
* List of operators and their flags.
81
- *
82
- * @var array<string, int>
83
82
*/
84
- public static array $ operators = [
83
+ private const OPERATORS = [
85
84
// Some operators (*, =) may have ambiguous flags, because they depend on
86
85
// the context they are being used in.
87
86
// For example: 1. SELECT * FROM table; # SQL specific (wildcard)
@@ -366,15 +365,18 @@ public static function isKeyword(string $string, bool $isReserved = false): int|
366
365
*/
367
366
public static function isOperator (string $ string ): int |null
368
367
{
369
- return static :: $ operators [$ string ] ?? null ;
368
+ return self :: OPERATORS [$ string ] ?? null ;
370
369
}
371
370
372
371
/**
373
372
* Checks if the given character is a whitespace.
374
373
*/
375
- public static function isWhitespace (string $ string ): bool
374
+ public static function isWhitespace (string $ character ): bool
376
375
{
377
- return $ string === ' ' || $ string === "\r" || $ string === "\n" || $ string === "\t" ;
376
+ return match ($ character ) {
377
+ ' ' , "\r" , "\n" , "\t" => true ,
378
+ default => false ,
379
+ };
378
380
}
379
381
380
382
/**
@@ -384,39 +386,20 @@ public static function isWhitespace(string $string): bool
384
386
*/
385
387
public static function isComment (string $ string , bool $ end = false ): int |null
386
388
{
387
- if ($ string === '' ) {
388
- return null ;
389
- }
390
-
391
- // If comment is Bash style (#):
392
- if (str_starts_with ($ string , '# ' )) {
393
- return Token::FLAG_COMMENT_BASH ;
394
- }
395
-
396
- // If comment is a MySQL command
397
- if (str_starts_with ($ string , '/*! ' )) {
398
- return Token::FLAG_COMMENT_MYSQL_CMD ;
399
- }
400
-
401
- // If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict
402
- // with wildcard and a real opening C style.
403
- // It would look like the following valid SQL statement: "SELECT */* comment */ FROM...".
404
- if (str_starts_with ($ string , '/* ' ) || str_starts_with ($ string , '*/ ' )) {
405
- return Token::FLAG_COMMENT_C ;
406
- }
407
-
408
- // If comment is SQL style (--\s?):
409
- if (
389
+ return match (true ) {
390
+ str_starts_with ($ string , '# ' ) => Token::FLAG_COMMENT_BASH ,
391
+ str_starts_with ($ string , '/*! ' ) => Token::FLAG_COMMENT_MYSQL_CMD ,
392
+ // If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict
393
+ // with wildcard and a real opening C style.
394
+ // It would look like the following valid SQL statement: "SELECT */* comment */ FROM...".
395
+ str_starts_with ($ string , '/* ' ) || str_starts_with ($ string , '*/ ' ) => Token::FLAG_COMMENT_C ,
410
396
str_starts_with ($ string , '-- ' )
411
- || str_starts_with ($ string , "-- \r" )
412
- || str_starts_with ($ string , "-- \n" )
413
- || str_starts_with ($ string , "-- \t" )
414
- || ($ string === '-- ' && $ end )
415
- ) {
416
- return Token::FLAG_COMMENT_SQL ;
417
- }
418
-
419
- return null ;
397
+ || str_starts_with ($ string , "-- \r" )
398
+ || str_starts_with ($ string , "-- \n" )
399
+ || str_starts_with ($ string , "-- \t" )
400
+ || ($ string === '-- ' && $ end ) => Token::FLAG_COMMENT_SQL ,
401
+ default => null ,
402
+ };
420
403
}
421
404
422
405
/**
@@ -445,49 +428,30 @@ public static function isNumber(string $string): bool
445
428
*
446
429
* @return int|null the appropriate flag for the symbol type
447
430
*/
448
- public static function isSymbol (string $ string ): int |null
431
+ public static function isSymbol (string $ character ): int |null
449
432
{
450
- if ($ string === '' ) {
451
- return null ;
452
- }
453
-
454
- if (str_starts_with ($ string , '@ ' )) {
455
- return Token::FLAG_SYMBOL_VARIABLE ;
456
- }
457
-
458
- if (str_starts_with ($ string , '` ' )) {
459
- return Token::FLAG_SYMBOL_BACKTICK ;
460
- }
461
-
462
- if (str_starts_with ($ string , ': ' ) || str_starts_with ($ string , '? ' )) {
463
- return Token::FLAG_SYMBOL_PARAMETER ;
464
- }
465
-
466
- return null ;
433
+ return match ($ character ) {
434
+ '@ ' => Token::FLAG_SYMBOL_VARIABLE ,
435
+ '` ' => Token::FLAG_SYMBOL_BACKTICK ,
436
+ ': ' , '? ' => Token::FLAG_SYMBOL_PARAMETER ,
437
+ default => null ,
438
+ };
467
439
}
468
440
469
441
/**
470
442
* Checks if the given character is the beginning of a string.
471
443
*
472
- * @param string $string string to be checked
444
+ * @param string $character a character to be checked
473
445
*
474
446
* @return int|null the appropriate flag for the string type
475
447
*/
476
- public static function isString (string $ string ): int |null
448
+ public static function isString (string $ character ): int |null
477
449
{
478
- if ($ string === '' ) {
479
- return null ;
480
- }
481
-
482
- if (str_starts_with ($ string , '\'' )) {
483
- return Token::FLAG_STRING_SINGLE_QUOTES ;
484
- }
485
-
486
- if (str_starts_with ($ string , '" ' )) {
487
- return Token::FLAG_STRING_DOUBLE_QUOTES ;
488
- }
489
-
490
- return null ;
450
+ return match ($ character ) {
451
+ '\'' => Token::FLAG_STRING_SINGLE_QUOTES ,
452
+ '" ' => Token::FLAG_STRING_DOUBLE_QUOTES ,
453
+ default => null ,
454
+ };
491
455
}
492
456
493
457
/**
@@ -522,22 +486,19 @@ public static function load(string $context = ''): bool
522
486
$ context = ContextMySql50700::class;
523
487
}
524
488
525
- if (! class_exists ($ context )) {
526
- if (! class_exists (self ::$ contextPrefix . $ context )) {
527
- return false ;
528
- }
529
-
530
- // Could be the fully qualified class name was given, like `ContextDBMS::class`.
531
- if (class_exists ('\\' . $ context )) {
532
- $ context = '\\' . $ context ;
533
- } else {
534
- // Short context name (must be formatted into class name).
535
- $ 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
+ }
536
497
}
537
498
}
538
499
539
- self ::$ loadedContext = $ context ;
540
- self ::$ keywords = $ context :: $ keywords ;
500
+ self ::$ loadedContext = $ contextClass ;
501
+ self ::$ keywords = $ contextClass :: KEYWORDS ;
541
502
542
503
return true ;
543
504
}
@@ -688,11 +649,7 @@ public static function escape(string $str, string $quote = '`'): string
688
649
*/
689
650
public static function escapeAll (array $ strings ): array
690
651
{
691
- foreach ($ strings as $ key => $ value ) {
692
- $ strings [$ key ] = static ::escape ($ value );
693
- }
694
-
695
- return $ strings ;
652
+ return array_map (static ::escape (...), $ strings );
696
653
}
697
654
698
655
/**
0 commit comments