Remove Misc class by kamil-tekiela · Pull Request #454 · phpmyadmin/sql-parser · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
20 changes: 3 additions & 17 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5177,6 +5177,9 @@
</PossiblyNullOperand>
</file>
<file src="src/Statements/SelectStatement.php">
<PossiblyNullArrayOffset>
<code>$tables[$thisDb]</code>
</PossiblyNullArrayOffset>
<PossiblyUnusedProperty>
<code>$endOptions</code>
<code>$groupOptions</code>
Expand Down Expand Up @@ -5419,14 +5422,6 @@
<code><![CDATA[$curr->type === Token::TYPE_KEYWORD]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Utils/Misc.php">
<DocblockTypeContradiction>
<code>! ($statement instanceof SelectStatement)</code>
</DocblockTypeContradiction>
<PossiblyNullArrayOffset>
<code>$tables[$thisDb]</code>
</PossiblyNullArrayOffset>
</file>
<file src="src/Utils/Query.php">
<InvalidNullableReturnType>
<code>int</code>
Expand Down Expand Up @@ -5735,15 +5730,6 @@
<code>setAccessible</code>
</UnusedMethodCall>
</file>
<file src="tests/Utils/MiscTest.php">
<ArgumentTypeCoercion>
<code>$statement</code>
</ArgumentTypeCoercion>
<PossiblyNullArgument>
<code>$db</code>
<code>$statement</code>
</PossiblyNullArgument>
</file>
<file src="tests/Utils/QueryTest.php">
<PossiblyNullArgument>
<code><![CDATA[$parser->list]]></code>
Expand Down
81 changes: 81 additions & 0 deletions src/Statements/SelectStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,85 @@ public function getClauses()

return static::$clauses;
}

/**
* Gets a list of all aliases and their original names.
*
* @param string $database the name of the database
*
* @return array<string, array<string, array<string, array<string, array<string, string>|string|null>>|null>>
*/
public function getAliases(string $database): array
{
if (empty($this->expr) || empty($this->from)) {
return [];
}

$retval = [];

$tables = [];

/**
* Expressions that may contain aliases.
* These are extracted from `FROM` and `JOIN` keywords.
*/
$expressions = $this->from;

// Adding expressions from JOIN.
if (! empty($this->join)) {
foreach ($this->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 ($this->expr as $expr) {
if (! isset($expr->column, $expr->alias) || ($expr->column === '') || ($expr->alias === '')) {
continue;
Comment thread
williamdes marked this conversation as resolved.
}

$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;
}
}
98 changes: 0 additions & 98 deletions src/Utils/Misc.php

This file was deleted.

118 changes: 118 additions & 0 deletions tests/Builder/StatementTest.php
Loading