Skip to content

Commit

Permalink
Merge pull request #591 from kamil-tekiela/Micro-optimizations
Browse files Browse the repository at this point in the history
Micro optimizations and refactorings
  • Loading branch information
MauricioFauth authored Oct 25, 2024
2 parents 0ff9969 + dbee88a commit 9d4796d
Show file tree
Hide file tree
Showing 37 changed files with 123 additions and 283 deletions.
51 changes: 0 additions & 51 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@
<InvalidPropertyAssignmentValue>
<code><![CDATA[[]]]></code>
</InvalidPropertyAssignmentValue>
<InvalidPropertyFetch>
<code><![CDATA[$context::$keywords]]></code>
</InvalidPropertyFetch>
<MixedAssignment>
<code><![CDATA[self::$keywords]]></code>
</MixedAssignment>
Expand All @@ -155,49 +152,6 @@
<MixedOperand>
<code><![CDATA[$this->str[$this->last]]]></code>
</MixedOperand>
<PossiblyNullArgument>
<code><![CDATA[$this->str[$this->last + 1]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$token]]></code>
<code><![CDATA[$token]]></code>
<code><![CDATA[$token]]></code>
<code><![CDATA[$token]]></code>
<code><![CDATA[$token]]></code>
</PossiblyNullArgument>
<PossiblyNullOperand>
<code><![CDATA[$this->str[$this->last++]]]></code>
<code><![CDATA[$this->str[$this->last++]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$this->str[++$this->last]]]></code>
<code><![CDATA[$this->str[++$this->last]]]></code>
<code><![CDATA[$this->str[++$this->last]]]></code>
<code><![CDATA[$this->str[++$this->last]]]></code>
<code><![CDATA[$this->str[++$this->last]]]></code>
</PossiblyNullOperand>
<PossiblyNullPropertyFetch>
<code><![CDATA[$next->type]]></code>
<code><![CDATA[$next->value]]></code>
Expand All @@ -210,11 +164,6 @@
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $flags]]></code>
<code><![CDATA[! $flags]]></code>
<code><![CDATA[! $flags]]></code>
<code><![CDATA[! $flags]]></code>
<code><![CDATA[Context::isComment($token)]]></code>
<code><![CDATA[Context::isComment($token)]]></code>
<code><![CDATA[Context::isComment($token, $end)]]></code>
<code><![CDATA[empty($delimiter)]]></code>
</RiskyTruthyFalsyComparison>
</file>
Expand Down
139 changes: 48 additions & 91 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\SqlParser\Contexts\ContextMySql50700;

