From 406636fb66b4756a0f4517f41ba99d2914974602 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Thu, 31 Aug 2023 00:37:52 +0100 Subject: [PATCH] Remove class Tokens It was not used anywhere in PMA project. It's very likely broken. It has serious feature envy on TokensList. The two methods had very unclear purpose and behaviour. Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 25 ------ psalm-baseline.xml | 20 ----- src/Lexer.php | 15 ---- src/Utils/Tokens.php | 151 ------------------------------------- tests/Utils/TokensTest.php | 121 ----------------------------- 5 files changed, 332 deletions(-) delete mode 100644 src/Utils/Tokens.php delete mode 100644 tests/Utils/TokensTest.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index af8f22d78..5c7198b21 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -820,26 +820,6 @@ parameters: count: 2 path: src/Utils/Table.php - - - message: "#^Binary operation \"&\" between int\\|string and int results in an error\\.$#" - count: 1 - path: src/Utils/Tokens.php - - - - message: "#^Cannot cast mixed to string\\.$#" - count: 1 - path: src/Utils/Tokens.php - - - - message: "#^Parameter \\#1 \\$string1 of function strcasecmp expects string, int\\|string given\\.$#" - count: 1 - path: src/Utils/Tokens.php - - - - message: "#^Parameter \\#2 \\$pattern of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Tokens\\:\\:match\\(\\) expects array\\, PhpMyAdmin\\\\SqlParser\\\\Token given\\.$#" - count: 1 - path: src/Utils/Tokens.php - - message: "#^Cannot call method __toString\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" count: 2 @@ -950,11 +930,6 @@ parameters: count: 1 path: tests/Utils/TableTest.php - - - message: "#^Parameter \\#2 \\$find of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Tokens\\:\\:replaceTokens\\(\\) expects array\\, array\\\\> given\\.$#" - count: 1 - path: tests/Utils/TokensTest.php - - message: "#^Expression \"\\$str1\\[\\$i\\]\" on a separate line does not do anything\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index a0c58ab81..0b22645b9 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1410,17 +1410,6 @@ has - - - $find[$k] - - - - - - - - provideBuilderForRenameColumn @@ -1859,15 +1848,6 @@ getForeignKeysProvider - - - $find - - - matchProvider - replaceTokensProvider - - $testContents diff --git a/src/Lexer.php b/src/Lexer.php index b8ced2c0f..3e14ae4ad 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -151,21 +151,6 @@ class Lexer extends Core */ public $delimiterLen; - /** - * Gets the tokens list parsed by a new instance of a lexer. - * - * @param string|UtfString $str the query to be lexed - * @param bool $strict whether strict mode should be - * enabled or not - * @param string $delimiter the delimiter to be used - */ - public static function getTokens($str, $strict = false, $delimiter = null): TokensList - { - $lexer = new self($str, $strict, $delimiter); - - return $lexer->list; - } - /** * @param string|UtfString $str the query to be lexed * @param bool $strict whether strict mode should be diff --git a/src/Utils/Tokens.php b/src/Utils/Tokens.php deleted file mode 100644 index 9c6d4c37d..000000000 --- a/src/Utils/Tokens.php +++ /dev/null @@ -1,151 +0,0 @@ - $pattern the pattern to be matches - */ - public static function match(Token $token, array $pattern): bool - { - // Token. - if (isset($pattern['token']) && ($pattern['token'] !== $token->token)) { - return false; - } - - // Value. - if (isset($pattern['value']) && ($pattern['value'] !== $token->value)) { - return false; - } - - if (isset($pattern['value_str']) && strcasecmp($pattern['value_str'], (string) $token->value)) { - return false; - } - - // Type. - if (isset($pattern['type']) && ($pattern['type'] !== $token->type)) { - return false; - } - - // Flags. - return ! isset($pattern['flags']) - || (! (($pattern['flags'] & $token->flags) === 0)); - } - - /** - * @param Token[] $find - * @param Token[] $replace - */ - public static function replaceTokens( - TokensList|string|UtfString $list, - array $find, - array $replace - ): TokensList|string { - /** - * Whether the first parameter is a list. - */ - $isList = $list instanceof TokensList; - - // Parsing the tokens. - if (! $list instanceof TokensList) { - $list = Lexer::getTokens($list); - } - - /** - * The list to be returned. - */ - $newList = new TokensList(); - - /** - * The length of the find pattern is calculated only once. - * - * @var int - */ - $findCount = count($find); - - /** - * The starting index of the pattern. - * - * @var int - */ - $i = 0; - - while ($i < $list->count) { - // A sequence may not start with a comment. - if ($list->tokens[$i]->type === Token::TYPE_COMMENT) { - $newList[] = $list->tokens[$i]; - ++$i; - continue; - } - - /** - * The index used to parse `$list->tokens`. - * - * This index might be running faster than `$k` because some tokens - * are skipped. - */ - $j = $i; - - /** - * The index used to parse `$find`. - * - * This index might be running slower than `$j` because some tokens - * are skipped. - * - * @var int - */ - $k = 0; - - // Checking if the next tokens match the pattern described. - while (($j < $list->count) && ($k < $findCount)) { - // Comments are being skipped. - if ($list->tokens[$j]->type === Token::TYPE_COMMENT) { - ++$j; - } - - if (! static::match($list->tokens[$j], $find[$k])) { - // This token does not match the pattern. - break; - } - - // Going to next token and segment of find pattern. - ++$j; - ++$k; - } - - // Checking if the sequence was found. - if ($k === $findCount) { - // Inserting new tokens. - foreach ($replace as $token) { - $newList[] = $token; - } - - // Skipping next `$findCount` tokens. - $i = $j; - } else { - // Adding the same token. - $newList[] = $list->tokens[$i]; - ++$i; - } - } - - return $isList ? $newList : $newList->build(); - } -} diff --git a/tests/Utils/TokensTest.php b/tests/Utils/TokensTest.php deleted file mode 100644 index 6246ddc70..000000000 --- a/tests/Utils/TokensTest.php +++ /dev/null @@ -1,121 +0,0 @@ -[] $find - * @param Token[] $replace - */ - #[DataProvider('replaceTokensProvider')] - public function testReplaceTokens(string $list, array $find, array $replace, string $expected): void - { - $this->assertEquals($expected, Tokens::replaceTokens($list, $find, $replace)); - } - - /** - * @return array[]|Token[]>> - * @psalm-return list>, Token[], string}> - */ - public static function replaceTokensProvider(): array - { - return [ - [ - 'SELECT * FROM /*x*/a/*c*/.b', - [ - ['value_str' => 'a'], - ['token' => '.'], - ], - [ - new Token('c'), - new Token('.'), - ], - 'SELECT * FROM /*x*/c.b', - ], - ]; - } - - /** - * @param array $pattern - */ - #[DataProvider('matchProvider')] - public function testMatch(Token $token, array $pattern, bool $expected): void - { - $this->assertSame($expected, Tokens::match($token, $pattern)); - } - - /** - * @return array>> - * @psalm-return list, bool}> - */ - public static function matchProvider(): array - { - return [ - [ - new Token(''), - [], - true, - ], - - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['token' => '"abc"'], - true, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['value' => 'abc'], - true, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['value_str' => 'ABC'], - true, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['type' => Token::TYPE_STRING], - true, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['flags' => Token::FLAG_STRING_DOUBLE_QUOTES], - true, - ], - - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['token' => '"abcd"'], - false, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['value' => 'abcd'], - false, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['value_str' => 'ABCd'], - false, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['type' => Token::TYPE_NUMBER], - false, - ], - [ - new Token('"abc"', Token::TYPE_STRING, Token::FLAG_STRING_DOUBLE_QUOTES), - ['flags' => Token::FLAG_STRING_SINGLE_QUOTES], - false, - ], - ]; - } -}