Skip to content

Commit 0d4d75e

Browse files
committed
Merge #454 - Remove Misc class
Pull-request: #454 Signed-off-by: William Desportes <[email protected]>
2 parents 8f524d7 + e47aef6 commit 0d4d75e

File tree

6 files changed

+202
-262
lines changed

6 files changed

+202
-262
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,16 +1185,6 @@ parameters:
11851185
count: 1
11861186
path: tests/Parser/WithStatementTest.php
11871187

1188-
-
1189-
message: "#^Parameter \\#1 \\$statement of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Misc\\:\\:getAliases\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement, PhpMyAdmin\\\\SqlParser\\\\Statement\\|null given\\.$#"
1190-
count: 1
1191-
path: tests/Utils/MiscTest.php
1192-
1193-
-
1194-
message: "#^Parameter \\#2 \\$database of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Misc\\:\\:getAliases\\(\\) expects string, string\\|null given\\.$#"
1195-
count: 1
1196-
path: tests/Utils/MiscTest.php
1197-
11981188
-
11991189
message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#"
12001190
count: 9

psalm-baseline.xml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5177,6 +5177,9 @@
51775177
</PossiblyNullOperand>
51785178
</file>
51795179
<file src="src/Statements/SelectStatement.php">
5180+
<PossiblyNullArrayOffset>
5181+
<code>$tables[$thisDb]</code>
5182+
</PossiblyNullArrayOffset>
51805183
<PossiblyUnusedProperty>
51815184
<code>$endOptions</code>
51825185
<code>$groupOptions</code>
@@ -5419,14 +5422,6 @@
54195422
<code><![CDATA[$curr->type === Token::TYPE_KEYWORD]]></code>
54205423
</RedundantConditionGivenDocblockType>
54215424
</file>
5422-
<file src="src/Utils/Misc.php">
5423-
<DocblockTypeContradiction>
5424-
<code>! ($statement instanceof SelectStatement)</code>
5425-
</DocblockTypeContradiction>
5426-
<PossiblyNullArrayOffset>
5427-
<code>$tables[$thisDb]</code>
5428-
</PossiblyNullArrayOffset>
5429-
</file>
54305425
<file src="src/Utils/Query.php">
54315426
<InvalidNullableReturnType>
54325427
<code>int</code>
@@ -5735,15 +5730,6 @@
57355730
<code>setAccessible</code>
57365731
</UnusedMethodCall>
57375732
</file>
5738-
<file src="tests/Utils/MiscTest.php">
5739-
<ArgumentTypeCoercion>
5740-
<code>$statement</code>
5741-
</ArgumentTypeCoercion>
5742-
<PossiblyNullArgument>
5743-
<code>$db</code>
5744-
<code>$statement</code>
5745-
</PossiblyNullArgument>
5746-
</file>
57475733
<file src="tests/Utils/QueryTest.php">
57485734
<PossiblyNullArgument>
57495735
<code><![CDATA[$parser->list]]></code>

src/Statements/SelectStatement.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,85 @@ public function getClauses()
366366

367367
return static::$clauses;
368368
}
369+
370+
/**
371+
* Gets a list of all aliases and their original names.
372+
*
373+
* @param string $database the name of the database
374+
*
375+
* @return array<string, array<string, array<string, array<string, array<string, string>|string|null>>|null>>
376+
*/
377+
public function getAliases(string $database): array
378+
{
379+
if (empty($this->expr) || empty($this->from)) {
380+
return [];
381+
}
382+
383+
$retval = [];
384+
385+
$tables = [];
386+
387+
/**
388+
* Expressions that may contain aliases.
389+
* These are extracted from `FROM` and `JOIN` keywords.
390+
*/
391+
$expressions = $this->from;
392+
393+
// Adding expressions from JOIN.
394+
if (! empty($this->join)) {
395+
foreach ($this->join as $join) {
396+
$expressions[] = $join->expr;
397+
}
398+
}
399+
400+
foreach ($expressions as $expr) {
401+
if (! isset($expr->table) || ($expr->table === '')) {
402+
continue;
403+
}
404+
405+
$thisDb = isset($expr->database) && ($expr->database !== '') ?
406+
$expr->database : $database;
407+
408+
if (! isset($retval[$thisDb])) {
409+
$retval[$thisDb] = [
410+
'alias' => null,
411+
'tables' => [],
412+
];
413+
}
414+
415+
if (! isset($retval[$thisDb]['tables'][$expr->table])) {
416+
$retval[$thisDb]['tables'][$expr->table] = [
417+
'alias' => isset($expr->alias) && ($expr->alias !== '') ?
418+
$expr->alias : null,
419+
'columns' => [],
420+
];
421+
}
422+
423+
if (! isset($tables[$thisDb])) {
424+
$tables[$thisDb] = [];
425+
}
426+
427+
$tables[$thisDb][$expr->alias] = $expr->table;
428+
}
429+
430+
foreach ($this->expr as $expr) {
431+
if (! isset($expr->column, $expr->alias) || ($expr->column === '') || ($expr->alias === '')) {
432+
continue;
433+
}
434+
435+
$thisDb = isset($expr->database) && ($expr->database !== '') ?
436+
$expr->database : $database;
437+
438+
if (isset($expr->table) && ($expr->table !== '')) {
439+
$thisTable = $tables[$thisDb][$expr->table] ?? $expr->table;
440+
$retval[$thisDb]['tables'][$thisTable]['columns'][$expr->column] = $expr->alias;
441+
} else {
442+
foreach ($retval[$thisDb]['tables'] as &$table) {
443+
$table['columns'][$expr->column] = $expr->alias;
444+
}
445+
}
446+
}
447+
448+
return $retval;
449+
}
369450
}