use function array_map;
use function class_exists;
use function explode;
use function in_array;
Expand All @@ -26,7 +27,7 @@
*
* Holds the configuration of the context that is currently used.
*/
abstract class Context
final class Context
{
/**
* The maximum length of a keyword.
Expand Down Expand Up @@ -54,7 +55,7 @@ abstract class Context
* The prefix concatenated to the context name when an incomplete class name
* is specified.
*/
public static string $contextPrefix = 'PhpMyAdmin\\SqlParser\\Contexts\\Context';
private const CONTEXT_PREFIX = 'PhpMyAdmin\\SqlParser\\Contexts\\Context';

/**
* List of keywords.
Expand All @@ -78,10 +79,8 @@ abstract class Context

/**
* List of operators and their flags.
*
* @var array<string, int>
*/
public static array $operators = [
private const OPERATORS = [
// Some operators (*, =) may have ambiguous flags, because they depend on
// the context they are being used in.
// For example: 1. SELECT * FROM table; # SQL specific (wildcard)
Expand Down Expand Up @@ -366,15 +365,18 @@ public static function isKeyword(string $string, bool $isReserved = false): int|
*/
public static function isOperator(string $string): int|null
{
return static::$operators[$string] ?? null;
return self::OPERATORS[$string] ?? null;
}

/**
* Checks if the given character is a whitespace.
*/
public static function isWhitespace(string $string): bool
public static function isWhitespace(string $character): bool
{
return $string === ' ' || $string === "\r" || $string === "\n" || $string === "\t";
return match ($character) {
' ', "\r", "\n", "\t" => true,
default => false,
};
}

/**
Expand All @@ -384,39 +386,20 @@ public static function isWhitespace(string $string): bool
*/
public static function isComment(string $string, bool $end = false): int|null
{
if ($string === '') {
return null;
}

// If comment is Bash style (#):
if (str_starts_with($string, '#')) {
return Token::FLAG_COMMENT_BASH;
}

// If comment is a MySQL command
if (str_starts_with($string, '/*!')) {
return Token::FLAG_COMMENT_MYSQL_CMD;
}

// If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict
// with wildcard and a real opening C style.
// It would look like the following valid SQL statement: "SELECT */* comment */ FROM...".
if (str_starts_with($string, '/*') || str_starts_with($string, '*/')) {
return Token::FLAG_COMMENT_C;
}

// If comment is SQL style (--\s?):
if (
return match (true) {
str_starts_with($string, '#') => Token::FLAG_COMMENT_BASH,
str_starts_with($string, '/*!') => Token::FLAG_COMMENT_MYSQL_CMD,
// If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict
// with wildcard and a real opening C style.
// It would look like the following valid SQL statement: "SELECT */* comment */ FROM...".
str_starts_with($string, '/*') || str_starts_with($string, '*/') => Token::FLAG_COMMENT_C,
str_starts_with($string, '-- ')
|| str_starts_with($string, "--\r")
|| str_starts_with($string, "--\n")
|| str_starts_with($string, "--\t")
|| ($string === '--' && $end)
) {
return Token::FLAG_COMMENT_SQL;
}

return null;
|| str_starts_with($string, "--\r")
|| str_starts_with($string, "--\n")
|| str_starts_with($string, "--\t")
|| ($string === '--' && $end) => Token::FLAG_COMMENT_SQL,
default => null,
};
}

/**
Expand Down Expand Up @@ -445,49 +428,30 @@ public static function isNumber(string $string): bool
*
* @return int|null the appropriate flag for the symbol type
*/
public static function isSymbol(string $string): int|null
public static function isSymbol(string $character): int|null
{
if ($string === '') {
return null;
}

if (str_starts_with($string, '@')) {
return Token::FLAG_SYMBOL_VARIABLE;
}

if (str_starts_with($string, '`')) {
return Token::FLAG_SYMBOL_BACKTICK;
}

if (str_starts_with($string, ':') || str_starts_with($string, '?')) {
return Token::FLAG_SYMBOL_PARAMETER;
}

return null;
return match ($character) {
'@' => Token::FLAG_SYMBOL_VARIABLE,
'`' => Token::FLAG_SYMBOL_BACKTICK,
':', '?' => Token::FLAG_SYMBOL_PARAMETER,
default => null,
};
}

/**
* Checks if the given character is the beginning of a string.
*
* @param string $string string to be checked
* @param string $character a character to be checked
*
* @return int|null the appropriate flag for the string type
*/
public static function isString(string $string): int|null
public static function isString(string $character): int|null
{
if ($string === '') {
return null;
}

if (str_starts_with($string, '\'')) {
return Token::FLAG_STRING_SINGLE_QUOTES;
}

if (str_starts_with($string, '"')) {
return Token::FLAG_STRING_DOUBLE_QUOTES;
}

return null;
return match ($character) {
'\'' => Token::FLAG_STRING_SINGLE_QUOTES,
'"' => Token::FLAG_STRING_DOUBLE_QUOTES,
default => null,
};
}

/**
Expand Down Expand Up @@ -522,22 +486,19 @@ public static function load(string $context = ''): bool
$context = ContextMySql50700::class;
}

if (! class_exists($context)) {
if (! class_exists(self::$contextPrefix . $context)) {
return false;
}

// Could be the fully qualified class name was given, like `ContextDBMS::class`.
if (class_exists('\\' . $context)) {
$context = '\\' . $context;
} else {
// Short context name (must be formatted into class name).
$context = self::$contextPrefix . $context;
$contextClass = $context;
if (! class_exists($contextClass)) {
$contextClass = self::CONTEXT_PREFIX . $context;
if (! class_exists($contextClass)) {
$contextClass = '\\' . $context;
if (! class_exists($contextClass)) {
return false;
}
}
}

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

return true;
}
Expand Down Expand Up @@ -688,11 +649,7 @@ public static function escape(string $str, string $quote = '`'): string
*/
public static function escapeAll(array $strings): array
{
foreach ($strings as $key => $value) {
$strings[$key] = static::escape($value);
}

return $strings;
return array_map(static::escape(...), $strings);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Contexts/ContextMariaDb100000.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PhpMyAdmin\SqlParser\Contexts;

use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Token;

/**
Expand All @@ -15,7 +14,7 @@
*
* @see https://mariadb.com/kb/en/reserved-words/
*/
class ContextMariaDb100000 extends Context
final class ContextMariaDb100000
{
/**
* List of keywords.
Expand All @@ -24,11 +23,10 @@ class ContextMariaDb100000 extends Context
*
* @see Token
*
* @var array<string,int>
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
*/
public static array $keywords = [
public const KEYWORDS = [
'ACTION' => Token::FLAG_KEYWORD,
'AFTER' => Token::FLAG_KEYWORD,
'AGGREGATE' => Token::FLAG_KEYWORD,
Expand Down
6 changes: 2 additions & 4 deletions src/Contexts/ContextMariaDb100100.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PhpMyAdmin\SqlParser\Contexts;

use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Token;

/**
Expand All @@ -15,7 +14,7 @@
*
* @see https://mariadb.com/kb/en/reserved-words/
*/
class ContextMariaDb100100 extends Context
final class ContextMariaDb100100
{
/**
* List of keywords.
Expand All @@ -24,11 +23,10 @@ class ContextMariaDb100100 extends Context
*
* @see Token
*
* @var array<string,int>
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
*/
public static array $keywords = [
public const KEYWORDS = [
'ACCOUNT' => Token::FLAG_KEYWORD,
'ACTION' => Token::FLAG_KEYWORD,
'AFTER' => Token::FLAG_KEYWORD,
Expand Down
6 changes: 2 additions & 4 deletions src/Contexts/ContextMariaDb100200.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PhpMyAdmin\SqlParser\Contexts;

use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Token;

/**
Expand All @@ -15,7 +14,7 @@
*
* @see https://mariadb.com/kb/en/reserved-words/
*/
class ContextMariaDb100200 extends Context
final class ContextMariaDb100200
{
/**
* List of keywords.
Expand All @@ -24,11 +23,10 @@ class ContextMariaDb100200 extends Context
*
* @see Token
*
* @var array<string,int>
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
*/
public static array $keywords = [
public const KEYWORDS = [
'ACCOUNT' => Token::FLAG_KEYWORD,
'ACTION' => Token::FLAG_KEYWORD,
'AFTER' => Token::FLAG_KEYWORD,
Expand Down
Loading

0 comments on commit 9d4796d

Please sign in to comment.