From 4a2d8618e4d13faab3c13b9e6cf3a8105c2218a7 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 31 May 2023 19:56:12 +0100 Subject: [PATCH 1/2] Remove unused Misc class Signed-off-by: Kamil Tekiela --- src/Utils/Misc.php | 98 ---------------------------- tests/Utils/MiscTest.php | 137 --------------------------------------- 2 files changed, 235 deletions(-) delete mode 100644 src/Utils/Misc.php delete mode 100644 tests/Utils/MiscTest.php diff --git a/src/Utils/Misc.php b/src/Utils/Misc.php deleted file mode 100644 index f4e0bcd83..000000000 --- a/src/Utils/Misc.php +++ /dev/null @@ -1,98 +0,0 @@ -|string|null>>|null>> - */ - public static function getAliases($statement, $database) - { - if (! ($statement instanceof SelectStatement) || empty($statement->expr) || empty($statement->from)) { - return []; - } - - $retval = []; - - $tables = []; - - /** - * Expressions that may contain aliases. - * These are extracted from `FROM` and `JOIN` keywords. - * - * @var Expression[] - */ - $expressions = $statement->from; - - // Adding expressions from JOIN. - if (! empty($statement->join)) { - foreach ($statement->join as $join) { - $expressions[] = $join->expr; - } - } - - foreach ($expressions as $expr) { - if (! isset($expr->table) || ($expr->table === '')) { - continue; - } - - $thisDb = isset($expr->database) && ($expr->database !== '') ? - $expr->database : $database; - - if (! isset($retval[$thisDb])) { - $retval[$thisDb] = [ - 'alias' => null, - 'tables' => [], - ]; - } - - if (! isset($retval[$thisDb]['tables'][$expr->table])) { - $retval[$thisDb]['tables'][$expr->table] = [ - 'alias' => isset($expr->alias) && ($expr->alias !== '') ? - $expr->alias : null, - 'columns' => [], - ]; - } - - if (! isset($tables[$thisDb])) { - $tables[$thisDb] = []; - } - - $tables[$thisDb][$expr->alias] = $expr->table; - } - - foreach ($statement->expr as $expr) { - if (! isset($expr->column, $expr->alias) || ($expr->column === '') || ($expr->alias === '')) { - continue; - } - - $thisDb = isset($expr->database) && ($expr->database !== '') ? - $expr->database : $database; - - if (isset($expr->table) && ($expr->table !== '')) { - $thisTable = $tables[$thisDb][$expr->table] ?? $expr->table; - $retval[$thisDb]['tables'][$thisTable]['columns'][$expr->column] = $expr->alias; - } else { - foreach ($retval[$thisDb]['tables'] as &$table) { - $table['columns'][$expr->column] = $expr->alias; - } - } - } - - return $retval; - } -} diff --git a/tests/Utils/MiscTest.php b/tests/Utils/MiscTest.php deleted file mode 100644 index 192b22c34..000000000 --- a/tests/Utils/MiscTest.php +++ /dev/null @@ -1,137 +0,0 @@ -}> - * }> $expected - * - * @dataProvider getAliasesProvider - */ - public function testGetAliases(string $query, ?string $db, array $expected): void - { - $parser = new Parser($query); - $statement = empty($parser->statements[0]) ? - null : $parser->statements[0]; - $this->assertEquals($expected, Misc::getAliases($statement, $db)); - } - - /** - * @return array> - * @psalm-return list}> - * }>}> - */ - public static function getAliasesProvider(): array - { - return [ - [ - 'select * from (select 1) tbl', - 'mydb', - [], - ], - [ - 'select i.name as `n`,abcdef gh from qwerty i', - 'mydb', - [ - 'mydb' => [ - 'alias' => null, - 'tables' => [ - 'qwerty' => [ - 'alias' => 'i', - 'columns' => [ - 'name' => 'n', - 'abcdef' => 'gh', - ], - ], - ], - ], - ], - ], - [ - 'select film_id id,title from film', - 'sakila', - [ - 'sakila' => [ - 'alias' => null, - 'tables' => [ - 'film' => [ - 'alias' => null, - 'columns' => ['film_id' => 'id'], - ], - ], - ], - ], - ], - [ - 'select `sakila`.`A`.`actor_id` as aid,`F`.`film_id` `fid`,' - . 'last_update updated from `sakila`.actor A join `film_actor` as ' - . '`F` on F.actor_id = A.`actor_id`', - 'sakila', - [ - 'sakila' => [ - 'alias' => null, - 'tables' => [ - 'film_actor' => [ - 'alias' => 'F', - 'columns' => [ - 'film_id' => 'fid', - 'last_update' => 'updated', - ], - ], - 'actor' => [ - 'alias' => 'A', - 'columns' => [ - 'actor_id' => 'aid', - 'last_update' => 'updated', - ], - ], - ], - ], - ], - ], - [ - 'SELECT film_id FROM (SELECT * FROM film) as f;', - 'sakila', - [], - ], - [ - '', - null, - [], - ], - [ - 'SELECT 1', - null, - [], - ], - [ - 'SELECT * FROM orders AS ord WHERE 1', - 'db', - [ - 'db' => [ - 'alias' => null, - 'tables' => [ - 'orders' => [ - 'alias' => 'ord', - 'columns' => [], - ], - ], - ], - ], - ], - ]; - } -} From e60c36d06690d12048cae094616fc8d8dd99f471 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 31 May 2023 19:59:32 +0100 Subject: [PATCH 2/2] Update baselines Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 10 ---------- psalm-baseline.xml | 34 ++++++++++------------------------ 2 files changed, 10 insertions(+), 34 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c3e5e5d57..950d71912 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1185,16 +1185,6 @@ parameters: count: 1 path: tests/Parser/WithStatementTest.php - - - message: "#^Parameter \\#1 \\$statement of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Misc\\:\\:getAliases\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement, PhpMyAdmin\\\\SqlParser\\\\Statement\\|null given\\.$#" - count: 1 - path: tests/Utils/MiscTest.php - - - - message: "#^Parameter \\#2 \\$database of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Misc\\:\\:getAliases\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: tests/Utils/MiscTest.php - - message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#" count: 9 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7d800b1da..60a370a7b 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + partitions]]> @@ -191,9 +191,6 @@ - - \AllowDynamicProperties - keyword]]]> keyword]]]> @@ -236,6 +233,9 @@ + + \AllowDynamicProperties + @@ -4905,6 +4905,9 @@ getNextOfType getNextOfType + + list]]> + @@ -4914,9 +4917,6 @@ $built[$field] value]]]> - - \AllowDynamicProperties - __toString @@ -4952,6 +4952,9 @@ count($clauses) === 0 + + \AllowDynamicProperties + $index @@ -5423,14 +5426,6 @@ type === Token::TYPE_KEYWORD]]> - - - ! ($statement instanceof SelectStatement) - - - $tables[$thisDb] - - int @@ -5747,15 +5742,6 @@ setAccessible - - - $statement - - - $db - $statement - - list]]>