Skip to content

Commit 3cfb999

Browse files
authored
Merge pull request #215 from simon-mundy/qa/mago-sql-array-key-first
Read join and FROM aliases with array_key_first()
2 parents 783540e + 2fee9ca commit 3cfb999

3 files changed

Lines changed: 26 additions & 20 deletions

File tree

‎analyzer-baseline.toml‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,12 +1788,6 @@ code = "overly-wide-return-type"
17881788
message = "Declared return type `null|string` for `processexpressionparametername` has unused branches: `null`."
17891789
count = 1
17901790

1791-
[[issues]]
1792-
file = "src/Sql/AbstractSql.php"
1793-
code = "possibly-false-argument"
1794-
message = 'Argument #1 of method `PhpDb\Adapter\Platform\PlatformInterface::quoteIdentifier` is possibly `false`, but parameter type `string` does not accept it.'
1795-
count = 1
1796-
17971791
[[issues]]
17981792
file = "src/Sql/AbstractSql.php"
17991793
code = "possibly-invalid-argument"
@@ -1860,6 +1854,12 @@ code = "possibly-null-argument"
18601854
message = 'Argument #4 of method `PhpDb\Sql\AbstractSql::processExpressionParameterName` is possibly `null`, but parameter type `PhpDb\Adapter\Driver\DriverInterface` does not accept it.'
18611855
count = 1
18621856

1857+
[[issues]]
1858+
file = "src/Sql/AbstractSql.php"
1859+
code = "possibly-undefined-string-array-index"
1860+
message = 'Possibly undefined array key `string` accessed on `non-empty-array<string, PhpDb\Sql\Expression|PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|string>`.'
1861+
count = 1
1862+
18631863
[[issues]]
18641864
file = "src/Sql/AbstractSql.php"
18651865
code = "redundant-cast"
@@ -3582,12 +3582,6 @@ code = "possible-method-access-on-null"
35823582
message = "Attempting to call a method on `null`."
35833583
count = 2
35843584

3585-
[[issues]]
3586-
file = "src/Sql/Select.php"
3587-
code = "possibly-false-argument"
3588-
message = 'Argument #1 of method `PhpDb\Sql\AbstractSql::resolveTable` is possibly `false`, but parameter type `PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|null|string` does not accept it.'
3589-
count = 1
3590-
35913585
[[issues]]
35923586
file = "src/Sql/Select.php"
35933587
code = "possibly-false-iterator"
@@ -3636,6 +3630,12 @@ code = "possibly-null-argument"
36363630
message = 'Argument #1 of method `PhpDb\Sql\AbstractSql::renderTable` is possibly `null`, but parameter type `string` does not accept it.'
36373631
count = 1
36383632

3633+
[[issues]]
3634+
file = "src/Sql/Select.php"
3635+
code = "possibly-null-array-index"
3636+
message = "Possibly using `null` as an array index to access elementof variable $table."
3637+
count = 1
3638+
36393639
[[issues]]
36403640
file = "src/Sql/Select.php"
36413641
code = "possibly-null-operand"
@@ -3672,6 +3672,12 @@ code = "possibly-undefined-string-array-index"
36723672
message = '''Possibly undefined array key 'type' accessed on `array{'modifier'?: truthy-string, 'select'?: PhpDb\Sql\Select, 'type'?: string}`.'''
36733673
count = 1
36743674

3675+
[[issues]]
3676+
file = "src/Sql/Select.php"
3677+
code = "possibly-undefined-string-array-index"
3678+
message = 'Possibly undefined array key `null|string` accessed on `array<string, PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|string>`.'
3679+
count = 1
3680+
36753681
[[issues]]
36763682
file = "src/Sql/Select.php"
36773683
code = "redundant-comparison"

‎src/Sql/AbstractSql.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
use PhpDb\Sql\Argument\Values;
1818
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
1919

20+
use function array_key_first;
2021
use function count;
21-
use function current;
2222
use function get_object_vars;
2323
use function implode;
2424
use function is_array;
2525
use function is_string;
26-
use function key;
2726
use function rtrim;
2827
use function str_replace;
2928
use function strtoupper;
@@ -343,8 +342,9 @@ protected function processJoin(
343342
$joinAs = null;
344343
$joinNameValue = $join['name'];
345344
if (is_array($joinNameValue)) {
346-
$joinName = current($joinNameValue);
347-
$joinAs = $platform->quoteIdentifier(key($joinNameValue));
345+
$alias = array_key_first($joinNameValue);
346+
$joinName = $joinNameValue[$alias];
347+
$joinAs = $platform->quoteIdentifier($alias);
348348
} else {
349349
$joinName = $joinNameValue;
350350
}

‎src/Sql/Select.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use PhpDb\Sql\Predicate\PredicateInterface;
1313

1414
use function array_key_exists;
15+
use function array_key_first;
1516
use function count;
16-
use function current;
1717
use function explode;
1818
use function gettype;
1919
use function is_array;
@@ -650,7 +650,7 @@ protected function processSelect(
650650
}
651651

652652
foreach ($this->getJoins()->getJoins() as $join) {
653-
$joinName = is_array($join['name']) ? key($join['name']) : $join['name'];
653+
$joinName = is_array($join['name']) ? array_key_first($join['name']) : $join['name'];
654654
$joinName = parent::resolveTable($joinName, $platform, $driver, $parameterContainer);
655655

656656
foreach ($join['columns'] as $jKey => $jColumn) {
@@ -746,8 +746,8 @@ protected function resolveTable(
746746
$alias = null;
747747

748748
if (is_array($table)) {
749-
$alias = key($table);
750-
$table = current($table);
749+
$alias = array_key_first($table);
750+
$table = $table[$alias];
751751
}
752752

753753
$table = parent::resolveTable($table, $platform, $driver, $parameterContainer);

0 commit comments

Comments
 (0)