Skip to content

Commit 04242cf

Browse files
committed
Avoid using a null alias as an array offset in getAliases()
SelectStatement::getAliases() built a table-alias map with `$tables[$thisDb][$expr->alias] = $expr->table` for every FROM/JOIN expression. When a table has no alias, `$expr->alias` is null, so this used null as an array offset, which is deprecated as of PHP 8.5 ("Using null as an array offset is deprecated, use an empty string instead"). The null-keyed entry was never read back either: the only consumer looks up `$tables[$thisDb][$expr->table]`, whose offset is always a non-empty string. Skip expressions without an alias, mirroring the existing `isset($expr->alias) && $expr->alias !== ''` checks above. getAliases() output is unchanged. Removes the now-resolved PossiblyNullArrayOffset entry from the Psalm baseline and adds a regression test.
1 parent 0ee3626 commit 04242cf

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,6 @@
806806
<code><![CDATA[array<string, array<string, array<string, array<string, array<string, string>|string|null>>|null>>]]></code>
807807
</MixedReturnTypeCoercion>
808808
<PossiblyNullArrayOffset>
809-
<code><![CDATA[$tables[$thisDb]]]></code>
810809
<code><![CDATA[Parser::STATEMENT_PARSERS]]></code>
811810
</PossiblyNullArrayOffset>
812811
<PossiblyUnusedProperty>

src/Statements/SelectStatement.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,13 @@ public function getAliases(string $database): array
607607
];
608608
}
609609

610+
// Only record an alias => table mapping when an alias exists.
611+
// Using a null alias as an array offset is deprecated as of
612+
// PHP 8.5, and such an entry is never read back below anyway.
613+
if (! isset($expr->alias) || ($expr->alias === '')) {
614+
continue;
615+
}
616+
610617
if (! isset($tables[$thisDb])) {
611618
$tables[$thisDb] = [];
612619
}

tests/Builder/StatementTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ public static function getAliasesProvider(): array
155155
],
156156
],
157157
],
158+
[
159+
// An unaliased table alongside an aliased one. The unaliased
160+
// table must not break alias resolution; previously its null
161+
// alias was used as an array offset (deprecated in PHP 8.5).
162+
'SELECT mytable.a x, o.b y FROM mytable JOIN other o ON o.id = mytable.id',
163+
'shop',
164+
[
165+
'shop' => [
166+
'alias' => null,
167+
'tables' => [
168+
'mytable' => [
169+
'alias' => null,
170+
'columns' => ['a' => 'x'],
171+
],
172+
'other' => [
173+
'alias' => 'o',
174+
'columns' => ['b' => 'y'],
175+
],
176+
],
177+
],
178+
],
179+
],
158180
];
159181
}
160182
}

0 commit comments

Comments
 (0)