src/Utils/Misc.php

Lines changed: 0 additions & 98 deletions
This file was deleted.

tests/Builder/StatementTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\SqlParser\Components\Expression;
99
use PhpMyAdmin\SqlParser\Components\Limit;
1010
use PhpMyAdmin\SqlParser\Components\OptionsArray;
11+
use PhpMyAdmin\SqlParser\Parser;
1112
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
1213
use PhpMyAdmin\SqlParser\Tests\TestCase;
1314

@@ -39,4 +40,121 @@ public function testBuilder(): void
3940
(string) $stmt
4041
);
4142
}
43+
44+
/**
45+
* @psalm-param array<string, array{
46+
* alias: (string|null),
47+
* tables: array<string, array{alias: (string|null), columns: array<string, string>}>
48+
* }> $expected
49+
*
50+
* @dataProvider getAliasesProvider
51+
*/
52+
public function testGetAliases(string $query, string $db, array $expected): void
53+
{
54+
$parser = new Parser($query);
55+
$this->assertInstanceOf(SelectStatement::class, $parser->statements[0]);
56+
$this->assertEquals($expected, $parser->statements[0]->getAliases($db));
57+
}
58+
59+
/**
60+
* @psalm-return list<array{string, string, array<string, array{
61+
* alias: (string|null),
62+
* tables: array<string, array{alias: (string|null), columns: array<string, string>}>
63+
* }>}>
64+
*/
65+
public static function getAliasesProvider(): array
66+
{
67+
return [
68+
[
69+
'select * from (select 1) tbl',
70+
'mydb',
71+
[],
72+
],
73+
[
74+
'select i.name as `n`,abcdef gh from qwerty i',
75+
'mydb',
76+
[
77+
'mydb' => [
78+
'alias' => null,
79+
'tables' => [
80+
'qwerty' => [
81+
'alias' => 'i',
82+
'columns' => [
83+
'name' => 'n',
84+
'abcdef' => 'gh',
85+
],
86+
],
87+
],
88+
],
89+
],
90+
],
91+
[
92+
'select film_id id,title from film',
93+
'sakila',
94+
[
95+
'sakila' => [
96+
'alias' => null,
97+
'tables' => [
98+
'film' => [
99+
'alias' => null,
100+
'columns' => ['film_id' => 'id'],
101+
],
102+
],
103+
],
104+
],
105+
],
106+
[
107+
'select `sakila`.`A`.`actor_id` as aid,`F`.`film_id` `fid`,'
108+
. 'last_update updated from `sakila`.actor A join `film_actor` as '
109+
. '`F` on F.actor_id = A.`actor_id`',
110+
'sakila',
111+
[
112+
'sakila' => [
113+
'alias' => null,
114+
'tables' => [
115+
'film_actor' => [
116+
'alias' => 'F',
117+
'columns' => [
118+
'film_id' => 'fid',
119+
'last_update' => 'updated',
120+
],
121+
],
122+
'actor' => [
123+
'alias' => 'A',
124+
'columns' => [
125+
'actor_id' => 'aid',
126+
'last_update' => 'updated',
127+
],
128+
],
129+
],
130+
],
131+
],
132+
],
133+
[
134+
'SELECT film_id FROM (SELECT * FROM film) as f;',
135+
'sakila',
136+
[],
137+
],
138+
[
139+
'SELECT 1',
140+
'',
141+
[],
142+
],
143+
[
144+
'SELECT * FROM orders AS ord WHERE 1',
145+
'db',
146+
[
147+
'db' => [
148+
'alias' => null,
149+
'tables' => [
150+
'orders' => [
151+
'alias' => 'ord',
152+
'columns' => [],
153+
],
154+
],
155+
],
156+
],
157+
],
158+
];
159+
}
42160
}

0 commit comments

Comments
 (0)